deepFace + Python: Create a Look-alike Algorithm

What will we cover in this tutorial?

We will explore the deepFace library, which includes the state of the art face recognition algorithm. deepFace is a Python library as we like it you can do complicated stuff with only a few lines of code.

In this tutorial we will use the deepFace library to create a look-alike algorithm. That is, we will calculate which movie star you look most like. Your movie-star look-alike.

Step 1: Collect your library of movie stars you want to compare yourself to

Well, this requires you use pictures that are available of movie stars online on the internet.

My library consists of the following images.

My library of images

Just to clarify, the deepFace is not part of the library. I wonder if it can detect a face on it?

Let’s try that (the below code is not part of the official tutorial). If you need help to install the deepFace library read this tutorial.

from deepface import DeepFace

result = DeepFace.analyze("deepFaceLogo.png", actions=['age', 'gender', 'race', 'emotion'])
print(result)

Where we have the deepFaceLogo.png to be the following image.

ValueError: Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.

Bummer. The deepFace library cannot detect a face in it’s logo.

Well, back on track. Find a collection of movie stars and save them in a folder.

Step 2: Understand how easy deepFace is to use

A modern face recognition pipeline consists of 4 common stages: detectalignrepresent and verifyDeepFacehandles all these common stages in the background.

https://pypi.org/project/deepface/

A normal process would first identify where in the picture a face is located (the detect stage). This is needed to remove all unnecessary background in the image and only focus on the face. Then it would proceed to align it, so the eyes in the head are in a horizontal line (the align stage). That is, the head is not tilting to one side. This makes the face recognition algorithm work better as all faces will have the same alignment. Further, we will change the representation of the image (the aligned face part) in the model. Finally, verify if the distance is close. That is, the smaller distance from the image, the more we are certain it is the right person.

Let’s take a simple example. Let’s compare if I am Angelina Jolie. (If you need help to install the deepFace library read this tutorial).

from deepface import DeepFace

result = DeepFace.verify("rune.jpg", "pics-db/Angelina Jolie.jpg", model_name="VGG-Face")
print(result)

I have added the picture of me (rune.jpg) in the folder where I run my Python program and a pics-db with a picture of Angelina Jolie.

And the result looks like this.

{
  'verified': False,
  'distance': 0.7834609150886536,
  'max_threshold_to_verify': 0.4,
  'model': 'VGG-Face',
  'similarity_metric': 'cosine'
}

The algorithm has determined that I am not Angelina Jolie (‘verified’: False). Now to the interesting part, it has a distance (‘distance’: 0.7834609150886536), which we will use to determine which movie star you look the most like. Here it is important to understand, that the lower the distance, the more you look-alike. Also, you see that the maximum threshold to verify is 0.4. That is the distance should be less than 0.4 to conclude that it is the same person in the picture.

Step 3: Making our Look-alike algorithm

We have collected a library of images and located them in pics-db. Then I try with a picture of me (the same one I used in last step). (If you need help to install the deepFace library read this tutorial).

from deepface import DeepFace
import glob

pic = "rune.jpeg"
files = glob.glob("pics-db/*")

model = "VGG-Face"
results = []
for file in files:
    result = DeepFace.verify(pic, file, model_name=model)
    results.append((file, result['distance']))

results.sort(key=lambda x: x[1])
print("Model:", model)
for file, distance in results:
    print(file, distance)

Now I am a bit nervous, which one do I look the most like?

Model: VGG-Face
pics-db/Tom Cruise.jpg 0.4702601432800293
pics-db/The Rock.jpg 0.493824303150177
pics-db/Robert Downey Jr.jpg 0.4991753101348877
pics-db/Daniel Craig.jpg 0.5135003626346588
pics-db/Christian Bale.jpg 0.5176380276679993
pics-db/Brad Pitt.jpg 0.5225759446620941
pics-db/Will Smith.jpg 0.5245362818241119
pics-db/Michael Douglas.png 0.5407796204090118
pics-db/Keanu Reeves.jpg 0.5416552424430847
pics-db/Angelina Jolie.jpg 0.7834609150886536

Wow. I like this immediately. Tom Cruise, The Rock, Robert Downey Jr. Luckily, I look the least like Angelina Jolie, which is not that surprising (At least I would think so).

Are we done?

Maybe, it depends. I guess you can make an App or Web-service with a movie star look-alike algorithm.

You can also play around with different models. The deepFace library contains the following: “VGG-Face”, “Facenet”, “OpenFace”, “DeepFace”, “DeepID”, and “Dlib”.

The results are not the same.

Model: Facenet
pics-db/Tom Cruise.jpg 0.7826492786407471
pics-db/Brad Pitt.jpg 0.870269775390625
pics-db/The Rock.jpg 0.8774074390530586
pics-db/Keanu Reeves.jpg 0.9102083444595337
pics-db/Daniel Craig.jpg 0.914682649075985
pics-db/Christian Bale.jpg 0.9467008262872696
pics-db/Robert Downey Jr.jpg 1.0119212558493018
pics-db/Angelina Jolie.jpg 1.0243268758058548
pics-db/Michael Douglas.png 1.067187249660492
pics-db/Will Smith.jpg 1.2313244044780731

The Facenet model says I look less like Will Smith and more like Angelina Jolie. Not sure I trust this one.

