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.

Leave a Reply