Last updated on June 28th, 2021 at 11:07 pm
This blog is in continuation of the blog How to Install dlib library in Ubuntu. Today I will show you a simple demo of how powerful the dlib library is and how you can run face recognition on your Ubuntu machine without too much of a coding.
I will be using Adam Geitgey’s famous face_recognition library for the purposes of face_recognition.
Install face_recognition library
To install face_recognition library, we will make use of pip3 again.
Open the terminal app and switch to the virtual workspace ml-py3 that we have created in the last blog of ours.
Now, install face_recognition by typing the following command
pip3 install face_recognition
After successful installation, you should have
face_detection commands tools available from terminal. Type
face_recognition --help in terminal to confirm. It should display help info as shown below:
Usage: face_recognition [OPTIONS] KNOWN_PEOPLE_FOLDER IMAGE_TO_CHECK
--cpus INTEGER number of CPU cores to use in parallel (can speed
up processing lots of images). -1 means "use all in
--tolerance FLOAT Tolerance for face comparisons. Default is 0.6.
Lower this if you get multiple matches for the same
--show-distance BOOLEAN Output face distance. Useful for tweaking tolerance
--help Show this message and exit.
How to use use face_recognition tool
Now, we have successfully installed face_recognition library and we can use pre-built tools for matching faces. Let’s create a project directory
face_demo In which we will store all our images for the matching algorithm. Then, create two folders
unknown_faces in face_demo directory and copy few images of known people into known_faces directory. To test our tool, we’ll also need sample data labeled as unknown_faces in unknown directory. You can download the resources from here. Then, run face_recognition program as shown below:
mkdir -p face_demo/known_faces
mkdir -p face_demo/unknown_faces
face_recognition ./known_faces ./unknown_faces --show-distance true --cpus 4
You should see the output of matched picture path.
How to use the face_recognition library in python project
You can also use the face_recognition library in your python project. It’s very simple and straightforward. Following code is taken straight from the Geitgey’s Github page.
picture_of_me = face_recognition.load_image_file("./pictures/modi.jpeg")
my_face_encoding = face_recognition.face_encodings(picture_of_me)
# my_face_encoding now contains a universal 'encoding' of my facial features that can be compared to any other picture of a face!
unknown_picture = face_recognition.load_image_file("unknown/unknown.jpeg")
unknown_face_encoding = face_recognition.face_encodings(unknown_picture)
# Now we can see the two face encodings are of the same person with `compare_faces`!
results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding)
if results == True:
print("It's a picture of Modi!")
print("It's not a picture of Modi!")
In the above code, first you will need to import the face_recognition library into your project. Then, load files into program using
load_image_file function. Once the images are loaded, you need to call
face_encodings function to extract all the 128 encodings. Finally, we need to compare the encodings using
compare_faces function. This way, in very few lines of code, you can have a working program that can compare known faces with the unknown one.
Note: If you have Nvidia graphics card installed like Zotac GeForce GTX 1050 Ti OC Edition. You can greatly increase your face_recognition library performance by installing CUDA and cuDNN.
This library is a good point to start exploring solutions where you need basic type of face matching or face detection. As the library is open source you could also look into the source code customize as you may see fit. Hope you liked this post. Leave a comment in case of any query. I will be happy to help. Keep learning. Cheers!