opencv 人脸识别 java版------2
发布日期:2021-06-30 20:07:36 浏览次数:2 分类:技术文章

本文共 2534 字,大约阅读时间需要 8 分钟。

代码下载地址 http://download.csdn.net/detail/u013378306/9656546

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("\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.编写测试类:

 

 

  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  

 

 

 

运行结果:

 

 

转载地址:https://liuhuiyao.blog.csdn.net/article/details/52846438 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:centos7 网速很慢解决办法----关闭ipv6
下一篇:opencv 人脸识别 环境搭建---java版-----1

发表评论

最新留言

很好
[***.229.124.182]2024年04月29日 10时33分08秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

刷好老毛子系统进不了老毛子系统后台的解决办法 2019-04-30
Parallels Desktop 16 不能联网的解决办法 2019-04-30
ERROR 1292 (22007): Incorrect datetime value: ‘2002‘ for column ‘出版日期‘ at row 1 2019-04-30
SLAM中TUM数据集更改图片名字 2019-04-30
【并发控制】并发控制与分布式锁(redis/zookeeper)实现【图文教程】_ 第1章 2019-04-30
【并发控制】并发控制与分布式锁(redis/zookeeper)实现【图文教程】_ 第2章 2019-04-30
【并发控制】并发控制与分布式锁(redis/zookeeper)实现【图文教程】_ 第3章 2019-04-30
【并发控制】并发控制与分布式锁(redis/zookeeper)实现【图文教程】_ 第4章 2019-04-30
【并发控制】并发控制与分布式锁(redis/zookeeper)实现【图文教程】_ 第5章 2019-04-30
synchronized和CAS锁的区别【图文教程】 2019-04-30
【java】属性别名:@JsonProperty和@JSONField的区别?【图文教程】 2019-04-30
配置nginx只允许域名访问,禁止ip访问【图文教程】 2019-04-30
Java代理【图文教程】_第1章_静态代理 2019-04-30
Java代理【图文教程】_第2章_jdk动态代理 2019-04-30
AOP面向切面编程【图文教程】_第1章 2019-04-30
AOP面向切面编程【图文教程】_第2章 2019-04-30
二叉树之前序、中序、后序和层次遍历【图文教程】 2019-04-30
【java】class内部模块(静态方法,静态块,静态变量,方法块等)执行顺序【图文教程】 2019-04-30
java类的构成 2019-04-30
创建安装linux:centOS 2019-04-30