发布时间:2023-09-04 11:30
一、下载,https://opencv.org/releases/
二、安装
三、配置环境变量
1、新建OPENCV_HOME
2、添加path
三、新建springboot项目,jdk选择8
1、pom添加依赖
org
opencv
4.5.1
2、Edit Configuration,添加 -Djava.library.path=D:\\Dev\\opencv\\opencv\\build\\java\\x64
3、代码,新建FaceDetectTest测试类
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class FaceDetectTest {
@Test
void contextLoads() {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// Create a face detector from the cascade file in the resources
//分类器文档地址
CascadeClassifier faceDetector = new CascadeClassifier(\"D:\\\\Dev\\\\opencv\\\\opencv\\\\sources\\\\data\\\\lbpcascades\\\\lbpcascade_frontalface.xml\");
if(faceDetector.empty()){
System.out.println(\"读取配置文件失败\");
return;
}
//识别的文档地址
Mat image = Imgcodecs.imread(\"D:\\\\face.png\");
// 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()) {
Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
//识别后文档地址
String filename = \"D:\\\\faceDetection.png\";
System.out.println(String.format(\"Writing %s\", filename));
Imgcodecs.imwrite(filename, image);
}
}
四、结果对比
【云原生之Docker实战】使用Docker部署反向代理 Nginx Proxy Manager
小米回应“被意大利机构罚款2176万元”;Win 12要来?曝微软调整Windows迭代节奏;Vite 3.0 发布|极客头条
将 Terraform 生态粘合到 Kubernetes 世界
2022年windows的Visual Studio 安装后初始配置
AE插件:Red Giant Trapcode Suite红巨星粒子插件
《MATLAB智能算法30个案例》:第3章 基于遗传算法的BP神经网络优化算法