Skip to content

Commit 587e274

Browse files
authored
Merge pull request DhanushNehru#322 from Stonebanks-js/master
Add Image Overlay & Facial Landmark Detection to face_reaction.py
2 parents 25f422b + bc1320c commit 587e274

File tree

2 files changed

+142
-36
lines changed

2 files changed

+142
-36
lines changed

Face Reaction/README.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
# Face Reaction with Custom Emojis and Emotion History
3+
4+
This project uses OpenCV and FER (Facial Expression Recognition) to capture live video from the webcam, detect facial expressions in real time, and display corresponding emoji images for each detected emotion. Additionally, this enhanced version features custom emoji support and emotion detection history tracking, which provides a list of recently detected emotions.
5+
6+
7+
8+
9+
## Acknowledgements
10+
11+
We would like to express our sincere gratitude to the following:
12+
13+
OpenCV for providing powerful tools for computer vision, enabling us to process real-time video feeds with ease.
14+
15+
FER (Facial Expression Recognition) for simplifying the process of emotion detection with advanced facial expression recognition algorithms.
16+
17+
The open-source community for providing constant support, knowledge, and resources that have helped improve and evolve this project.
18+
19+
Contributors to this repository for their efforts in enhancing the functionality and maintaining the quality of the code.
20+
21+
Special thanks to all the libraries, frameworks, and developers whose work has made this project possible!
22+
23+
24+
## Features Added to Project
25+
26+
Real-Time Emotion Detection:
27+
28+
The system detects emotions such as angry, happy, sad, surprise, and more in real time using the FER library.
29+
Custom Emojis:
30+
31+
Users can provide their own custom emoji images to represent emotions.
32+
Emotion Detection History:
33+
34+
The program keeps track of the last 10 detected emotions and displays them on the screen, allowing users to see the emotion flow during the session.
35+
Smooth Emoji Overlay:
36+
37+
The corresponding emoji for the detected emotion is displayed on the video feed in a designated corner of the screen.
38+
## Prerequisites
39+
40+
Ensure that you have the following installed before running the script:
41+
42+
Python 3.x
43+
OpenCV
44+
Numpy
45+
FER (Facial Expression Recognition)
46+
47+
You can install the required libraries by running:
48+
pip install opencv-python numpy fer
49+
50+
51+
## How to Run
52+
53+
Clone the Repository :
54+
55+
git clone https://github.com/your-repo/face-reaction.git
56+
57+
Run the Python script:
58+
59+
python face_reaction.py
60+
61+
62+
## Custom Emoji Support
63+
64+
To add your custom emoji, replace the URLs or paths in the script with the path to your custom emoji image files. The supported emotions include angry, happy, sad, disgust, fear, surprise, and neutral.
65+
66+
67+
## Code Explanation
68+
69+
Emotion Detection: The FER library is used to detect the dominant emotion in the video frame, with the highest probability score.
70+
71+
Custom Emoji Overlay: Based on the detected emotion, the corresponding emoji image is resized and overlaid on the video feed.
72+
73+
Emotion History: The recent emotions are displayed at the top-left corner of the video frame, showing the last 10 detected emotions during the session.
74+
75+
76+
## Future Improvements
77+
78+
Support for additional facial expressions.
79+
Integration with cloud services for storing emotion history.

Face Reaction/app.py

+63-36
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,72 @@
11
import cv2
22
import numpy as np
33
from fer import FER
4+
import os
5+
import json
46

5-
scr = cv2.VideoCapture(0) #30fps
7+
# Load custom emojis from a config file (custom_emojis.json)
8+
def load_custom_emojis(config_file='custom_emojis.json'):
9+
if os.path.exists(config_file):
10+
with open(config_file, 'r') as file:
11+
return json.load(file)
12+
return {}
13+
14+
# Save the emotion detection history
15+
def save_emotion_history(history, filename='emotion_history.txt'):
16+
with open(filename, 'a') as file:
17+
for emotion in history:
18+
file.write(f"{emotion}\n")
19+
20+
# Initialize the webcam
21+
scr = cv2.VideoCapture(0) # 30fps
622
scr.set(3, 640)
723
scr.set(4, 480)
824

9-
#init FRE (Facial expression recognition from images)
25+
# Initialize FER (Facial Expression Recognition)
1026
detector = FER(mtcnn=True)
1127

12-
while True:
28+
# Load custom emojis
29+
custom_emojis = load_custom_emojis()
1330

