forked from bytedeco/javacv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FaceRecognizerInVideo.java
114 lines (94 loc) · 4.45 KB
/
FaceRecognizerInVideo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.io.File;
import org.bytedeco.javacpp.IntPointer;
import org.bytedeco.javacpp.DoublePointer;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.OpenCVFrameGrabber;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_face.*;
import org.bytedeco.opencv.opencv_highgui.*;
import org.bytedeco.opencv.opencv_imgproc.*;
import org.bytedeco.opencv.opencv_objdetect.*;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_face.*;
import static org.bytedeco.opencv.global.opencv_highgui.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_objdetect.*;
/**
* This is an example how to detect face in a video file with javacv
* @author Vincent He ([email protected])
*
*/
public class FaceRecognizerInVideo {
public static void main(String[] args) throws Exception {
OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();
if (args.length < 2) {
System.out.println("Two parameters are required to run this program, first parameter is the analized video and second parameter is the trained result for fisher faces.");
}
String videoFileName = args[0];
String trainedResult = args[1];
CascadeClassifier face_cascade = new CascadeClassifier(
"data\\haarcascade_frontalface_default.xml");
FaceRecognizer lbphFaceRecognizer = LBPHFaceRecognizer.create();
lbphFaceRecognizer.read(trainedResult);
File f = new File(videoFileName);
OpenCVFrameGrabber grabber = null;
try {
grabber = OpenCVFrameGrabber.createDefault(f);
grabber.start();
} catch (Exception e) {
System.err.println("Failed start the grabber.");
}
Frame videoFrame = null;
Mat videoMat = new Mat();
while (true) {
videoFrame = grabber.grab();
videoMat = converterToMat.convert(videoFrame);
Mat videoMatGray = new Mat();
// Convert the current frame to grayscale:
cvtColor(videoMat, videoMatGray, COLOR_BGRA2GRAY);
equalizeHist(videoMatGray, videoMatGray);
Point p = new Point();
RectVector faces = new RectVector();
// Find the faces in the frame:
face_cascade.detectMultiScale(videoMatGray, faces);
// At this point you have the position of the faces in
// faces. Now we'll get the faces, make a prediction and
// annotate it in the video. Cool or what?
for (int i = 0; i < faces.size(); i++) {
Rect face_i = faces.get(i);
Mat face = new Mat(videoMatGray, face_i);
// If fisher face recognizer is used, the face need to be
// resized.
// resize(face, face_resized, new Size(im_width, im_height),
// 1.0, 1.0, INTER_CUBIC);
// Now perform the prediction, see how easy that is:
IntPointer label = new IntPointer(1);
DoublePointer confidence = new DoublePointer(1);
lbphFaceRecognizer.predict(face, label, confidence);
int prediction = label.get(0);
// And finally write all we've found out to the original image!
// First of all draw a green rectangle around the detected face:
rectangle(videoMat, face_i, new Scalar(0, 255, 0, 1));
// Create the text we will annotate the box with:
String box_text = "Prediction = " + prediction;
// Calculate the position for annotated text (make sure we don't
// put illegal values in there):
int pos_x = Math.max(face_i.tl().x() - 10, 0);
int pos_y = Math.max(face_i.tl().y() - 10, 0);
// And now put it into the image:
putText(videoMat, box_text, new Point(pos_x, pos_y),
FONT_HERSHEY_PLAIN, 1.0, new Scalar(0, 255, 0, 2.0));
}
// Show the result:
imshow("face_recognizer", videoMat);
char key = (char) waitKey(20);
// Exit this loop on escape:
if (key == 27) {
destroyAllWindows();
break;
}
}
}
}