Implementing Face Recognition Using Openface Using Python and Docker
Openface provides capabilities for face detection, facial landmark detection, face recognition, and tracking facial expressions. OpenFace is an open source that's built using deep learning techniques and is capable of processing images and videos to analyze faces in real time or from recorded media.
Prerequisites.
- A Linux or macOS operating system.
- Docker Manager has been installed.
Next, install a preconfigured Docker image that contains all the necessary software and dependencies.
docker pull bamos/openface
docker run -d --restart=always --name openface bamos/openface
root@tuturulianda:~# docker exec -it openface /bin/bash
Step 1: Perform System and Python Patch Update
To commence, execute the following commands to upgrade system files and patch the Python 2.7 library named label.py
.
root@7ba6f9ec1f7b:/# apt update && app upgrade -y
root@7ba6f9ec1f7b:/# apt install nano
root@7ba6f9ec1f7b:/# nano /root/.local/lib/python2.7/site-packages/sklearn/preprocessing/label.py
On line 166, modify the condition from if diff:
to if diff.size > 0:
Create a folder named ./training-images/
within the Openface folder.
root@7ba6f9ec1f7b:/# cd /root/openface
root@7ba6f9ec1f7b:~/openface# mkdir -p training-images
root@7ba6f9ec1f7b:~/openface# mkdir -p unknown-images
Step 2 - Create Image Folders
Subsequently, establish an image folder for each individual you intend to recognize. For instance:
root@7ba6f9ec1f7b:~/openface# mkdir -p ./training-images/tutu-rulianda/
root@7ba6f9ec1f7b:~/openface# mkdir -p ./training-images/chad-smith/
root@7ba6f9ec1f7b:~/openface# mkdir -p ./training-images/jimmy-fallon/
root@7ba6f9ec1f7b:~/openface# mkdir -p ./training-images/will-ferrell/
Step 3 - Populate Image Folders
Copy images of each individual from the unknown-images
folder on the VPS into the corresponding destination folders. Ensure that only one face is visible in each image. Crop the images around the face is unnecessary, as OpenFace will perform this automatically.
root@7ba6f9ec1f7b:~/openface# exit
root@tuturulianda:~# docker cp training-images openface:/root/openface
root@tuturulianda:~# docker cp unknown-images openface:/root/openface
root@tuturulianda:~# docker exec -it openface /bin/bash
root@7ba6f9ec1f7b:~# cd /root/openface
root@7ba6f9ec1f7b:~/openface#
Step 4 - Detect Face and Align Face
Execute the align-dlib.py
script from the root directory of the Openface project. This script performs pose detection and alignment.
root@7ba6f9ec1f7b:~/openface# ./util/align-dlib.py ./training-images/ align outerEyesAndNose ./aligned-images/ --size 96
This action will create a new folder named ./aligned-images/
, which will contain cropped and aligned versions of each of your test images. Subsequently, it will generate the representations from the aligned images using a script named main.lua
located within the batch-represent
folder.
root@7ba6f9ec1f7b:~/openface# ./batch-represent/main.lua -outDir ./generated-embeddings/ -data ./aligned-images/
Upon executing the provided script, the ./generated-embeddings/
folder will be created and contain a csv
file that stores the embeddings for each image. Subsequently, you can proceed to train your face detection model by executing the classifier.py
script located within the demos folder.
root@7ba6f9ec1f7b:~/openface# ./demos/classifier.py train ./generated-embeddings
This action will generate a new file named ./generated-embeddings/classifier.pkl
. This file contains the SVM model
that will be utilized for face recognition purposes. Consequently, you should now possess a functional face recognition system.
Step 5 - Recognize Face
Obtain a new image featuring an unidentified individual from the unknown-images
folder. Subsequently, transmit this image to the classifier script via the following command:
root@7ba6f9ec1f7b:~/openface# ./demos/classifier.py infer ./generated-embeddings/classifier.pkl your_test_image.jpg
You should obtain a prediction that bears a striking resemblance to the following:
root@7ba6f9ec1f7b:~/openface# ./demos/classifier.py --verbose infer ./generated-embeddings/classifier.pkl unknown-images/tutu-rulianda.jpg
Argument parsing and import libraries took 0.550723075867 seconds.
Loading the dlib and OpenFace models took 1.12552189827 seconds.
=== unknown-images/tutu-rulianda.jpg ===
+ Original size: (400, 400, 3)
Loading the image took 0.00343298912048 seconds.
Face detection took 0.12503695488 seconds.
Alignment took 0.00670099258423 seconds.
This bbox is centered at 251, 108
Neural network forward pass took 0.0909860134125 seconds.
Prediction took 0.000488996505737 seconds.
Predict tutu-rulianda with 0.82 confidence.
root@7ba6f9ec1f7b:~/openface#
If you obtain unsatisfactory results, consider incorporating additional images of each individual in Step 3, particularly those depicting diverse poses.
Conclusion
The Openface application has been successfully launched.
Citations.
Adam Geitgey
https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78
Brandon Amos, Bartosz Ludwiczuk, Mahadev Satyanarayanan
https://github.com/cmusatyalab/openface.git
@techreport{amos2016openface,
title={OpenFace: A general-purpose face recognition
library with mobile applications},
author={Amos, Brandon and Bartosz Ludwiczuk and Satyanarayanan, Mahadev},
year={2016},
institution={CMU-CS-16-118, CMU School of Computer Science},
}
B. Amos, B. Ludwiczuk, M. Satyanarayanan,
"Openface: A general-purpose face recognition library with mobile applications,"
CMU-CS-16-118, CMU School of Computer Science, Tech. Rep., 2016.