opencv 人脸识别 java版------2
发布日期:2021-06-30 20:07:36
浏览次数:2
分类:技术文章
本文共 2534 字,大约阅读时间需要 8 分钟。
代码下载地址 http://download.csdn.net/detail/u013378306/9656546
1.环境搭建:见上一篇博客
整个项目的结构图:
2.编写DetectFaceDemo.java,代码如下:
- package com.njupt.zhb.test;
- import org.opencv.core.Core;
- import org.opencv.core.Mat;
- import org.opencv.core.MatOfRect;
- import org.opencv.core.Point;
- import org.opencv.core.Rect;
- import org.opencv.core.Scalar;
- import org.opencv.highgui.Highgui;
- import org.opencv.objdetect.CascadeClassifier;
- //
- // Detects faces in an image, draws boxes around them, and writes the results
- // to "faceDetection.png".
- //
- public class DetectFaceDemo {
- public void run() {
- System.out.println("\nRunning DetectFaceDemo");
- System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
- // Create a face detector from the cascade file in the resources
- // directory.
- //CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
- //Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
- //注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
- /*
- * Detected 0 faces Writing faceDetection.png libpng warning: Image
- * width is zero in IHDR libpng warning: Image height is zero in IHDR
- * libpng error: Invalid IHDR data
- */
- //因此,我们将第一个字符去掉
- String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
- CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
- Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
- // Detect faces in the image.
- // MatOfRect is a special container class for Rect.
- MatOfRect faceDetections = new MatOfRect();
- faceDetector.detectMultiScale(image, faceDetections);
- System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
- // Draw a bounding box around each face.
- for (Rect rect : faceDetections.toArray()) {
- Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
- }
- // Save the visualized detection.
- String filename = "faceDetection.png";
- System.out.println(String.format("Writing %s", filename));
- Highgui.imwrite(filename, image);
- }
- }
3.编写测试类:
- package com.njupt.zhb.test;
- public class TestMain {
- public static void main(String[] args) {
- System.out.println("Hello, OpenCV");
- // Load the native library.
- System.loadLibrary("opencv_java246");
- new DetectFaceDemo().run();
- }
- }
- //运行结果:
- //Hello, OpenCV
- //
- //Running DetectFaceDemo
- ///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
- //Detected 8 faces
- //Writing faceDetection.png
运行结果:
转载地址:https://liuhuiyao.blog.csdn.net/article/details/52846438 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月29日 10时33分08秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
刷好老毛子系统进不了老毛子系统后台的解决办法
2019-04-30
Parallels Desktop 16 不能联网的解决办法
2019-04-30
SLAM中TUM数据集更改图片名字
2019-04-30
synchronized和CAS锁的区别【图文教程】
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类的构成
2019-04-30
创建安装linux:centOS
2019-04-30