"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "und9jTlIXcM3"
},
"source": [
"# **Summary** 📋\n",
"Deepface is a lightweight facial recognition and facial attribute analysis tool for Python."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-iPphL5BXleD"
},
"source": [
"## **Features**\n",
"----\n",
"- Compare two images to see if they have the face of the same person (even if there is more than one person in the 2nd image)\n",
"- Analyze a person's face to predict their emotion, age, gender, and race\n",
"- Compare an image against a collection of images to see which of them match the initial image"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "biiP0gRQXrwr"
},
"source": [
"## **Limitations**\n",
"____\n",
"- Memory intensive\n",
"- More advanced functionality requires Python scripting rather than simpler command-line commands\n",
"- **Important research note**: Comparing faces is a a task that leads to false positives. The face X can match face Y with X and Y being different people. So,it should only be used to verify on top of additional reasons to believe that X and Y belong to the same person, take it with a grain of salt and not apply the tool blindly. You should also use other tools to validate a positive result, such as [AWS Rekogintion](https://aws.amazon.com/getting-started/hands-on/detect-analyze-compare-faces-rekognition/)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AreDeY4SYFp2"
},
"source": [
"# **Installation** 💿\n",
"\n",
"You can install Deepface using `pip`, a package installer for Python\n",
"\n",
"Run the following code snippet to install deepface:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6ivU8abXaHmA"
},
"outputs": [],
"source": [
"!pip install deepface"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OWDwCxvAJaIX"
},
"source": [
"## **Uploading Images for Analysis** 🖼\n",
"\n",
"In order to use Deepface with Google Colab or Binder, you will need to upload the images to be compared. \n",
"See the instructions below for each of the two platforms.\n",
"\n",
"### Google Colab\n",
"----\n",
"1. Click the folder icon on the left side.\n",
"2. Click the file upload icon, which is a page with an upwards pointing arrow on it.\n",
"3. In the window that opens up, select the images you would like to upload.\n",
"\n",
"*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.\n",
"\n",
"\n",
"### Binder\n",
"----\n",
"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.\n",
"\n",
"The file can be accessed based on its path relative to the root of the repository.\n",
"\n",
"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`."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cxvekRnVYQL2"
},
"source": [
"# **Usage** 💻\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EvbfPpl-ax-h"
},
"source": [
"## **Face Verification**\n",
"____\n",
"\n",
"Using Deepface, a command to compare the faces in each of two image files will take the following format:\n",
"\n",
"`deepface verify -img1_path -img2_path ` where ``s are the image file names.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "u3riys2jNDpN"
},
"outputs": [],
"source": [
"!deepface verify -img1_path img1.jpg -img2_path img2.jpg"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hDwN1ZTNc2N1"
},
"source": [
"### Analyzing the Result\n",
"----\n",
"Your result will look something like this:\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "eX-E7kCze-lo"
},
"source": [
"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. "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vt-jeHtWazwJ"
},
"source": [
"## **Facial Analysis**\n",
"-----\n",
"`deepface analyze -img_path `"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "DbBNBzDPkXhK"
},
"outputs": [],
"source": [
"!deepface analyze -img_path img1.png"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ti8bghGrM0jb"
},
"source": [
"### Analyzing the Result\n",
"---\n",
"Your result will look something like this:"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fSnikkCml5t8"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Jdj7TvulwzuL"
},
"source": [
"Here are the following pieces of information you can expect to receive with their scales:\n",
"\n",
"- `Emotion` - the emotion with the highest numerical value is the most representative of the emotion of the person in the picture\n",
" - `angry`\n",
" - `disgust`\n",
" - `fear`\n",
" - `happy`\n",
" - `sad`\n",
" - `surpise`\n",
" - `neutral`\n",
" - `dominant emotion` - most visible emotion\n",
"- `region` - position and dimensions of face in image (in pixels)\n",
"- `age` - predicted age\n",
"- `gender` - predictions for whether the person in the image is a man or woman\n",
" - `Woman` - the probability (out of 100%) that the person in the image is a woman\n",
" - `Man` - the probability (out of 100%) that the person in the image is a main\n",
" - `dominant_gender` - the gender that has the highest percentage\n",
"- `race` - 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 100\n",
" - `asian`\n",
" - `indian`\n",
" - `black`\n",
" - `white`\n",
" - `middle eastern`\n",
" - `latino hispanic`\n",
" - `dominant_race` - the race with the highest percentage\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "m3ErkdJHJ5MI"
},
"source": [
"## **Large Scale Facial Recognition**\n",
"___\n",
"With 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.\n",
"\n",
"In order to accomplish this in a Notebook in Google Colab.\n",
"1. Connect your Notebook to Google Drive.\n",
"2. Upload your images to the folder that is created.\n",
"4. Upload your single image to the session storage.\n",
"5. Execute the relevant Deepface command."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yClaAzHpWGla"
},
"source": [
"\n",
"### **Step 1** - Connect your Notebook to Google Drive\n",
"----\n",
"1. Click on the button circled below to connect your Notebook to your Google Drive."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XJeLmNTjll-E"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "H-Rq1ckGl2Pq"
},
"source": [
"2. You will see the dialog below pop up. Click \"Connect to Google Drive\" and go through the subsequent steps, which will involve you logging into your Google account and authorizing Google Colab to access your Google Drive."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aPh5mFVcl5Sg"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fuSyD8wWWH_G"
},
"source": [
"### **Step 2** - Upload your images to a new folder\n",
"____\n",
"\n",
"1. After connecting your Google Drive account to Google Colab, a folder called \"drive\" will appear. Click the arrow next to \"drive\" and then next to the \"MyDrive\" folder that appears underneath.\n",
"\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TJTtaGREEg-s"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jHoW3MwhqdDN"
},
"source": [
"2. A dropdown will appear. In this drop down, click on the \"New Folder\" button circled below. We will create a new folder called \"deep_face_analysis,\" but you can call it whatever you want."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QoeLSYcVn0uy"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dye47uphHVGg"
},
"source": [
"3. Once this new folder has been created, click the same 3 dot icon on the right of the folder name to trigger the dropdown. From the dropdown, select the \"Upload\" option. From here, you can select photos to upload into your folder."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Blm3mZeAHNwP"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "09f-y97RWN4u"
},
"source": [
"### **Step 3** - Upload your single image to the session storage\n",
"-----\n",
"See the section \"Uploading Images for Analysis\" for instructions on how to do this."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hoemv-OIajIU"
},
"source": [
"### **Step 4** - Execute the relevant Deepface command\n",
"_____\n",
"\n",
"In order to use this functionality, you will need the path to both your image and folder of images to compare against.\n",
"\n",
"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:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6zb7eC0lVY_U"
},
"outputs": [],
"source": [
"from deepface import DeepFace\n",
"\n",
"image_to_find = \"/content/img-1.jpeg\"\n",
"folder_to_search = \"/content/drive/MyDrive/deep_face_analysis\"\n",
"\n",
"metric_model = \"euclidean_l2\"\n",
"\n",
"dfs = DeepFace.find(img_path = image_to_find,\n",
" db_path = folder_to_search,\n",
" distance_metric = metric_model\n",
")\n",
"\n",
"print(dfs)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "A7IaZQkxH98O"
},
"source": [
"### Analyzing the Output\n",
"-----\n",
"Your output will consist of two sets of data.\n",
"1. You will have a list of the files that were found to be a match, with their corresponding indices (0, 1, 2...). See the example below:"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5lsbHqG0JLwA"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gHhMkdfvJ6HV"
},
"source": [
"2. You will have a list of all of the matching files (denoted by their corresponding index) and the extent to which they match the target image (VGG-Face_euclidean_12). See the example below:"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ghpJabvAIGxb"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "M2akIAUxYSik"
},
"source": [
"# **FAQ** 📃"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "klN7cKj9eml8"
},
"source": [
"## Can I use other image formats besides JPEG?\n",
"---\n",
"Yes, you can also analyze PNG files using Deepface."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Hbhj43vnOrjT"
},
"source": [
"## Are there any alternatives to the methods for uploading photos mentioned above?\n",
"---\n",
"\n",
"Yes, you can run the following code to upload images locally (but not to Google Drive)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "cIp-LCtM2QKe"
},
"outputs": [],
"source": [
"from google.colab import files\n",
"uploaded = files.upload()\n",
"\n",
"# this part being optional, maybe just calling `files.upload()` is enough.\n",
"for fn in uploaded.keys():\n",
" print('User uploaded file \"{name}\" with length {length} bytes'.format(\n",
" name=fn, length=len(uploaded[fn])))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1_4bUy_4Qexj"
},
"source": [
"## How can you convert images from one format to another?\n",
"---\n",
"1. Install the Python library that will handle the conversion."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3qoSMKvLTdWA"
},
"outputs": [],
"source": [
"!pip install pillow"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uLcy7K30T3Yp"
},
"source": [
"2. Do the conversion.\n",
"See the comments in the Python code for more details on how to modify the code for your own needs.\n",
"\n",
"You can download [this image](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) and name it john-snow.jpg, you can execute the cell below to achieve it"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FS3xfkmrTjVV"
},
"outputs": [],
"source": [
"from PIL import Image\n",
"\n",
"# Load the image to colab (john-snow.jpg)\n",
"# File path, if uploaded to Colab, is simply the name of the file.\n",
"image = Image.open('john-snow.jpg')\n",
"\n",
"# Converting image from JPG to PNG format\n",
"# Change the file extension (.png) to indicate the format your original should be converted to.\n",
"image.save(\"john-snow.png\")\n",
"print(\"Image successfully converted!\")"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}