This tutorial was written with Python 3.6, however the library used is compatible with versions 3.3 and above.
Face recognition software is awesome. The fact that we are able to write software that accurately picks out where someone’s eyes and nose reside within an image still astounds me and the fact that there are libraries out there for this sort of things is awesome. These libraries help to lower the barrier to entry for beginners looking to write their own face recognition systems and allow people to do some really cool things.
ageitgey/face_recognition is one such library and at the time of writing this it features well over 7,000
stars on github.
Setting up
In order to get started with the face_recognition
library you will first have to install it, this can be done with a simple pip
install command like so:
|
|
A Simple Example
Let’s take a stock image with a number of people in it. If we wanted to automatically find all of the faces in an image, we could easily do that in 4 lines of code. This code will first take in an image and then will compute the locations of all faces using face_recognition.face_locations(image)
. After we will simply print out the number of faces that were found.
|
|
If I were to run this against the image above I would get the following output:
|
|
This is an example of face detection and you could potentially sync this up with something like a security camera and perform real-time analysis using this detection algorithm to see if someone has just walked into your house for example.
Identifying Faces
A more complex example would be identifying the exact coordinates of each of the faces found and translating those coordinates into separate images.
Say we wanted to take the example above a step further and store the faces that we’ve detected in our new security software. This can be done like so using the face_recognition
library:
|
|
Running this would give the following output and it would open the 5 temporary image files.
|
|
Pretty cool, huh? Again you could potentially run this across a series a video stream and capture all of the faces that appear within that video.
If you weren’t interested in concepts such as face recognition which we’ll be covering below, you could potentially start doing cool things such as sentiment analysis and try to guage how happy or sad people are within your videos.
This could be useful if you were trying to get real-time feedback on how well an attraction is doing at a museum or an amusement park!
Checking if A Person Exists Within an Image
Recognizing that an image contains multiple faces is pretty cool but we can actually take this one step further and determine who exists within an image. Say for instance you have a photo of a group of people, you could determine using the face_recognition
library whether your friend Alan
exists within that photo.
In order for this to work however you need at least one reference image of the people you are trying to identify.
|
|
Large scale face recognition systems tend to build up big databases of people and their faces. As more faces get added to a database, checking to see who exists within a photo or series of photos becomes more expensive.
This means we couldn’t take a photo of everyone in the world and expect our software to be able to tell us in real-time who exists within any given photo.
Conclusion
This was a fairly simple introduction to the art of facial recognition software and hopefully you found it both useful and interesting. In this tutorial we managed to cover both face detection and face recognition.
If you require further assistance or wish to chat then please leave a comment in the comments section below or tweet me: @Elliot_f.