Skip to content

Commit 1cd54ec

Browse files
committed
Add Custom Emoji Support and Emotion Detection History
1 parent af1e729 commit 1cd54ec

File tree

1 file changed

+63
-36
lines changed

1 file changed

+63
-36
lines changed

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)