1
1
import cv2
2
2
import numpy as np
3
3
from fer import FER
4
+ import os
5
+ import json
4
6
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
6
22
scr .set (3 , 640 )
7
23
scr .set (4 , 480 )
8
24
9
- #init FRE (Facial expression recognition from images )
25
+ # Initialize FER (Facial Expression Recognition )
10
26
detector = FER (mtcnn = True )
11
27
12
- while True :
28
+ # Load custom emojis
29
+ custom_emojis = load_custom_emojis ()
13
30
14
- ret , frame = scr . read ()
15
- # frame = cv2.flip(frame,1)
31
+ # Initialize emotion detection history
32
+ emotion_history = []
16
33
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
18
38
emotion , score = detector .top_emotion (frame )
19
39
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)
37
44
45
+ print (emotion , score )
38
46
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
41
70
# Read emoj and resize
42
71
size = 150
43
72
emoj = cv2 .resize (emoj , (size , size ))
47
76
ret , mask = cv2 .threshold (img2gray , 1 , 255 , cv2 .THRESH_BINARY )
48
77
49
78
roi = frame [- size - 20 :- 20 , - size - 20 :- 20 ]
50
- # roi = frame[-size-310:-310, -size-470:-470]
51
79
# Set an index of where the mask is
52
80
roi [np .where (mask )] = 0
53
81
roi += emoj
54
82
55
-
56
- #add text
83
+ # Add text
57
84
font = cv2 .FONT_HERSHEY_SIMPLEX
58
85
org = (40 , 210 )
59
86
fontScale = 1
60
87
color = (255 , 0 , 0 )
61
88
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
68
95
if cv2 .waitKey (1 ) & 0xff == ord ('q' ):
69
96
break
70
97
98
+ # Release resources
71
99
scr .release ()
72
-
73
- cv2 .destroyAllWindows ()
100
+ cv2 .destroyAllWindows ()
0 commit comments