If you enjoy this I recommend you read the following tutorial by the author of deepface Sefik Ilkin Serengil.

How to Get Started with DeepFace using PyCharm

What will we cover in this tutorial?

In this tutorial we will show you how to setup your virtual environment in PyCharm to use Deepface. Then run a small program from DeepFace.

This tutorial has been done for both Mac and Windows. See the additional notes for Windows at the end if you experience problems.

Step 1: Importing the DeepFace library and run our first program

You know how it works.

from deepface import DeepFace


demography = DeepFace.analyze("angelina.jpg", actions=['age', 'gender', 'race', 'emotion'])
print("Age: ", demography["age"])
print("Gender: ", demography["gender"])
print("Emotion: ", demography["dominant_emotion"])
print("Race: ", demography["dominant_race"])

You see that deepface is not available and click to install it.

It seems to work. But then…

Okay. Let’s do it the easy way. Just add a import dlib in your code.

But it fails.

Step 2: Install CMake to make it work

If you search the internet you will see you need also to install CMake to make DeepFace work.

So let’s import that as well.

We end up with this code.

from deepface import DeepFace
import cmake
import dlib


demography = DeepFace.analyze("angelina.jpg", actions=['age', 'gender', 'race', 'emotion'])
print("Age: ", demography["age"])
print("Gender: ", demography["gender"])
print("Emotion: ", demography["dominant_emotion"])
print("Race: ", demography["dominant_race"])

Where you first install cmake by putting your mouse on top of the red line under cmake and choose install. Then you do the same for dlib.

And by magic it works.

Step 3: Run our little program

You need to get the picture of Angelina (angelina.jpg). Apparently, the authors of this DeepFace have a thing for her and use her as an example.

Angelina

Then when you run the program you will get the following output, as it needs to download a lot of stuff.

Actions to do:  ['age', 'gender', 'race', 'emotion']
facial_expression_model_weights.h5 will be downloaded...
Downloading...
From: https://drive.google.com/uc?id=13iUHHP3SlNg53qSuQZDdHDSDNdBP9nwy
To: /Users/admin/.deepface/weights/facial_expression_model_weights.zip
5.54MB [00:00, 10.4MB/s]
age_model_weights.h5 will be downloaded...
Downloading...
From: https://drive.google.com/uc?id=1YCox_4kJ-BYeXq27uUbasu--yz28zUMV
To: /Users/admin/.deepface/weights/age_model_weights.h5
539MB [01:04, 8.36MB/s]
Downloading...
From: https://drive.google.com/uc?id=1wUXRVlbsni2FN9-jkS_f4UTUrm1bRLyk
To: /Users/admin/.deepface/weights/gender_model_weights.h5
79.7MB [00:08, 10.1MB/s]gender_model_weights.h5 will be downloaded...
537MB [00:58, 9.16MB/s]
Downloading...
From: https://drive.google.com/uc?id=1nz-WDhghGQBC4biwShQ9kYjvQMpO6smj
To: /Users/admin/.deepface/weights/race_model_single_batch.zip
78.3MB [00:08, 9.37MB/s]race_model_single_batch.h5 will be downloaded...
511MB [00:54, 9.35MB/s]
Analyzing:   0%|          | 0/1 [00:00<?, ?it/s]
Finding actions:   0%|          | 0/4 [00:00<?, ?it/s]
Action: age:   0%|          | 0/4 [00:00<?, ?it/s]    
Action: age:  25%|██▌       | 1/4 [00:01<00:05,  1.79s/it]
Action: gender:  25%|██▌       | 1/4 [00:01<00:05,  1.79s/it]
Action: gender:  50%|█████     | 2/4 [00:02<00:03,  1.54s/it]
Action: race:  50%|█████     | 2/4 [00:02<00:03,  1.54s/it]  
Action: race:  75%|███████▌  | 3/4 [00:03<00:01,  1.23s/it]
Action: emotion:  75%|███████▌  | 3/4 [00:03<00:01,  1.23s/it]
Action: emotion: 100%|██████████| 4/4 [00:03<00:00,  1.13it/s]
Analyzing:   0%|          | 0/1 [00:03<?, ?it/s]

Age:  33.10586443589396
Gender:  Woman
Emotion:  neutral
Race:  white

Hmm… Apparently she is 33.1 years old on that picture. Angelina is also a woman and has neutral emotions. Finally, she is white.

Let’s try to see what it says about me.

Me

I am happy to see this.

Age:  27.25606073226045
Gender:  Man
Emotion:  happy
Race:  white

I am still in my 20ies. How can you not love that. DeepFace is considered to be top of the art, so let’s not doubt that.

Additional notes for Windows

To ensure that it also worked on Windows, I tried it out and ran into a few challenges there.

First of all, I was using a 32-bit version of Python, which was causing tensorflow not to be installed our found a matching library. Hence, make sure to install Python 64-bit version on Windows. You can have both versions running in parallel. The easiest way to get PyCharm to use your 64-bit version is to create a new project and make the default Python using the 64-bit version.

Secondly, it was still having troubles. It was not having a new enough version of C/C++ compiler. In this case I needed to update my Visual Studio.

Then the above tutorial just ran like a charm on PyCharm.