Recognition Model Real Time-Facial Emotion Recogination using Deepface Analyzer

Adarsh Chaurasiya
3 min readMar 20, 2024

--

Facial emotion recognition using Python involves analyzing images or video frames to detect and classify human emotions based on facial expressions. One of the popular libraries for this task is DeepFace, which provides pre-trained deep learning models for facial analysis tasks, including emotion recognition. Here’s an introduction to using DeepFace for face emotion recognition in Python:

DeepFace Overview:

DeepFace is a Python library built on top of Keras and TensorFlow. It offers a range of facial analysis functionalities, including face detection, facial recognition, facial attribute analysis, and emotion recognition. The library utilizes deep learning models to perform these tasks efficiently.

Install:

pip install deepface

1-Import library:

import cv2
import matplotlib.pyplot as plt
from deepface import DeepFace

2- Load Image:

img=cv2.imread('happy.jpg')#load the image

3-Fetch the loaded image

plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))#get original color image

4-Analyse the face:

predict=DeepFace.analyze(img)#inbuilt library where it analyze it deeper

5-Now print Predict:

These are the prediction.

print("Emotion",predict[0]['dominant_emotion'])

Result:

Happy

REAL TIME EMOTION DETECTION:

1-open webcam

2-Analyse the face

3-Draw Rectangle

4-use font family to write the emotion side of of the webcam

Final Code:

import cv2
from deepface import DeepFace

#open webcam
facecascade=cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
cap=cv2.VideoCapture(1)
if not cap.isOpened():
cap=cv2.VideoCapture(0)
if not cap.isOpened():
raise IOError("Cannot open Webcam")
#read face
while True:
ret,frame=cap.read()
result=DeepFace.analyze(frame)#analyse face
#draw rectangle
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces=facecascade.detectMultiScale(gray,1.1,4)
for(x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
font=cv2.FONT_HERSHEY_SIMPLEX#font type
#write these things
cv2.putText(frame,result[0]['dominant_emotion'],(0,50),font,2,(0,255,0),3,cv2.LINE_4);

cv2.imshow('Demo video',frame)

if cv2.waitKey(2) & 0xFF==ord('q'):
break
cap.release()
cv2.destroyAllWindows()

Results:

Benifits of using deepface:

Using DeepFace for facial emotion recognition in Python offers several benefits:

  1. Pre-trained Models: DeepFace provides pre-trained deep learning models specifically designed for facial analysis tasks, including emotion recognition. These models are trained on large datasets, enabling them to accurately detect and classify emotions in faces.
  2. Ease of Use: DeepFace offers a simple and intuitive API for performing facial emotion recognition tasks. With just a few lines of code, you can analyze images or video frames and extract emotion information from faces.
  3. Efficiency: DeepFace is built on top of efficient deep learning frameworks like Keras and TensorFlow, allowing for fast and parallelized computations. This ensures that facial emotion recognition can be performed quickly, even on large datasets or real-time video streams.
  4. Customization: While DeepFace provides pre-trained models out of the box, it also allows for customization and fine-tuning of models to better suit specific use cases. You can adjust model parameters, train on additional data, or even train entirely new models from scratch if needed.
  5. Integration: DeepFace can be easily integrated into existing Python applications or workflows. Whether you’re building a mobile app, a web service, or a desktop application, you can incorporate facial emotion recognition functionality using DeepFace.
  6. Community Support: DeepFace benefits from an active community of developers and researchers who contribute to its development and provide support through forums, documentation, and tutorials. This means you can find assistance and resources to help you make the most of the library.
  7. Real-world Applications: Facial emotion recognition has numerous real-world applications, including sentiment analysis in market research, human-computer interaction, virtual reality, healthcare (e.g., mental health monitoring), and security (e.g., emotion-aware surveillance systems). DeepFace facilitates the implementation of such applications by providing robust emotion recognition capabilities.

Conclusion:

DeepFace simplifies facial emotion recognition tasks in Python by providing pre-trained models and a convenient interface. However, keep in mind that the accuracy of emotion recognition heavily depends on the quality of the input image, the diversity of the training data, and the performance of the underlying deep learning models. Experimentation and fine-tuning may be necessary to achieve optimal results for specific use cases.

Thank You😊👨‍💻

--

--

Adarsh Chaurasiya

Data Science enthusiast 👨‍💻|| 3rd year Student at G.L.B.I.T.M ||