Coders Packet

Real-Time Facial Emotion Detection using Python

By Vinit Kumar Mahato

Python libraries can analyze emotions in real-time by detecting facial expressions and displaying corresponding emoji emotions.

Step 1: Importing Libraries

import cv2
from deepface import DeepFace

Step 2: Loading Pre-trained Models and emojis

faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

emojis = {
    'angry': cv2.imread('angry.png', cv2.IMREAD_UNCHANGED),
    'happy': cv2.imread('happy.png', cv2.IMREAD_UNCHANGED),
    'surprise': cv2.imread('surprise.png', cv2.IMREAD_UNCHANGED),
    'neutral': cv2.imread('neutral.png', cv2.IMREAD_UNCHANGED),
    'sad': cv2.imread('sad.png', cv2.IMREAD_UNCHANGED),
    'fear': cv2.imread('fear.png', cv2.IMREAD_UNCHANGED)
}

emoji_size = (190, 190)  # change the size of emoji

Please make sure that the emoji directory is located in the same folder as the execution guide.

Step 3: Real-Time Emotion Detection

The program accesses the webcam and captures real-time video frames, which are then converted to grayscale. The Haar Cascade classifier is used to detect faces in the frames. Emotion analysis is performed on each detected face using DeepFace to extract the dominant emotion. The detected emotion is displayed as text above the face and an emoji is overlaid on the face if the corresponding emoji is available in the dictionary. The program runs continuously until the user presses the "Enter" key (keycode 13) or the "Esc" key (keycode 27), at which point the webcam is released and all OpenCV windows are closed.

The goal of this project is to provide users with a friendly and entertaining visual experience by overlaying emojis that represent the detected emotions on top of the detected faces. This approach is more effective than presenting emotions in a text format as pictorial representations tend to capture more attention. By watching their faces transform into emoji representations, users may gain insight into their own emotions, and society may benefit from this project.

Output (Screenshots):

Real_Time Facial Emotion Detectionreal time facial emotion detectionreal time facial emotion detectionreal time facial emotion detection Note: To analyze emotions accurately, make sure you have the necessary libraries, a working webcam, and a designated folder for your emojis. Good lighting and a stable webcam feed are also essential for a successful project.

Source code: zip folder

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Vinit Kumar Mahato (vinit112)

Download packets of source code on Coders Packet