zoukankan      html  css  js  c++  java
  • Eclipse+Java+OpenCV246人脸识别

    ---恢复内容开始---

    1.环境搭建:见上一篇博客

    整个项目的结构图:


    2.编写DetectFaceDemo.java,代码如下:
    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(" Running 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.编写测试类:
    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  


    运行结果:


    源码下载:http://download.csdn.net/detail/nuptboyzhb/5961985

    未经允许,不得用于商业目的

    ---恢复内容结束---

    1.环境搭建:见上一篇博客

    整个项目的结构图:


    2.编写DetectFaceDemo.java,代码如下:
    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(" Running 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.编写测试类:
    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  


    运行结果:


    源码下载:http://download.csdn.net/detail/nuptboyzhb/5961985

    未经允许,不得用于商业目的

  • 相关阅读:
    [dfs+水] hdu 4462 Scaring the Birds
    [Leetcode]-containsNearbyDuplicate
    測试流程的规范性与重要性
    柯里化函数之Javascript
    android AChartEngine源代码
    Android高斯模糊
    linux64位系统中g++4.4.7下使用wcsncpy函数有造成段错误的BUG(其它g++版本号未知)
    便利贴女孩_百度百科
    法爱格官方旗舰店
    法爱格2014 春夏新款欧美纯色修身高腰无袖吊带V领 拼接性感 连衣裙 黑色 M【图片 价格 品牌 报价】-京东
  • 原文地址:https://www.cnblogs.com/lidabo/p/3501285.html
Copyright © 2011-2022 走看看