1.环境搭建:见上一篇博客 整个项目的结构图: 2.编写DetectFaceDemo.java,代码如下:[java] view plaincopyprint? 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) }} package com.njupt.zhb.testimport org.opencv.core.Coreimport org.opencv.core.Matimport org.opencv.core.MatOfRectimport org.opencv.core.Pointimport org.opencv.core.Rectimport org.opencv.core.Scalarimport org.opencv.highgui.Highguiimport 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.编写测试类:[java] view plaincopyprint? 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 package com.njupt.zhb.testpublic 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.png1、首先
就是数据的准备,你要从网络上下载一些人脸库,后面用来训练人脸识别
模型。人脸检测模型opencv是自带的,但是识别模型需要自己训练。下载人脸库之后需要对人脸进行标记,这是一个繁琐的工作,不过网上有脚本或者自己写个程序简化工作量。
2、把数据标记好之后就是opencv的事情。训练的函数非常简单。只有下面这三句:
Ptr<FaceRecognizer>model = createEigenFaceRecognizer()
model->train(images, labels)
model->save("MyFacePCAModel.xml")
3、然后打开摄像头进行人脸检测,就是框出人脸的位置。人脸检测模型是opencv自带的。
CascadeClassifier cascade
cascade.load("haarcascade_frontalface_alt.xml")
这是加载的方法。
cascade.detectMultiScale(gray, faces,
1.1, 2, 0
//|CV_HAAR_FIND_BIGGEST_OBJECT
//|CV_HAAR_DO_ROUGH_SEARCH
| CV_HAAR_SCALE_IMAGE,
Size(30, 30))
这是检测的方法。这里检测得到的就是一个一个的人脸的矩形框,然后用画矩形的方法把它们画出来就行了。
4、然后就是对检测到的人脸进行识别了,用我们刚才训练好的人脸模型。加载:
Ptr<FaceRecognizer>modelPCA = createEigenFaceRecognizer()
modelPCA->load("MyFacePCAModel.xml")
检测:
int predictPCA = 0
if (face.rows >= 120)
{
resize(face, face_test, Size(92, 112))
}
if (!face_test.empty())
{
predictPCA = modelPCA->predict(face_test)
}
如果预测结果等于标记结果,说明识别正确。
大致流程就这样了,如果你对opencv有了一定的了解,应该能看懂了。
C++. 首先OpenCV2和3本身就是用C++编写的, 用C++可以做到和OpenCV的"无缝对接", OpenCV的C++资料也最多, 其次人脸识别等与图像相关的代码需要很高的运行时效率, 而C++的运行时效率远大于java, 最后, 在人脸识别的计算过程中有协方差矩阵的计算需要很大的内存(空间复杂度O(n^2),n为图像像素数,本身又是边长的平方), 所以要有一个内存动态管理方案, 或者从线性代数上对协方差矩阵的计算进行简化, 而高级编程语言中只有C和C++这两种语言才能直接 *** 作内存.
评论列表(0条)