Skip to content

gv22ga/dlib-face-recognition-android

Repository files navigation

dlib-face-recognition-android

Recognize faces in android using dlib state-of-the-art face recognition model based on deep learning. The model has an accuracy of 99.13% on the LFW benchmark.

App

Face recognition is very accurate but it takes approximately 6 seconds on a Redmi4 with Snapdragon 435 and Android 7.1.

Usage

git clone --recursive https://github.com/gv22ga/dlib-face-recognition-android.git

Now you can directly open the dlib-face-recognition-app in android studio

Build JNI (Optional)

First get the required libraries

./envsetup

To build native code and copy to android studio's project manually

cd [dlib-face-recognition-android]
ndk-build -j 2
cp -r libs/* dlib-face-recognition-app/dlib/src/main/jniLibs

Please see dlib-android for more details

Dlib 19.9

This project uses dlib 19.9. Due to some c++11 issues, I had problem compiling dlib 19.9 with opencv. So I made some small changes in dlib/dlib/serialize.h, dlib/dlib/dnn/layers.h, dlib/dlib/geometry/rectangle.h, dlib/dlib/image_transforms/interpolation.h, dlib/dlib/dnn/loss.h, dlib/dlib/statistics/running_gradient.h and dlib/dlib/global_optimization/global_function_search.cpp files in dlib library.

Sample code

// recognize person
FaceRec mFaceRec = new FaceRec(Constants.getDLibDirectoryPath());
List<VisionDetRet> results = mFaceRec.recognize(image_bitmap);
for(VisionDetRet n:results) {
    Log.d(TAG, n.getLabel()); // prints the name of recognized person
}

// add person
// add the person image to dlib_rec_example/images directory with name `[PersonName].jpg`
mFaceRec.train()

Image Quality

You can change the parameters INPUT_SIZE in MainActivity.java and MAX_IMAGE_SIZE in AddPerson.java if recognition is not working accurately.

Todos

This app currently uses HOG based face detector. This detector fails to detect small faces and is not very accurate. Instead we can use CNN based face detector, which is very accurate but will take much more time.

License

MIT

Thanks

Many thanks to tzutalin for dlib-android