Step by Step Facial Recognition in Python

A simple how-to using Python, Pillow, and a few lines of code

Ulku Guneysu
Jun 4 · 7 min read
Source: attn

In this article, I will guide you to create your own face recognition in images. For this purpose, I will use the Python face recognition library and Pillow, the Python Imaging Library (PIL).

I chose to use Visual Studio Code since I need to use integrated terminal. First, I start by setting a virtual environment and install pipenv on my terminal.

Run pipenv shell to start your virtual environment and install the face recognition library.

For this tutorial, I created two folders named known and unknown. The first folder includes pictures of some of the more well-known people in politics like Barack Obama, Donald Trump, Bernie Sanders, Joe Biden, and Elizabeth Warren. The latter includes different pictures of the people from the first folder, some of the 2020 Presidential candidates, and some SNL characters (played by different actors) of Donald Trump, Barack Obama, and Bernie Sanders.

I will run a match on the known and unknown folders to see if there are any pictures of known people in the unknown folder. I can do this by command line easily by running:

This will go through all the images and show us the matches in the second folder from the first one.

As you can see from the output, Bernie_SNL.jpg—which was performed by Larry Davidis matched as Bernie Sanders. To avoid that, I will check the distance of each match, which essentially tells how much of a match the images are, by running:

face_recognition — show-distance true ./img/known ./img/unknown

I can see the decimal value of distance between matched images.

I will add the flag and change the tolerance so the matching algorithm will only accept the values under a certain number. Adjusting tolerance helps get more accurate results.

As seen in the above image, Bernie_SNL.jpg did not match with the real Bernie Sanders.jpg.

If I just want to get the names of the people in the images, I will use:

face_recognition — show-distance true ./img/known ./img/unknown | cut -d ‘,’ -f2

to get the output below.

Let’s move one of the unknown people, Andrew Yang, to our known folder and run the code above again. As you see below, Andrew Yang will also be defined as a known person and it will show the matches from the unknown folder.

If we want this process to go faster we can add — cpus flag to our command line.

Now I will create the Python files to work with the facial recognition library.


1. findfaces.py

Stock Photo

I will create a new python file on my root. I will have a folder named group in my img folder and have two images: team1 and team2. The first image includes five people and the other includes nine people. In this section, I will recognize the people, get their locations as numpy array, and get the number of people in the images.

The face_locations method returns list of tuples of found face locations in css (in top, right, bottom, left order). The code above will print out a numpy array of coordinates for each image. We have five people in theteam1 image so we will have five items which are top, right, bottom, left values.


2. facematch.py

In this section, I will repeat what I did in the command line in python and compare faces to see if they are match with built-in method compare_faces from the face recognition library. This built-in method compares a list of face encodings against a candidate encoding to see if they match.

Compare_faces takes the parameters below:

  • known_face_encodings — A list of known face encodings.
  • face_encoding_to_check — A single face encoding to compare against the list.
  • tolerance — How much distance we allow between faces to consider it a match. Lower is more strict. 0.6 is typical best performance.

3. pullfaces.py

In this section, I will show how to pull faces from an image and save it in a local folder. We need to import the Image module from the Pillow library. Image module provides a class with the same name which is used to represent a PIL image.

First, I will load the image and get the locations as a numpy array. Then, I will iterate through the locations with a for loop and save the image location in top, right, bottom, left order.

face_image is stored in the form of a numpy array. We can use the Pillow library to get the actual image using fromarrayand pass the numpy array. Image.fromarray creates an image memory from an object exporting the array interface and returns an image object.

This will show us each of the faces in the image as separate images as below. We can use pil_image.save(f’{top}.jpg’). We can name the images as we like. I used {top}.jpg since it is a for loop.

Faces of the people from team1

4. identify.py:

I aim to identify the people in the image and put a box around their faces with their names on it. In order to do this, first I need to identify the people that I would like my code to recognize. For this example, I will download Image and ImageDraw from the Pillow library since I will be drawing on the images.

I will start with identifying Barack Obama and Donald Trump, so I will first load_image_file as a numpy array. Then I will use face_encodings to return a list of 128-dimensional face encodings (one for each face in the image). We will only need the first item, so we set the index to [0]. Now, we have face encodings for both people. Next thing we want to do is create separate lists of encodings and names.

Next, I will load the test image, find all the faces in the test image, and do face encodings by passing test image and face locations.

Since our image is stored as a numpy array, we convert it using Image.fromarray to a pillow image so we can have image object.

In order to be able to draw on an image, we use ImageDraw and Draw from Pillow library to create an instance and pass the pillow image.

Now we will loop through the faces in the test image. First, I pass the positions as top, right, bottom, left and face_encodings so each iteration will have access to each face coordinates and their encoding. With compare_faces, I will check if the known_faces_encodings match with any face_encoding in my for loop.

By default, I set the name for all the faces to “Unknown Person”. If there is a match, the resulting image name will take the name that matches from the known_face_names array. Draw the first box around the face. I used black rgb-color, so I set my outline as (0,0,0). Then, I used draw.rectangle to draw the rectangle that I would put the label on. I added the label with draw.text in white (255,255,255,255) and pass ‘name’.

We will use del draw since we are required to delete the draw instance from memory. To save the image, we can simply use pil_image.save().


Try it for Yourself

In this tutorial, I gave you an introduction to facial recognition with Python. To get started yourself, visit the source links below.

Sources

Better Programming

Advice for programmers.

Ulku Guneysu

Written by

Electrical and Electronics Engineer interested in Deep Learning, Machine Learning, and AI

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade