packag,低调大师" />

Eclipse+Java+OpenCV246人脸识别

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

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

整个项目的结构图:


2.编写DetectFaceDemo.java,代码如下:
[java]   view plain copy
  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 plain copy
  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,代码如下:
[java]   view plain copy
  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 plain copy
  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

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

优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/245949

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
优质分享Android(本站安卓app)

优质分享Android(本站安卓app)

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario,低调大师唯一一个Java游戏作品

Mario,低调大师唯一一个Java游戏作品

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。