Facial Expression recognition with DeepFace
Facial expressions can be detected using DeepFace and RetinaFace.
The first cell is only run on google colab and installs the ammico package.
After that, we can import ammico and read in the files given a folder path.
[1]:
# if running on google colab
# flake8-noqa-cell
import os
if "google.colab" in str(get_ipython()):
# update python version
# install setuptools
# %pip install setuptools==61 -qqq
# install ammico
%pip install git+https://github.com/ssciwr/ammico.git -qqq
# mount google drive for data and API key
from google.colab import drive
drive.mount("/content/drive")
[2]:
import ammico
from ammico import utils as mutils
from ammico import display as mdisplay
We select a subset of image files to try facial expression detection on, see the limit keyword. The find_files function finds image files within a given directory:
[3]:
# Here you need to provide the path to your google drive folder
# or local folder containing the images
images = mutils.find_files(
path="data/",
limit=10,
)
We need to initialize the main dictionary that contains all information for the images and is updated through each subsequent analysis:
[4]:
mydict = mutils.initialize_dict(images)
To check the analysis, you can inspect the analyzed elements here. Loading the results takes a moment, so please be patient. If you are sure of what you are doing, you can skip this and directly export a csv file in the step below. Here, we display the face recognition results provided by the DeepFace and RetinaFace libraries. Click on the tabs to see the results in the right sidebar. You may need to increment the port number if you are already running several notebook instances on the same
server.
[5]:
analysis_explorer = mdisplay.AnalysisExplorer(mydict, identify="faces")
analysis_explorer.run_server(port = 8050)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[5], line 1
----> 1 analysis_explorer = mdisplay.AnalysisExplorer(mydict, identify="faces")
2 analysis_explorer.run_server(port = 8050)
TypeError: __init__() got an unexpected keyword argument 'identify'
Instead of inspecting each of the images, you can also directly carry out the analysis and export the result into a csv. This may take a while depending on how many images you have loaded.
[6]:
for key in mydict.keys():
mydict[key] = ammico.faces.EmotionDetector(mydict[key]).analyse_image()
Downloading data from 'https://github.com/serengil/deepface_models/releases/download/v1.0/retinaface.h5' to file '/home/runner/.cache/pooch/3be32af6e4183fa0156bc33bda371147-retinaface.h5'.
Downloading data from 'https://github.com/chandrikadeb7/Face-Mask-Detection/raw/v1.0.0/mask_detector.model' to file '/home/runner/.cache/pooch/865b4b1e20f798935b70082440d5fb21-mask_detector.model'.
1/1 [==============================] - 1s 641ms/step
Downloading data from 'https://github.com/serengil/deepface_models/releases/download/v1.0/age_model_weights.h5' to file '/home/runner/.cache/pooch/39859d3331cd91ac06154cc306e1acc8-age_model_weights.h5'.
Downloading data from 'https://github.com/serengil/deepface_models/releases/download/v1.0/facial_expression_model_weights.h5' to file '/home/runner/.cache/pooch/dd5d5d6d8f5cecdc0fa6cb34d4d82d16-facial_expression_model_weights.h5'.
Downloading data from 'https://github.com/serengil/deepface_models/releases/download/v1.0/gender_model_weights.h5' to file '/home/runner/.cache/pooch/2e0d8fb96c5ee966ade0f3f2360f6478-gender_model_weights.h5'.
Downloading data from 'https://github.com/serengil/deepface_models/releases/download/v1.0/race_model_single_batch.h5' to file '/home/runner/.cache/pooch/382cb5446128012fa5305ddb9d608751-race_model_single_batch.h5'.
1/1 [==============================] - 0s 320ms/step
1/1 [==============================] - 0s 315ms/step
1/1 [==============================] - 1s 595ms/step
1/1 [==============================] - 0s 189ms/step
1/1 [==============================] - 0s 191ms/step
1/1 [==============================] - 1s 604ms/step
1/1 [==============================] - 0s 201ms/step
1/1 [==============================] - 0s 196ms/step
1/1 [==============================] - 0s 324ms/step
1/1 [==============================] - 0s 80ms/step
These steps are required to convert the dictionary of dictionarys into a dictionary with lists, that can be converted into a pandas dataframe and exported to a csv file.
[7]:
outdict = mutils.append_data_to_dict(mydict)
df = mutils.dump_df(outdict)
Check the dataframe:
[8]:
df.head(10)
[8]:
| filename | face | multiple_faces | no_faces | wears_mask | age | gender | race | emotion | emotion (category) | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | data/106349S_por.png | Yes | No | 1 | [Yes] | [24] | [Man] | [None] | [None] | [None] |
| 1 | data/102141_2_eng.png | Yes | No | 1 | [Yes] | [25] | [Man] | [None] | [None] | [None] |
| 2 | data/102730_eng.png | Yes | No | 1 | [No] | [27] | [Man] | [asian] | [sad] | [Negative] |
Write the csv file - here you should provide a file path and file name for the csv file to be written.
[9]:
df.to_csv("data_out.csv")
[ ]: