-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
OpenCVFaceRecognizer.java
88 lines (70 loc) · 3.07 KB
/
OpenCVFaceRecognizer.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
import java.io.File;
import java.io.FilenameFilter;
import java.nio.IntBuffer;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.IntPointer;
import org.bytedeco.javacpp.DoublePointer;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_face.*;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_face.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;
/**
* I couldn't find any tutorial on how to perform face recognition using OpenCV and Java,
* so I decided to share a viable solution here. The solution is very inefficient in its
* current form as the training model is built at each run, however it shows what's needed
* to make it work.
*
* The class below takes two arguments: The path to the directory containing the training
* faces and the path to the image you want to classify. Not that all images has to be of
* the same size and that the faces already has to be cropped out of their original images
* (Take a look here http://fivedots.coe.psu.ac.th/~ad/jg/nui07/index.html if you haven't
* done the face detection yet).
*
* For the simplicity of this post, the class also requires that the training images have
* filename format: <label>-rest_of_filename.png. For example:
*
* 1-jon_doe_1.png
* 1-jon_doe_2.png
* 2-jane_doe_1.png
* 2-jane_doe_2.png
* ...and so on.
*
* Source: http://pcbje.com/2012/12/doing-face-recognition-with-javacv/
*
* @author Petter Christian Bjelland
*/
public class OpenCVFaceRecognizer {
public static void main(String[] args) {
String trainingDir = args[0];
Mat testImage = imread(args[1], IMREAD_GRAYSCALE);
File root = new File(trainingDir);
FilenameFilter imgFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
name = name.toLowerCase();
return name.endsWith(".jpg") || name.endsWith(".pgm") || name.endsWith(".png");
}
};
File[] imageFiles = root.listFiles(imgFilter);
MatVector images = new MatVector(imageFiles.length);
Mat labels = new Mat(imageFiles.length, 1, CV_32SC1);
IntBuffer labelsBuf = labels.createBuffer();
int counter = 0;
for (File image : imageFiles) {
Mat img = imread(image.getAbsolutePath(), IMREAD_GRAYSCALE);
int label = Integer.parseInt(image.getName().split("\\-")[0]);
images.put(counter, img);
labelsBuf.put(counter, label);
counter++;
}
FaceRecognizer faceRecognizer = FisherFaceRecognizer.create();
// FaceRecognizer faceRecognizer = EigenFaceRecognizer.create();
// FaceRecognizer faceRecognizer = LBPHFaceRecognizer.create();
faceRecognizer.train(images, labels);
IntPointer label = new IntPointer(1);
DoublePointer confidence = new DoublePointer(1);
faceRecognizer.predict(testImage, label, confidence);
int predictedLabel = label.get(0);
System.out.println("Predicted label: " + predictedLabel);
}
}