forked from DhanushNehru/Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
100 lines (82 loc) · 3.01 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import cv2
import numpy as np
from fer import FER
import os
import json
# Load custom emojis from a config file (custom_emojis.json)
def load_custom_emojis(config_file='custom_emojis.json'):
if os.path.exists(config_file):
with open(config_file, 'r') as file:
return json.load(file)
return {}
# Save the emotion detection history
def save_emotion_history(history, filename='emotion_history.txt'):
with open(filename, 'a') as file:
for emotion in history:
file.write(f"{emotion}\n")
# Initialize the webcam
scr = cv2.VideoCapture(0) # 30fps
scr.set(3, 640)
scr.set(4, 480)
# Initialize FER (Facial Expression Recognition)
detector = FER(mtcnn=True)
# Load custom emojis
custom_emojis = load_custom_emojis()
# Initialize emotion detection history
emotion_history = []
while True:
ret, frame = scr.read()
# Return emotion name and % of true detection
emotion, score = detector.top_emotion(frame)
# Append detected emotion to the history
emotion_history.append(emotion)
# Optionally save the history to a file (uncomment to enable)
# save_emotion_history(emotion_history)
print(emotion, score)
# Use custom emoji if available, otherwise fall back to default
if emotion in custom_emojis:
emoj = cv2.imread(custom_emojis[emotion])
else:
# Default emojis if no custom emojis are set
if emotion == 'angry':
emoj = cv2.imread('https://i.ibb.co/QN0gqNH/angry.png')
elif emotion == 'disgust':
emoj = cv2.imread('https://i.ibb.co/tJDxrhD/disgust.png')
elif emotion == 'fear':
emoj = cv2.imread('https://i.ibb.co/yBczSFB/fear.png')
elif emotion == 'happy':
emoj = cv2.imread('https://i.ibb.co/g6DW0Cf/happy.png')
elif emotion == 'sad':
emoj = cv2.imread('https://i.ibb.co/NyF0sDq/sad.png')
elif emotion == 'surprise':
emoj = cv2.imread('https://i.ibb.co/D4rDyfM/surprise.png')
elif emotion == 'neutral':
emoj = cv2.imread('https://i.ibb.co/KX7VSjh/neutral.png')
else:
emoj = cv2.imread('https://i.ibb.co/LdnS9nL/none.png')
# Adding Image on Screen
# Read emoj and resize
size = 150
emoj = cv2.resize(emoj, (size, size))
# Create a mask of emoj
img2gray = cv2.cvtColor(emoj, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 1, 255, cv2.THRESH_BINARY)
roi = frame[-size-20:-20, -size-20:-20]
# Set an index of where the mask is
roi[np.where(mask)] = 0
roi += emoj
# Add text
font = cv2.FONT_HERSHEY_SIMPLEX
org = (40, 210)
fontScale = 1
color = (255, 0, 0)
thickness = 2
cv2.putText(frame, emotion, org, font, fontScale, color, thickness, cv2.LINE_AA)
# Show screen
cv2.imshow('frame', frame)
# Stop
if cv2.waitKey(1) & 0xff == ord('q'):
break
# Release resources
scr.release()
cv2.destroyAllWindows()