因为学校副院长带我们学习OpenCV做计算机视觉图像处理,小杨一向都是非常对于新技术都是非常追求的。在翻阅了众多大牛写的博客文章和百度上的资料后,终于简单的实现了一个通过计算机调取本地摄像头做人脸识别的功能。
1.下载
首先,我们要去opencv
的官网下载windows系统的安装包:
下载地址:https://opencv.org/releases/
1.把图1下面bin目录的文件保存到图2中:
2.导入依赖
<!-- opencv + javacv + ffmpeg-->
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg</artifactId>
<version>4.1-1.4.4</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/ffmpeg-platform -->
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>4.1-1.4.4</version>
</dependency>
<!-- 视频摄像头 -->
<!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-platform -->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/opencv-platform -->
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv-platform</artifactId>
<version>4.0.1-1.4.4</version>
</dependency>
3.导入库依赖File --> Project Structure,点击Modules,选择需要使用opencv.jar的项目。
4.人脸识别Demo:
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.VideoWriter;
import org.opencv.videoio.Videoio;
import java.util.Arrays;
import java.util.List;
/**
*
* @Title: Opencv 图片人脸识别、实时摄像头人脸识别、视频文件人脸识别
* @Description: OpenCV-4.1.1 测试文件
* @version: V-1.0.0
*
*/
public class FaceVideo {
// 初始化人脸探测器
static CascadeClassifier faceDetector;
static int i=0;
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
faceDetector = new CascadeClassifier("D:\\OpenCV\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
}
public static void main(String[] args) {
// 1- 从摄像头实时人脸识别,识别成功保存图片到本地
getVideoFromCamera();
/**
* OpenCV-4.1.1 从摄像头实时读取
* @return: void
*/
public static void getVideoFromCamera() {
//1 如果要从摄像头获取视频 则要在 VideoCapture 的构造方法写 0
VideoCapture capture=new VideoCapture(0);
Mat video=new Mat();
int index=0;
if (capture.isOpened()) {
while(i<3) {// 匹配成功3次退出
capture.read(video);
HighGui.imshow("实时人脸识别", getFace(video));
index=HighGui.waitKey(100);
if (index==27) {
capture.release();
break;
}
}
}else{
System.out.println("摄像头未开启");
}
try {
capture.release();
Thread.sleep(1000);
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
return;
}
/**
* OpenCV-4.1.1 人脸识别
* @param image 待处理Mat图片(视频中的某一帧)
* @return 处理后的图片
*/
public static Mat getFace(Mat image) {
// 1 读取OpenCV自带的人脸识别特征XML文件(faceDetector)
// CascadeClassifier facebook=new CascadeClassifier("D:\\Sofeware\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
// 2 特征匹配类
MatOfRect face = new MatOfRect();
// 3 特征匹配
faceDetector.detectMultiScale(image, face);
Rect[] rects=face.toArray();
System.out.println("匹配到 "+rects.length+" 个人脸");
if(rects != null && rects.length >= 1) {
// 4 为每张识别到的人脸画一个圈
for (int i = 0; i < rects.length; i++) {
Imgproc.rectangle(image, new Point(rects[i].x, rects[i].y), new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), new Scalar(0, 255, 0));
Imgproc.putText(image, "admin", new Point(rects[i].x, rects[i].y), Imgproc.FONT_HERSHEY_SCRIPT_SIMPLEX, 1.0, new Scalar(0, 255, 0), 1, Imgproc.LINE_AA, false);
//Mat dst=image.clone();
//Imgproc.resize(image, image, new Size(300,300));
}
i++;
if(i==3) {// 获取匹配成功第10次的照片
Imgcodecs.imwrite("D:\\img\\" + "face.png", image);
}
}
return image;
}
}
}
像这样,出现下列错误:
编辑启动类:Edit Configuration VM options:
-Djava.library.path=D:\OpenCV\opencv\build\java\x64;
执行代码Demo: