opencv官方自带了人脸识别的模型,这里测试一下效果1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23/**
* 人脸识别测试
* @param args
*/
public static void main(String[] args) {
CascadeClassifier classifier = new CascadeClassifier("haarcascade_frontalface_alt.xml");
Mat image = Imgcodecs.imread("images/chuanpu.png");
MatOfRect faces = new MatOfRect();
classifier.detectMultiScale(image, faces);
Rect[] rects = faces.toArray();
System.out.println("识别人脸数:" + rects.length);
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, 0, 255), 1);
Imgproc.putText(image, "Human", new Point(rects[i].x, rects[i].y), Imgproc.FONT_HERSHEY_PLAIN, 1.0,
new Scalar(0, 255, 0), 1, Imgproc.LINE_AA, false);
}
imwrite("output/face2.png", image);
}