Face Recognition with TV Series Dark

Barış Ayten
5 min readJul 25, 2020

Hello everyone ✌,

I am currently doing my summer internship and working on a face recognition project during this period. I do research to be able to do this project and I want to share what I learned so far during this process.

In this article, I’m going to explain how we can perform simple face recognition. To make it a little fun, I will make a small program that recognizes the cast of the TV series Dark, which has been popular in recent years. So let's start…

giphy.com

First, you need to make sure that you have installed the 3 libraries required:

  • OpenCV
  • NumPy
  • face_recognition

If you’re looking for ways to install these libraries, I suggest this video. It was a real struggle to work on the project before coming across this video, it became a very tedious process. I’d like to thank and credit to the owner of this video.

If you installed the libraries and everything is ready to go, we can continue now.

First of all, we need to identify the faces we will introduce to the program. I randomly picked 4 people from the cast, you can see them in the picture below.

(all images are taken from IMDB.com)

Now we can move on to the code section. Let’s introduce the related libraries and the pictures we will use for the program.

(Line 6-12) We state that we will use our camera with “VideoCapture”. Then, with the “load_image_file()” method, we enter the path where we have pictures.

(Line 14) Later on, we need to encode these pictures that we introduced. “face_endcodings” turns the loaded images into 128-dimension face encoding for each face in the image. In short, it returns us as a list of encoded faces.

Next, let’s store these encoded pictures in an array. Also, we need to keep their names. I have defined some variables, we will use them in the recognizing process.

Very good, we’re almost halfway through. Let’s see what we’ve done so far.

  • We first told Python which libraries we would use.
  • Then, with the help of OpenCV, we accessed the camera of our computer
  • We uploaded the pictures we have in the program and encoded them.
  • We have defined the required variables and arrays.

Now let’s move on to the recognizing section.

With the read() method of OpenCV, we need to capture every frame that our camera takes. We resize each frame with the help of the resize() method so that the recognizing process is easy and fast. Then, we need to convert each frame from BGR color to RGB color. Because face_recognition wants like that(:

And then we go into the if loop. The loop here is controlled by “process_this_frame”. With the help of the “face_locations” method, it finds the faces in rgb_small_frame and returns an array of bounding boxes of human faces in an image. This array consists of a list of tuples of found face locations in (top, right, bottom, left) order.

Then with the help of the face_encodings, we need to encode the faces in rgb_small_frame. We will compare these faces with the faces that we introduced to the system later.

After we create an empty array named face_names, we will use it to store the names of the faces we define later.

(Line 18) Then we go on a journey with the for loop in face_encodings. Our aim here is to compare the faces that we have already introduced with the faces that we have registered in face_encodings. Here the compare_faces method is running to our help. We give known_face_encodings and face_encoding as parameters. It will compare these two parameters and return us an array that contains True or False.

(Line 22) Here we create a variable named name. Our goal is, if we find a matching face, we will assign the name of that face to this variable. If not, it will remain Unknown.

(Line 25) If you remember, the method of compare_faces returned an array containing True/False to us. We have stored this array in the matches variable. Now it’s time to use matches array. In here, We need the index of the True ones here because it matched and returned true.

We need to check whether there is True in matches. And if there is, we save the index of this True in the first_match_index. Then, using this index, we reach the corresponding name in known_face_names and assign it to the name variable.

And that’s it, we did the recognition and got the corresponding name.

Now, all we have to do is print this name under the face corresponding to the screen.

(Line 2) The purpose of the for loop is to match each face in face_locations with its own name and display it on the screen in a rectangle with the rectangle() method of OpenCV.

(Line 18) Again, we show our video to the screen with the imshow() method of OpenCV. And we indicate that the program should be closed when the “q” key is pressed.

Yes, that’s all. 🎉

Now I want to show examples that I have applied. Unfortunately, the image quality is a bit bad because I use the camera of my own laptop. But it works :)

Jonas :)
actually this photo is from another movie but it still works :)

Thank you for taking the time to read it. I hope it has been helpful. If I have made mistakes, please feel free to mention them in the comments or send a mail, I’m always open to learning 😍

Here is the full code:

--

--