Search in sources :

Example 1 with FaceRecognizer

use of org.bytedeco.javacpp.opencv_face.FaceRecognizer in project javacv by bytedeco.

the class OpenCVFaceRecognizer method main.

public static void main(String[] args) {
    String trainingDir = args[0];
    Mat testImage = imread(args[1], CV_LOAD_IMAGE_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(), CV_LOAD_IMAGE_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);
}
Also used : Mat(org.bytedeco.javacpp.opencv_core.Mat) FilenameFilter(java.io.FilenameFilter) IntBuffer(java.nio.IntBuffer) IntPointer(org.bytedeco.javacpp.IntPointer) DoublePointer(org.bytedeco.javacpp.DoublePointer) FaceRecognizer(org.bytedeco.javacpp.opencv_face.FaceRecognizer) EigenFaceRecognizer(org.bytedeco.javacpp.opencv_face.EigenFaceRecognizer) FisherFaceRecognizer(org.bytedeco.javacpp.opencv_face.FisherFaceRecognizer) LBPHFaceRecognizer(org.bytedeco.javacpp.opencv_face.LBPHFaceRecognizer) MatVector(org.bytedeco.javacpp.opencv_core.MatVector) File(java.io.File)

Aggregations

File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 IntBuffer (java.nio.IntBuffer)1 DoublePointer (org.bytedeco.javacpp.DoublePointer)1 IntPointer (org.bytedeco.javacpp.IntPointer)1 Mat (org.bytedeco.javacpp.opencv_core.Mat)1 MatVector (org.bytedeco.javacpp.opencv_core.MatVector)1 EigenFaceRecognizer (org.bytedeco.javacpp.opencv_face.EigenFaceRecognizer)1 FaceRecognizer (org.bytedeco.javacpp.opencv_face.FaceRecognizer)1 FisherFaceRecognizer (org.bytedeco.javacpp.opencv_face.FisherFaceRecognizer)1 LBPHFaceRecognizer (org.bytedeco.javacpp.opencv_face.LBPHFaceRecognizer)1