<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    Eclipse+Java+OpenCV246人臉識別

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    ---恢復內容開始---

    1.環境搭建:見上一篇博客

    整個項目的結構圖:

    RFID設備管理軟件

    2.編寫DetectFaceDemo.java,代碼如下:
    [java] view plaincopy
    1. package com.njupt.zhb.test;  
    2. import org.opencv.core.Core;  
    3. import org.opencv.core.Mat;  
    4. import org.opencv.core.MatOfRect;  
    5. import org.opencv.core.Point;  
    6. import org.opencv.core.Rect;  
    7. import org.opencv.core.Scalar;  
    8. import org.opencv.highgui.Highgui;  
    9. import org.opencv.objdetect.CascadeClassifier;  
    10.   
    11. //  
    12. // Detects faces in an image, draws boxes around them, and writes the results  
    13. // to "faceDetection.png".  
    14. //  
    15. public class DetectFaceDemo {  
    16.   public void run() {  
    17.     System.out.println("\nRunning DetectFaceDemo");  
    18.     System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());  
    19.     // Create a face detector from the cascade file in the resources  
    20.     // directory.  
    21.     //CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());  
    22.     //Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());  
    23.     //注意:源程序的路徑會多打印一個‘/’,因此總是出現如下錯誤  
    24.         /* 
    25.          * Detected 0 faces Writing faceDetection.png libpng warning: Image 
    26.          * width is zero in IHDR libpng warning: Image height is zero in IHDR 
    27.          * libpng error: Invalid IHDR data 
    28.          */  
    29.     //因此,我們將第一個字符去掉  
    30.     String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);  
    31.     CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);  
    32.     Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));  
    33.     // Detect faces in the image.  
    34.     // MatOfRect is a special container class for Rect.  
    35.     MatOfRect faceDetections = new MatOfRect();  
    36.     faceDetector.detectMultiScale(image, faceDetections);  
    37.   
    38.     System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));  
    39.   
    40.     // Draw a bounding box around each face.  
    41.     for (Rect rect : faceDetections.toArray()) {  
    42.         Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(02550));  
    43.     }  
    44.   
    45.     // Save the visualized detection.  
    46.     String filename = "faceDetection.png";  
    47.     System.out.println(String.format("Writing %s", filename));  
    48.     Highgui.imwrite(filename, image);  
    49.   }  
    50. }  


    3.編寫測試類:
    [java] view plaincopy
    1. package com.njupt.zhb.test;  
    2. public class TestMain {  
    3.   public static void main(String[] args) {  
    4.     System.out.println("Hello, OpenCV");  
    5.     // Load the native library.  
    6.     System.loadLibrary("opencv_java246");  
    7.     new DetectFaceDemo().run();  
    8.   }  
    9. }  
    10. //運行結果:  
    11. //Hello, OpenCV  
    12. //  
    13. //Running DetectFaceDemo  
    14. ///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml  
    15. //Detected 8 faces  
    16. //Writing faceDetection.png  


    運行結果:
    RFID設備管理軟件

    源碼下載:http://download.csdn.net/detail/nuptboyzhb/5961985

    未經允許,不得用于商業目的

    ---恢復內容結束---

    1.環境搭建:見上一篇博客

    整個項目的結構圖:

    RFID設備管理軟件

    2.編寫DetectFaceDemo.java,代碼如下:
    [java] view plaincopy
    1. package com.njupt.zhb.test;  
    2. import org.opencv.core.Core;  
    3. import org.opencv.core.Mat;  
    4. import org.opencv.core.MatOfRect;  
    5. import org.opencv.core.Point;  
    6. import org.opencv.core.Rect;  
    7. import org.opencv.core.Scalar;  
    8. import org.opencv.highgui.Highgui;  
    9. import org.opencv.objdetect.CascadeClassifier;  
    10.   
    11. //  
    12. // Detects faces in an image, draws boxes around them, and writes the results  
    13. // to "faceDetection.png".  
    14. //  
    15. public class DetectFaceDemo {  
    16.   public void run() {  
    17.     System.out.println("\nRunning DetectFaceDemo");  
    18.     System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());  
    19.     // Create a face detector from the cascade file in the resources  
    20.     // directory.  
    21.     //CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());  
    22.     //Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());  
    23.     //注意:源程序的路徑會多打印一個‘/’,因此總是出現如下錯誤  
    24.         /* 
    25.          * Detected 0 faces Writing faceDetection.png libpng warning: Image 
    26.          * width is zero in IHDR libpng warning: Image height is zero in IHDR 
    27.          * libpng error: Invalid IHDR data 
    28.          */  
    29.     //因此,我們將第一個字符去掉  
    30.     String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);  
    31.     CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);  
    32.     Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));  
    33.     // Detect faces in the image.  
    34.     // MatOfRect is a special container class for Rect.  
    35.     MatOfRect faceDetections = new MatOfRect();  
    36.     faceDetector.detectMultiScale(image, faceDetections);  
    37.   
    38.     System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));  
    39.   
    40.     // Draw a bounding box around each face.  
    41.     for (Rect rect : faceDetections.toArray()) {  
    42.         Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(02550));  
    43.     }  
    44.   
    45.     // Save the visualized detection.  
    46.     String filename = "faceDetection.png";  
    47.     System.out.println(String.format("Writing %s", filename));  
    48.     Highgui.imwrite(filename, image);  
    49.   }  
    50. }  


    3.編寫測試類:
    [java] view plaincopy
    1. package com.njupt.zhb.test;  
    2. public class TestMain {  
    3.   public static void main(String[] args) {  
    4.     System.out.println("Hello, OpenCV");  
    5.     // Load the native library.  
    6.     System.loadLibrary("opencv_java246");  
    7.     new DetectFaceDemo().run();  
    8.   }  
    9. }  
    10. //運行結果:  
    11. //Hello, OpenCV  
    12. //  
    13. //Running DetectFaceDemo  
    14. ///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml  
    15. //Detected 8 faces  
    16. //Writing faceDetection.png  


    運行結果:
    RFID設備管理軟件

    源碼下載:http://download.csdn.net/detail/nuptboyzhb/5961985

    未經允許,不得用于商業目的

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全