一小时,从零实现Java人脸识别功能,opencv
一小时,从零实现Java人脸识别
本案例成功与2021,09,02
此样图在本教程基础可实现,并非完全次教程实例图。
1. 安装OpenCv环境
实验环境为win,自行选择
下载成功后,安装即可
2. 进入开发
本案例使用Maven搭建
pom.xml(注意maven的opencv和自己下载的opencv版本需一致)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Opencv</groupId>
<artifactId>Opencv</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.5.3-1.5.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>
编写实体类
/**
* @Author: 王居三木超
* @Description: TODO
* @DateTime: 2021/9/2 19:54
**/
public class CvtMatEntity {
//原图Mat
public Mat img;
//灰度图Mat
public Mat gray;
public static CvtMatEntity cvtR2G(Mat img){
CvtMatEntity cvtMatEntity = new CvtMatEntity();
Mat rgb = new Mat();
//实现图片灰度转换
Imgproc.cvtColor(img, rgb, Imgproc.COLOR_BGR2RGB);
Mat gray = new Mat();
Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_RGB2GRAY);
//赋值
cvtMatEntity.img = img;
cvtMatEntity.gray = gray;
//返回
return cvtMatEntity;
}
}
编写核心类
/**
* @Author: 王居三木超
* @Description: TODO
* @DateTime: 2021/9/2 17:41
**/
public class InitInstance {
private static Logger logger = LoggerFactory.getLogger(InitInstance.class);
//脸部识别实例
private static CascadeClassifier faceDetector;
//此类加载人脸识别模块
public static void init(String dllAbsPath, String facexmlAbsPath, String eyexmlAbsPath) {
logger.info("开始读取脸部识别实例");
//加载dll文件
System.load(dllAbsPath);
faceDetector = new CascadeClassifier(facexmlAbsPath);
if (faceDetector.empty()) {
logger.error("人脸识别模块读取失败");
} else logger.info("人脸识别模块读取成功");
}
//此类实现打开视频,识别人脸
public static void videoDetectorModel() {
//打开摄像头
VideoCapture videoCapture = new VideoCapture(0);
//判断摄像头是否打开
if (!videoCapture.open(0)) {
logger.error("相机打开失败");
return;
}
while (true) {
//创建图片Mat
Mat img = new Mat();
//读取摄像头下的图像
if (!videoCapture.read(img)) return;
//为保证教程详细度,此处不调用实体方法,大家可自行选择
//图片灰度转化
Mat rgb = new Mat();
Imgproc.cvtColor(img, rgb, Imgproc.COLOR_BGR2RGB);
Mat gray = new Mat();
Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_RGB2GRAY);
//创建人脸识别出的矩形变量
MatOfRect faveRect = new MatOfRect();
//检测人脸
faceDetector.detectMultiScale(gray, faveRect);
//图形面勾选人脸
for (Rect re : faveRect.toArray()) {
Imgproc.rectangle(img, new Point(re.x, re.y), new Point(re.x + re.width, re.y + re.height), new Scalar(0, 0, 255), 2);
}
//显示在屏幕
HighGui.imshow("人脸识别", img);
//按'q'退出
if (HighGui.waitKey(1) == 81) break;
}
//释放资源
videoCapture.release();
HighGui.destroyAllWindows();
}
//以下内容为对比人脸模块。与打开视频,识别人脸完全分离
/**
* 获取灰度人脸
*/
public static Mat conv_Mat(String img) {
//读取图片Mat
Mat imgInfo = Imgcodecs.imread(img);
//此处调用了实体方法,实现灰度转化
CvtMatEntity cvtMatEntity = CvtMatEntity.cvtR2G(imgInfo);
//创建Mat矩形
MatOfRect faceMat = new MatOfRect();
//识别人人脸
faceDetector.detectMultiScale(cvtMatEntity.gray, faceMat);
for (Rect rect : faceMat.toArray()) {
//选出灰度人脸
Mat face = new Mat(cvtMatEntity.gray, rect);
return face;
}
return null;
}
/**
* 图片对比人脸
*/
public static double compare_image(String img_1, String img_2) {
//获得灰度人脸
Mat mat_1 = conv_Mat(img_1);
Mat mat_2 = conv_Mat(img_2);
Mat hist_1 = new Mat();
Mat hist_2 = new Mat();
//参数定义
MatOfFloat ranges = new MatOfFloat(0f, 256f);
MatOfInt histSize = new MatOfInt(10000000);
//实现图片计算
Imgproc.calcHist(Arrays.asList(mat_1), new MatOfInt(0), new Mat(), hist_1, histSize, ranges);
Imgproc.calcHist(Arrays.asList(mat_2), new MatOfInt(0), new Mat(), hist_2, histSize, ranges);
// 相关系数,获得相似度
double res = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL);
//返回相似度
return res;
}
}
3. 主函数调用
/**
* @Author: 王居三木超
* @Description: TODO
* @DateTime: 2021/9/2 17:32
**/
public class openapiMainApplication {
public static void main(String[] args) throws UnsupportedEncodingException {
//此为opencv的opencv_java453.dll
//位置在opencv安装目录下的build\\java\\x64\\位置
String dllAbsPath = "D:\\Users\\86159\\Desktop\\CloudPool\\opencv\\opencv\\build\\java\\x64\\opencv_java453.dll";
//位置在opencv安装目录下的sources\\data\\haarcascades\\位置
String facexmlAbsPath = "D:\\Users\\86159\\Desktop\\CloudPool\\opencv\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
//必须加载
InitInstance.init(dllAbsPath, facexmlAbsPath,eyexmlAbsPath);
// InitInstance.videoDetectorModel();
// System.out.println(InitInstance.compare_image("D:\\Users\\86159\\Desktop\\TEST\\2.png", "D:\\Users\\86159\\Desktop\\TEST\\2.png"));
}
}