Facial expression detection using Deepface module in Python
Last Updated :
03 Jan, 2023
In this article, we are going to detect the facial expression of an already existing image using OpenCV, Deepface, and matplotlib modules in python.
Module Needed
- OpenCV: OpenCV is an open-source library in python which is used for computer vision, machine learning, and image processing.
- Matplotlib: Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.
- Deepface: Deepface was built by an artificial intelligence researchers group at Facebook. It is a framework in python for facial recognition and attributes analysis. Deepface's core library components are used in Keras and TensorFlow.
pip install deepface
This is the most basic expression detection technique and there are several ways in which we can detect facial expression.
Stepwise Implementation
Step 1: Importing the required module.
Python3
#import the required modules
import cv2
import matplotlib.pyplot as plt
from deepface import DeepFace
Step 2: Copy the path of the picture of which expression detection is to be done, read the image using "imread()" method in cv2 providing the path within the bracket. imread() reads the image from the file and stores it in an array. Then use imshow() method of matplotlib. imshow() method converts data into image. Now plot the image using show method in order to ensure that the image has been correctly imported.
Python3
# read image
img = cv2.imread('img1.jpg')
# call imshow() using plt object
plt.imshow(img[:, :, : : -1])
# display that image
plt.show()
Output:
Output image
Step 3: Create a result variable that will store the result. Use Deepface analyze() method, Deepface analyze() method contains strong facial attribute analysis features such as age, gender, facial expressions. Facial expressions include anger, fear, neutral, sad, disgust, happy, and surprise. Print the result. The result shows the facial expressions percentage of the person.
Python3
# storing the result
result = DeepFace.analyze(img,
actions = ['emotion'])
# print result
print(result)
Output:
The result shows that person is 96% happy.
Below is the complete implementation:
Python3
# import the required modules
import cv2
import matplotlib.pyplot as plt
from deepface import DeepFace
# read image
img = cv2.imread('img.jpg')
# call imshow() using plt object
plt.imshow(img[:,:,::-1])
# display that image
plt.show()
# storing the result
result = DeepFace.analyze(img,actions=['emotion'])
# print result
print(result)
Output:
Similar Reads
Car driving using hand detection in Python In this project, we are going to demonstrate how one can drive a car by just detecting hand gestures on the steering wheel. Let's say the requirement is something like this - If driver wants to start the car then put both of your hands on the steering wheel. If someone having no hands on a steering
3 min read
Age Detection using Deep Learning in OpenCV The task of age prediction might sound simple at first but it's quite challenging in real-world applications. While predicting age is typically seen as a regression problem this approach faces many uncertainties like camera quality, brightness, climate condition, background, etc. In this article we'
5 min read
Pedestrian Detection using OpenCV-Python OpenCV is an open-source library, which is aimed at real-time computer vision. This library is developed by Intel and is cross-platform - it can support Python, C++, Java, etc. Computer Vision is a cutting edge field of Computer Science that aims to enable computers to understand what is being seen
3 min read
Python - Face detection and sending notification Nowadays python has become one of the most popular languages as well as favorite programming language among developers. The simplified syntax and pattern of this language make the presence of this language in the trending list. The biggest strength of Python is a huge collection of standard library
4 min read
Right and Left Hand Detection Using Python In this article, we are going to see how to Detect Hands using Python. We will use mediapipe and OpenCV libraries in python to detect the Right Hand and Left Hand. We will be using the Hands model from mediapipe solutions to detect hands, it is a palm detection model that operates on the full image
5 min read