-
Notifications
You must be signed in to change notification settings - Fork 0
/
fer_tools.py
92 lines (66 loc) · 2.31 KB
/
fer_tools.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
import cv2, base64
from fer import FER, Video
from PIL import Image
from io import BytesIO
import matplotlib.pyplot as plt
def picture_taker(img_name, img_path):
"""
Opens webcam and captures facial pic
Input: Image name and path (str)
Output: None
"""
cam = cv2.VideoCapture(0)
cv2.namedWindow("test")
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
cv2.imshow("test", frame)
k = cv2.waitKey(1)
if k%256 == 32:
# SPACE pressed
img_name = f"{img_path}/{img_name}.png"
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
break
cam.release()
cv2.destroyAllWindows()
def facial_emotion_recognizer(img_name, img_path):
"""
Recognizes emotions from a stored picture
Input: Image name and path (str)
Output: Dominant emotion (str) and Score (Float)
"""
test_image_one = plt.imread(f"{img_path}/{img_name}.jpeg")
emo_detector = FER(mtcnn=True)
# Use the top Emotion() function to call for the dominant emotion in the image
dominant_emotion, emotion_score = emo_detector.top_emotion(test_image_one)
return (dominant_emotion, emotion_score)
def video_emotion_recognizer(video_name, video_path):
"""
Recognizes emotions from a stored picture
Input: Image name and path (str)
Output: Dominant emotion (str) and Score (Float)
"""
test_video_one = Video(f"{video_path}/{video_name}.mpeg4")
emo_detector = FER(mtcnn=True)
processing_data = test_video_one.analyze(emo_detector, display=False)
# recognizing emotions
vid_df = test_video_one.to_pandas(processing_data)
vid_df = test_video_one.get_first_face(vid_df)
vid_df = test_video_one.get_emotions(vid_df)
return vid_df
def base64_decoder(b64_string, img_name, img_path):
"""
Converts a base 64 image to png format
Input: Base 64 image (string)
Output: None
"""
# converting b64 str to to img object
byte_img = bytes(b64_string, "utf-8")
decoded_bytes = base64.decodebytes(byte_img)
img = Image.open(BytesIO(decoded_bytes))
# storing img on designated path
filename = f"{img_path}/{img_name}.jpeg"
img.save(filename)