The world’s simplest facial recognition API for Python and the command line.
Everyone is a value carrier with information that can be utilized to enhance your business and add an extra layer of security.
Facial recognition software analyzes video streams and matches the results against previously stored images and information in your database, it can also analyze recorded footage for forensic use.
The nature of the information and its use is strictly up to you as an invaluable asset, also it can add quality or security to your daily operations.
Systems with facial recognition are very effective and have the added benefit of spotting tailgating. Facial recognition offers valuable benefits for casinos, banks, stadiums, corporate buildings, airports, and high-end retail stores to name a few.
In this article, we are going to see how to use the face recognition library in python.
So first face recognition is a python library that builds on top of many libraries like Dlib library which is a modern C++ toolkit containing machine learning algorithms and tools for creating complex software in C++ and other machine learning algorithms.
Working with face recognition library
In my case, I prefer using Pycharm for creating python codes, if you don’t know what is Pycharm or how to install it. well, watch this video:
Install Pycharm on Windows:
Install Pycharm on Linux:
Now you need to install the face recognition library on Pycharm, this is the documentation of this tool on Github here and the PyPI documentation here.
If you don’t know how to install a python library on Pycharm, watch this video:
Enough talking, let’s jump to the code.
Find faces in pictures
Before we start coding you can download all the code and pictures here.
First, create a python file on Pycharm called Face_Recognition or whatever you want, I found this name suitable.
The first program that we are going to create a code that gives you all the faces that appear in a picture.
The first line in code as shown in the image above imports the face recognition library.
import face recognition
Then after that, we create a variable called image and set that variable to the library face_recognition and there is a method called load_image_file so here we are going to pass the image that we want to find all the faces within it.
image = face_recognition.load_image_file("path_of_image")
Now after that, we create another variable called face_locations and set that variable to the library face_recognition and there is a method called face_locations that will find all the faces in that image.
Don’t forget to pass to it the image variable that we have created earlier.
face_locations = face_recognition.face_locations(image)
So it knows what is the image that you wanna find faces within it, and this method will return an array of coordinates for each face and we can print that out if you want.
print(face_locations)
As you see at the image above, the code has return four parentheses every one refer to one person and it has four values (top, right, bottom, left)
Count how many peoples within an image
The code above is the same code that we have discussed earlier except just add to it this following line:
print(“There are {} faces in the image”.format(len(face_locations)))
So we count how many people by counting the length of the variable face_locations, and you can see that the python code has print:
There are 4 people in the image
Recognize who is in the picture
First, we import the face recognition library, then we create a variable called image_of_obama and we set that variable to face_recognition and then we load the image of Obama.
import face_recognition
image_of_obama = face_recognition.load_image_file("path")
In the next line, we need to extract the landmarks or the face encoding so then we can compare it with the other faces.
obama_face_encoding = face_recognition.face_encodings(image_of_obama)[0]
The face encoding algorithm will return an array of numbers that represent the landmarks of the face.
Don’t forget to pass it the image_of_obama variable to the face_encodings algorithm.
Next, we repeat the process with another image so we create a variable called unknown_image and we load the image in this variable, then we create another variable called unknown_face_encoding and we also find the landmarks of that unknown image.
unknown_image = face_recognition.load_image_file(path)
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
Now, we need to compare the faces and find if there is a match or not at a particular image.
Go ahead and create a variable called result and set it to this function face_recognition.compre_faces and pass in the first argument the obama_face_encoding in curly brackets and the second argument pass the unknown_face_encoding variable this method will return true or false.
We can print that out if we want and as you can see the result shows True because it found a match in the two images and I have feed it two images of Obama’s faces.
result = face_recognition.compare_faces([obama_face_encoding], unknown_face_encoding)
print(result[0])
Now change the image in unknown_image with other person and see what you will get, as you can see in the image below with a picture of Donald Trump it returns False
Pull the faces within an image
The first thing to do here is we import the dependencies which are:
from PIL import Image
import face_recognition
As usual, we load the image and we find the faces with the face_locations method.
image = face_recognition.load_image_file(path)
face_locations = face_recognition.face_locations(image)
Then we loop through the faces with this code:
for face_location in face_locations
Then we determine the faces locations with :
top, right, bottom, left = face_location
After that, we pull the faces with:
pil_image = image[top:bottom, left:right]
Now, we need to convert the images to a format that the PIL library can recognize it with the following code:
face_image = Image.fromarray(face_image)
Then we need to show the images with the following code:
pil_image.show()
You can save the images to your computer by typing the following code:
pil_image.save(“{}.jpg”.format(top))
So we name every person picture with the top value as you see in the image below.
That was a brief introduction to the face recognition library in python you can check out the documentation here for more details.
Credit: BecomingHuman By: Abdelhakim Ouafi