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'])

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.

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")

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.

Learn Python


  • 70 pages to get you started on your journey to master Python.
  • How to install your setup with Anaconda.
  • Written description and introduction to all concepts.
  • Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

  1. How to get started with this 8 hours Python 101: A CRASH COURSE.
  2. Best practices for learning Python.
  3. How to download the material to follow along and create projects.
  4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

Expert Data Science Blueprint

Expert Data Science Blueprint

  • Master the Data Science Workflow for actionable data insights.
  • How to download the material to follow along and create projects.
  • A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

Machine Learning

Machine Learning – The Simple Path to Mastery

  • How to get started with Machine Learning.
  • How to download the material to follow along and make the projects.
  • One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

Leave a Comment