-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
KazemiFacemarkExample.java
72 lines (59 loc) · 2.52 KB
/
KazemiFacemarkExample.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
/**
* Kazemi Facemark example for JavaCV
*
* @author Théophile Gonos
*
* Link to Kazemi model :
* https://raw.githubusercontent.com/opencv/opencv_3rdparty/contrib_face_alignment_20170818/face_landmark_model.dat
*/
import java.io.IOException;
import java.net.URISyntaxException;
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_imgcodecs.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_objdetect.*;
public class KazemiFacemarkExample {
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
// Load Face Detector
CascadeClassifier faceDetector = new CascadeClassifier ("haarcascade_frontalface_alt2.xml");
// Create an instance of Facemark
FacemarkKazemi facemark = FacemarkKazemi.create();
// Load landmark detector
facemark.loadModel("face_landmark_model.dat");
// Load image
Mat img = imread("face.jpg");
// convert to grayscale and equalize histograe for better detection
Mat gray = new Mat ();
cvtColor(img, gray, COLOR_BGR2GRAY);
equalizeHist( gray, gray );
// Find faces on the image
RectVector faces = new RectVector ();
faceDetector.detectMultiScale(gray, faces);
System.out.println ("Faces detected: "+faces.size());
// Variable for landmarks.
// Landmarks for one face is a vector of points
// There can be more than one face in the image.
Point2fVectorVector landmarks = new Point2fVectorVector();
// Run landmark detector
boolean success = facemark.fit(img, faces, landmarks);
if(success) {
// If successful, render the landmarks on each face
for (long i = 0; i < landmarks.size(); i++) {
Point2fVector v = landmarks.get(i);
drawFacemarks(img, v, Scalar.YELLOW);
}
}
// Display results
imshow("Kazemi Facial Landmark", img);
cvWaitKey(0);
// Save results
imwrite ("kazemi_landmarks.jpg", img);
}
}