Building a Face Recognizer in Python | by Behic Guven | Towards Data Science

In this post, I will show you how to build your own face recognizer using Python. Building a program that detects and recognizes faces is a very interesting and fun project to get started with computer vision. In previous posts, I showed how to recognize text and also how to detect faces in an image, these are great projects to practice python in computer vision. Today, we will do something a little more advance and that is face recognition.
As can be understood from the name, we will write a program that will recognize faces in an image. When I say “program”, you can understand this as teaching a machine what to do and how to do it. I like to use teaching instead of programming because that’s actually what we will be doing. The best way of learning is teaching, so while teaching a machine how to detect faces, we are learning too. Before we start working on the project, I want to share the difference between face detection and face recognizer. This is something good to know.
Face Detection vs Face Recognition
Getting Started
Libraries
Training the Images
Face Recognition
Testing the Recognizer
These two things might sound very similar but actually, they are not the same. Let’s understand the difference so that we don’t miss the point.
Face Detection is the process of detecting faces, from an image or a video that doesn’t matter. The program doesn’t do anything more than finding the faces. But on the other hand, face recognition, the program that finds the faces and also it can tell which face belongs to who. So it is more informational than just detecting them.
To write a code that recognizes faces needs some training data, we should train our machine so that it knows the faces and who are they. In this project, we are the ones teaching our program. In machine learning, there are two types of learning; supervised and unsupervised. I will not go into details, in this project we are going to use supervised learning. Here is a nice post about machine learning methods.
We will use two main modules for this project, and they are called Face Recognition and OpenCV. OpenCV is a highly optimized library with a focus on real-time applications.
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code.
Source: https://opencv.org
towardsdatascience.com
We have to install some libraries so that our program works. Here is a list of the libraries we will install: cmake, face_recognition, numpy, opencv-python. Cmake is a prerequisite library so that face recognition library installation doesn’t give us an errors.
We can install them in one line using PIP library manager:
pip install cmake face_recognition numpy opencv-python
After the installation is completed, let’s import them into our code editor. Some of these libraries are included in Python that’s why we can import them without installing them.
import face_recognitionimport cv2import numpy as npimport osimport glob
Great! Now we move to the next step, where we will import images and use them to train our program.
First things first, let’s find our images.
I’ve downloaded images of some famous people and added them to a new folder called “faces”. Also to get the current directory, in other words, the location of your program, we can use an os method called “getcwd()”.
faces_encodings = []faces_names = []cur_direc = os.getcwd()path = os.path.join(cur_direc, 'data/faces/')list_of_files = [f for f in glob.glob(path+'*.jpg')]number_files = len(list_of_files)names = list_of_files.copy()
Understanding the lines above:
All the images are in one folder named “faces”.
Image file names have to be the name of the person in the image. (Such as: bill-gates.jpg).
File names are listed and assigned to “names” variable.
File types have to be the same. In this exercise, I used “jpg” format.
Let’s move to the next step.
for i in range(number_files):    globals()['image_{}'.format(i)] = face_recognition.load_image_file(list_of_files[i])    globals()['image_encoding_{}'.format(i)] = face_recognition.face_encodings(globals()['image_{}'.format(i)])[0]    faces_encodings.append(globals()['image_encoding_{}'.format(i)])# Create array of known names    names[i] = names[i].replace(cur_direc, "")      faces_names.append(names[i])
To give you some idea, here is how my ‘names’ list looks like.
Great! The images are trained. In the following step, we will use the device’s webcam to see how our code performs.
We have long lines of code in this step. If you go through it you can easily understand what is happening in each line. Let’s define the variables that will be needed.
face_locations = []face_encodings = []face_names = []process_this_frame = True
Here comes the face recognition code. (You may need to reformat the spacing if you copy the following code, I recommend writing it from scratch by looking at the code, and also try to understand)
video_capture = cv2.VideoCapture(0)while True:    ret, frame = video_capture.read()    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)    rgb_small_frame = small_frame[:, :, ::-1]    if process_this_frame:        face_locations = face_recognition.face_locations( rgb_small_frame)        face_encodings = face_recognition.face_encodings( rgb_small_frame, face_locations)        face_names = []        for face_encoding in face_encodings:            matches = face_recognition.compare_faces (faces_encodings, face_encoding)            name = "Unknown"            face_distances = face_recognition.face_distance( faces_encodings, face_encoding)            best_match_index = np.argmin(face_distances)            if matches[best_match_index]:                name = faces_names[best_match_index]            face_names.append(name)process_this_frame = not process_this_frame# Display the results    for (top, right, bottom, left), name in zip(face_locations, face_names):        top *= 4        right *= 4        bottom *= 4        left *= 4# Draw a rectangle around the face        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)# Input text label with a name below the face        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)        font = cv2.FONT_HERSHEY_DUPLEX        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)# Display the resulting image    cv2.imshow('Video', frame)# Hit 'q' on the keyboard to quit!    if cv2.waitKey(1) & 0xFF == ord('q'):        break
In the first picture, I am using the same exact image that was used in the training data.
Now, I will try it with a different image of Taylor Swift. It works perfectly!
Congrats!! You have created a program that detects and also recognizes faces in an image. Now, you have an idea of how to use computer vision in a real project. Hoping that you enjoyed reading this step-by-step guide. I would be glad if you learned something new today. Working on hands-on programming projects like this one is the best way to sharpen your coding skills.
Feel free to contact me if you have any questions while implementing the code.
I am Behic Guven, and I love sharing stories on programming, education, and life. Subscribe to my content to stay inspired and Towards Data Science to stay inspired. Thank you,