Ask Your Question
1

Face recognition app for android (OpenCV)

asked 2020-01-26 05:03:45 -0600

MohammadBMaraqa gravatar image

updated 2020-01-26 13:18:02 -0600

Hi all, I want to ask if anyone know how to use FaceRecognition with opencv on android using java. I am trying to build a simple application that detects and recognise faces, if anyone know any good and tried tutorials please helpp!!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-01-29 02:58:05 -0600

berak gravatar image

updated 2020-01-29 02:58:57 -0600

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 !");
    }
}
edit flag offensive delete link more

Comments

Hi berak, thank you for your help but I could not find the FaceRecognizer class in the android module. should i download a specific version or its not accessible for android?

MohammadBMaraqa gravatar imageMohammadBMaraqa ( 2020-01-29 03:44:40 -0600 )edit

again, to use the FaceRecognizer classes, you have to download the opencv_contrib repo, and rebuild the sdk (which is somewhat cumbersome, and why i'd recommend using the OpenFace dnn model)

berak gravatar imageberak ( 2020-01-29 03:50:07 -0600 )edit

How should i download it? sorry for asking too much but i have been searching for a week and every library i use it doesnt work with my project

MohammadBMaraqa gravatar imageMohammadBMaraqa ( 2020-01-29 03:51:58 -0600 )edit

Thank you so much i will check them!

MohammadBMaraqa gravatar imageMohammadBMaraqa ( 2020-01-29 04:04:56 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2020-01-26 05:03:45 -0600

Seen: 3,323 times

Last updated: Jan 29 '20