Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

you should start with one of the existing android samples for face detection, either using cascades or a detection dnn

if you got that running, time to move to the recognition. you can use the FaceRecognizer classes from opencv_contrib repo (but you would need to reebuild the sdk with contrib modules) or, alternatively use the pretrained OpenFace dnn model, with code like:

import org.opencv.core.*;
import org.opencv.dnn.*;
import org.opencv.imgcodecs.*;
import org.opencv.imgproc.*;

public class FaceRecognition {
    public static Mat process(Net net, Mat img) {
        Mat inputBlob = Dnn.blobFromImage(img, 1./255, 
             new Size(96,96), new Scalar(0,0,0), true, false);
        net.setInput(inputBlob);
        return net.forward().clone();
    }
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Net net = Dnn.readNetFromTorch("openface.nn4.small2.v1.t7");
        Mat feature1 = process(net, Imgcodecs.imread("Abdullah_Gul_0004.jpg"));
        Mat feature2 = process(net, Imgcodecs.imread("Abdullah_Gul_0007.jpg"));
        double dist  = Core.norm(feature1,  feature2);
        if (dist < 0.6)
            System.out.println("SAME !");
    }
}

you should start with one of the existing android samples for face detection, either using cascades or a detection dnn

if you got that running, time to move to the recognition. recognition.

you can use the FaceRecognizer classes from opencv_contrib repo (but you would need to reebuild the sdk with contrib modules)

or, alternatively use the pretrained OpenFace dnn model, with code like:

import org.opencv.core.*;
import org.opencv.dnn.*;
import org.opencv.imgcodecs.*;
import org.opencv.imgproc.*;

public class FaceRecognition {
    public static Mat process(Net net, Mat img) {
        Mat inputBlob = Dnn.blobFromImage(img, 1./255, 
             new Size(96,96), new Scalar(0,0,0), true, false);
        net.setInput(inputBlob);
        return net.forward().clone();
    }
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Net net = Dnn.readNetFromTorch("openface.nn4.small2.v1.t7");
        Mat feature1 = process(net, Imgcodecs.imread("Abdullah_Gul_0004.jpg"));
        Mat feature2 = process(net, Imgcodecs.imread("Abdullah_Gul_0007.jpg"));
        double dist  = Core.norm(feature1,  feature2);
        if (dist < 0.6)
            System.out.println("SAME !");
    }
}