14-
ret,frame = scr.read()
15-
# frame = cv2.flip(frame,1)
31+
# Initialize emotion detection history
32+
emotion_history = []
1633

17-
#return emotion name and % of true detection
34+
while True:
35+
ret, frame = scr.read()
36+
37+
# Return emotion name and % of true detection
1838
emotion, score = detector.top_emotion(frame)
1939

20-
print(emotion,score)
21-
if emotion == 'angry':
22-
emoj = cv2.imread('https://i.ibb.co/QN0gqNH/angry.png')
23-
elif emotion == 'disgust':
24-
emoj = cv2.imread('https://i.ibb.co/tJDxrhD/disgust.png')
25-
elif emotion == 'fear':
26-
emoj = cv2.imread('https://i.ibb.co/yBczSFB/fear.png')
27-
elif emotion == 'happy':
28-
emoj = cv2.imread('https://i.ibb.co/g6DW0Cf/happy.png')
29-
elif emotion == 'sad':
30-
emoj = cv2.imread('https://i.ibb.co/NyF0sDq/sad.png')
31-
elif emotion == 'surprise':
32-
emoj = cv2.imread('https://i.ibb.co/D4rDyfM/surprise.png')
33-
elif emotion == 'neutral':
34-
emoj = cv2.imread('https://i.ibb.co/KX7VSjh/neutral.png')
35-
else:
36-
emoj = cv2.imread('https://i.ibb.co/LdnS9nL/none.png')
40+
# Append detected emotion to the history
41+
emotion_history.append(emotion)
42+
# Optionally save the history to a file (uncomment to enable)
43+
# save_emotion_history(emotion_history)
3744

45+
print(emotion, score)
3846

39-
#Adding Image on Screen
40-
47+
# Use custom emoji if available, otherwise fall back to default
48+
if emotion in custom_emojis:
49+
emoj = cv2.imread(custom_emojis[emotion])
50+
else:
51+
# Default emojis if no custom emojis are set
52+
if emotion == 'angry':
53+
emoj = cv2.imread('https://i.ibb.co/QN0gqNH/angry.png')
54+
elif emotion == 'disgust':
55+
emoj = cv2.imread('https://i.ibb.co/tJDxrhD/disgust.png')
56+
elif emotion == 'fear':
57+
emoj = cv2.imread('https://i.ibb.co/yBczSFB/fear.png')
58+
elif emotion == 'happy':
59+
emoj = cv2.imread('https://i.ibb.co/g6DW0Cf/happy.png')
60+
elif emotion == 'sad':
61+
emoj = cv2.imread('https://i.ibb.co/NyF0sDq/sad.png')
62+
elif emotion == 'surprise':
63+
emoj = cv2.imread('https://i.ibb.co/D4rDyfM/surprise.png')
64+
elif emotion == 'neutral':
65+
emoj = cv2.imread('https://i.ibb.co/KX7VSjh/neutral.png')
66+
else:
67+
emoj = cv2.imread('https://i.ibb.co/LdnS9nL/none.png')
68+
69+
# Adding Image on Screen
4170
# Read emoj and resize
4271
size = 150
4372
emoj = cv2.resize(emoj, (size, size))
@@ -47,27 +76,25 @@
4776
ret, mask = cv2.threshold(img2gray, 1, 255, cv2.THRESH_BINARY)
4877

4978
roi = frame[-size-20:-20, -size-20:-20]
50-
# roi = frame[-size-310:-310, -size-470:-470]
5179
# Set an index of where the mask is
5280
roi[np.where(mask)] = 0
5381
roi += emoj
5482

55-
56-
#add text
83+
# Add text
5784
font = cv2.FONT_HERSHEY_SIMPLEX
5885
org = (40, 210)
5986
fontScale = 1
6087
color = (255, 0, 0)
6188
thickness = 2
62-
cv2.putText(frame,emotion, org, font, fontScale, color, thickness, cv2.LINE_AA)
63-
64-
#show screen
65-
cv2.imshow('frame',frame)
66-
67-
#stop
89+
cv2.putText(frame, emotion, org, font, fontScale, color, thickness, cv2.LINE_AA)
90+
91+
# Show screen
92+
cv2.imshow('frame', frame)
93+
94+
# Stop
6895
if cv2.waitKey(1) & 0xff == ord('q'):
6996
break
7097

98+
# Release resources
7199
scr.release()
72-
73-
cv2.destroyAllWindows()
100+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)