package face_demo; import static org.bytedeco.javacpp.opencv_highgui.cvWaitKey; import org.bytedeco.javacpp.opencv_imgcodecs; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.nio.IntBuffer; import org.bytedeco.javacpp.opencv_core; import org.bytedeco.javacpp.opencv_core.*; import org.bytedeco.javacpp.opencv_face; public class face_demo { public static void main(String[] args) throws IOException { Mat testImage = opencv_imgcodecs.imread("d:\\data\\subject02.happy.png",opencv_imgcodecs.CV_LOAD_IMAGE_GRAYSCALE); File root = new File("d:\\data\\faces"); 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,opencv_core.CV_32SC1); IntBuffer labelsBuf = labels.getIntBuffer(); int counter = 0; for (File image : imageFiles) { Mat img = opencv_imgcodecs.imread(image.getAbsolutePath(), opencv_imgcodecs.CV_LOAD_IMAGE_GRAYSCALE); int label = Integer.parseInt(image.getName().split("\\-")[0]); images.put(counter, img); labelsBuf.put(counter, label); counter++; } opencv_face.FaceRecognizer faceRecognizer = opencv_face.createFisherFaceRecognizer(); // FaceRecognizer faceRecognizer = createEigenFaceRecognizer(); // FaceRecognizer faceRecognizer = createLBPHFaceRecognizer() faceRecognizer.train(images, labels); int predictedLabel = faceRecognizer.predict(testImage); System.out.println("Predicted label: " + predictedLabel); org.bytedeco.javacpp.opencv_highgui.imshow("query", testImage); org.bytedeco.javacpp.opencv_highgui.imshow("found", images.get(predictedLabel-1)); cvWaitKey(0); } }