1、DetectFace
package com.facedetect.func;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
/**
-
@Auther: DarkKing
-
@Date: 2019/10/2 11:06
-
@Description:
*/
public class DetectFace {
//定义程序的基础路径
private String basePath =System.getProperty("user.dir");
//人眼识别分类器路径
private String eyeConfigPath=basePath+"\src\com\facedetect\config\haarcascade_eye_tree_eyeglasses.xml";
//人脸识别分类器路径
private String faceConfigPath=basePath+"\src\com\facedetect\config\haarcascade_frontalface_alt2.xml";
static{
// 载入 opencv 的库
String opencvpath = System.getProperty("user.dir") + "\libs\x64\";
String opencvDllName = opencvpath + Core.NATIVE_LIBRARY_NAME + ".dll";
System.load(opencvDllName);
}
/**
-
opencv 实现人脸识别
-
@param imagePath
-
@param outFile
-
@throws Exception
*/
public void detectFace(String imagePath, String outFile) throws Exception
{
System.out.println("Running DetectFace ...,config path is "+faceConfigPath);
String basePath =System.getProperty("user.dir");
String path= basePath+ "\src\com\facedetect\tmp\";
// 从配置文件 lbpcascade_frontalface.xml 中创建一个人脸识别器,该文件位于 opencv 安装目录中,为了方便从安装方便放到了程序路径里
CascadeClassifier faceDetector = new CascadeClassifier(faceConfigPath);
//创建图片处理对象
Mat image = Imgcodecs.imread(imagePath);
// 在图片中检测人脸
MatOfRect faceDetections = new MatOfRect();
//多条件结果检测
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
//检测结果集
Rect[] rects = faceDetections.toArray();
// 在每一个识别出来的人脸周围画出一个方框
for (int i = 0; i < rects.length; i++) {
Rect rect = rects[i];
Imgproc.rectangle(image, new Point(rect.x-2, rect.y-2),
new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0));
Mat copy = new