配置环境
管网下载地址:https://opencv.org/releases/
ps:高版本的管网jar中只有基础方法,功能比较全的jar包可以通过管网源码编译生成或者网上下载
根据环境下载匹配的包,执行下载文件自动解压出环境文件,在path环境变量中添加路径:D:\workSoft\opencv\build\x64\vc14\bin
文件说明
环境中包含基础方法jar包及人脸模型文件
人脸采集
采集10张人脸图片,保存到指定目录,命名规则:path\name\img\name-count.jpg
网络摄像头可以通过直接访问或者流媒体推流地址进行访问,可以先用VLC测试视频是否正常
ps:识别的人脸图片可能受环境影响,有可能会将环境误识别为人脸
例如:下面的图片测试的时候会标识出了2个人脸标记
先将jar包引入到工程中
JFrame测试代码
public class FaceCollect {
//模型文件路径
public static String modelXml = "D:\\workSoft\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
public static String name = "";
//摄像头直接访问地址
public static String url = "rtsp://name:password@ip:554/Streaming/Channels/1";
//推流访问地址
// public static String url = "http://xxx/live?port=1935&app=myapp&stream=34020000002000000001_34020000001320000001_Play";
public static boolean flag = true;
//图片保存路径
public static String path = "C:\\Users\\yf\\Desktop\\faceTest\\";
public static int count = 0;
public static long startTime;
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("Please input person name: ");
Scanner scanner = new Scanner(System.in);
name = scanner.next();
System.out.println("collecting images for : " + name);
//新建窗口
JFrame cameraFrame = new JFrame("camera");
cameraFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
cameraFrame.setSize(1280, 720);
cameraFrame.setBounds(0, 0, cameraFrame.getWidth(), cameraFrame.getHeight());
VideoPanel videoPanel = new VideoPanel();
cameraFrame.setContentPane(videoPanel);
cameraFrame.setVisible(true);
CascadeClassifier faceCascade = new CascadeClassifier();
//获取模型文件
faceCascade.load(modelXml);
//调用摄像头
VideoCapture capture = new VideoCapture(url);
try {
// capture.open(0);
if (capture.isOpened()) {
Mat image = new Mat();
while(flag) {
capture.read(image);
if (!image.empty()) {
detectAndDisplay(image, faceCascade);
videoPanel.setImageWithMat(image);
cameraFrame.repaint();
} else {
break;
}
}
}
}catch (Exception e) {
System.out.println(e.getMessage());
}finally {
capture.release();
}
cameraFrame.repaint();
cameraFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
cameraFrame.dispose();
}
/**
* 框出人脸
* @param frame
* @param faceCascade
*/
public static void detectAndDisplay(Mat frame, CascadeClassifier faceCascade){
MatOfRect faces = new MatOfRect();
Mat grayFrame = new Mat();
//convert the frame in gray scale
Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
//equalize the frame histogram to improve the result
Imgproc.equalizeHist(grayFrame, grayFrame);
//compute minimum face size (20% of the frame height, in our case)
int absoluteFaceSize = 0;
int height = grayFrame.rows();
if (Math.round(height * 0.2f) > 0) {
absoluteFaceSize = Math.round(height * 0.2f);
}
//detect faces
faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, Objdetect.CASCADE_SCALE_IMAGE, new Size(absoluteFaceSize, absoluteFaceSize), new Size());
//each rectangle in faces is a face: draw them!
Rect[] facesArray = faces.toArray();
//保存人脸图片
if (facesArray.length==1) {
if ((count ==