Deepface is a lightweight facial recognition and facial attribute analysis tool for Python.
You can install Deepface using pip
, a package installer for Python
Run the following code snippet to install deepface:
!pip install deepface
In order to use Deepface with Google Colab or Binder, you will need to upload the images to be compared. See the instructions below for each of the two platforms.
*Note: These images will only be available for the current runtime. If you refresh your page or exit the Jupyter Notebook, the files will be lost and you will have to upload them again.
The easiest way to add images that can be used for analysis is to add them to your Github repository, which, in any case, is necessary for working with a Jupyter Notebook in Binder.
The file can be accessed based on its path relative to the root of the repository.
For example, if you have an image called img1.jpg
stored in the imgs
folder located at the root of the repository, in the following commands, you could access it at imgs/img1.jpg
.
Using Deepface, a command to compare the faces in each of two image files will take the following format:
deepface verify -img1_path <file_path> -img2_path <file_path>
where <file_path>
s are the image file names.
!deepface verify -img1_path img1.jpg -img2_path img2.jpg
If you see verified: True
, that means that you have a match. This means that the distance
calculated via the similarity_metric
between the result of the model
is below the default threshold
for a match to be considered true.
!deepface analyze -img_path img1.png
Here are the following pieces of information you can expect to receive with their scales:
Emotion
- the emotion with the highest numerical value is the most representative of the emotion of the person in the pictureangry
disgust
fear
happy
sad
surpise
neutral
dominant emotion
- most visible emotionregion
- position and dimensions of face in image (in pixels)age
- predicted agegender
- predictions for whether the person in the image is a man or womanWoman
- the probability (out of 100%) that the person in the image is a womanMan
- the probability (out of 100%) that the person in the image is a maindominant_gender
- the gender that has the highest percentagerace
- You will see a percentage of how much the picture matches each of the below races. All of the percentages together will add up to 100asian
indian
black
white
middle eastern
latino hispanic
dominant_race
- the race with the highest percentageWith Deepface, we can compare all the images in a folder against a single image with a face, and find out which images in the folder have a face that matches that single image.
In order to accomplish this in a Notebook in Google Colab.
Move your cursor "MyDrive" and then click on the 3 dots that appear at the far right end of the shaded area, circled in the image below.
See the section "Uploading Images for Analysis" for instructions on how to do this.
In order to use this functionality, you will need the path to both your image and folder of images to compare against.
Unfortunately, this cannot be done purely via a command-line command and requires executing a Python script. See the example (and modify the file paths in image_to_find
and folder_to_search
and names like MyDrive/deep_face_analysis
) below:
from deepface import DeepFace
image_to_find = "/content/img-1.jpeg"
folder_to_search = "/content/drive/MyDrive/deep_face_analysis"
metric_model = "euclidean_l2"
dfs = DeepFace.find(img_path = image_to_find,
db_path = folder_to_search,
distance_metric = metric_model
)
print(dfs)
Your output will consist of two sets of data.
Yes, you can run the following code to upload images locally (but not to Google Drive).
from google.colab import files
uploaded = files.upload()
# this part being optional, maybe just calling `files.upload()` is enough.
for fn in uploaded.keys():
print('User uploaded file "{name}" with length {length} bytes'.format(
name=fn, length=len(uploaded[fn])))
!pip install pillow
See the comments in the Python code for more details on how to modify the code for your own needs.
You can download this image and name it john-snow.jpg, you can execute the cell below to achieve it
!wget https://sm.ign.com/ign_pt/news/g/game-of-th/game-of-thrones-jon-snow-spin-off-will-see-the-character-dea_htsy.jpg -O john-snow.jpg
from PIL import Image
# Load the image to colab (john-snow.jpg)
# File path, if uploaded to Colab, is simply the name of the file.
image = Image.open('john-snow.jpg')
# Converting image from JPG to PNG format
# Change the file extension (.png) to indicate the format your original should be converted to.
image.save("john-snow.png")
print("Image successfully converted!")