-
Notifications
You must be signed in to change notification settings - Fork 0
/
emotion_recognition.py
198 lines (166 loc) · 6.31 KB
/
emotion_recognition.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""
Utilities for emotion recognition
"""
import random
from typing import List, Tuple
import cv2
import numpy as np
IMAGE_WIDTH, IMAGE_HEIGHT = 416, 416
TARGET_SIZE = 48
COLOR_GREEN = (0, 255, 0)
COLOR_WHITE = (255, 255, 255)
def class_colors(names):
"""
Create a dict with one random BGR color for each
class name
"""
return {
name: (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
for name in names
}
class Position:
"""
Represents the position of an object
"""
def __init__(self, left, top, width, height) -> None:
self.left = left
self.top = top
self.width = width
self.height = height
@property
def right(self):
"""Right position of the object, which is calculated as left + width"""
return self.left + self.width
@property
def bottom(self):
"""Bottom position of the object, which is calculated as top + height"""
return self.top + self.height
def __str__(self) -> str:
return f"Position(left={self.left}, top={self.top}, right={self.right}, bottom={self.bottom}, width={self.width}, height={self.height})"
class EmotionRecognition:
"""Class to recognise faces. Uses OpenCV under the hood to draw bounding boxes and load the model"""
CLASSES = [
"neutral",
"happiness",
"surprise",
"sadness",
"anger",
"disgust",
"fear",
"contempt",
"unknown",
]
COLORS = class_colors(CLASSES)
def __init__(self, face_recognition_model, emotion_recognition_model) -> None:
self.face_recognition_model = face_recognition_model
self.emotion_recognition_model = emotion_recognition_model
def _refined_box(self, left, top, width, height):
right = left + width
bottom = top + height
original_vert_height = bottom - top
top = int(top + original_vert_height * 0.15)
bottom = int(bottom - original_vert_height * 0.05)
margin = ((bottom - top) - (right - left)) // 2
left = (
left - margin
if (bottom - top - right + left) % 2 == 0
else left - margin - 1
)
right = right + margin
return left, top, right, bottom
def draw_bounding_box(
self,
image: cv2.Mat,
conf_threshold,
pos: Position,
label=None,
with_emotion_label=True,
):
"""Draws a bounding box
Args:
image (cv2.Mat): Image to draw a bounding box on
conf_threshold (_type_): Confidence threshold
pos (Position): Position of the bounding box
"""
cv2.rectangle(
image, (pos.left, pos.top), (pos.right, pos.bottom), self.COLORS[label], 2
)
# text = f"{conf_threshold:.2f}"
if with_emotion_label:
# Display the label at the top of the bounding box
label_size, _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
top = max(pos.top, label_size[1])
cv2.putText(
image,
label,
(pos.left, top - 6),
cv2.FONT_HERSHEY_SIMPLEX,
0.65,
self.COLORS[label],
1,
)
def predict_emotion(self, image):
roi = cv2.resize(image, (TARGET_SIZE, TARGET_SIZE)) / 255
predictions = self.emotion_recognition_model.predict(
roi.reshape(-1, TARGET_SIZE, TARGET_SIZE, 3)
)
predicted_emotion = self.CLASSES[predictions.argmax(-1)[0]]
return predicted_emotion
def draw_bounding_boxes(self, detections, image, with_emotions=True):
emotion = None
for pos, confidence in detections:
if with_emotions:
roi = image[pos.top : pos.bottom, pos.left : pos.right]
emotion = self.predict_emotion(roi)
self.draw_bounding_box(image, confidence, pos, emotion)
return image
def detect_faces(
self, image: cv2.Mat, conf_threshold: float, nms_threshold: float
) -> List[Tuple[Position, float]]:
"""Detect faces given an image
Scan through all the bounding boxes output from the network and keep only
those with high confidence scores. Assign the box's class label as the class
with the highest score.
Args:
image (cv2.Mat): Image to detect
conf_threshold (float): Confidence threshold
nms_threshold (float): Non-max suppression threshold
"""
blob = cv2.dnn.blobFromImage(
image, 1 / 255, (IMAGE_WIDTH, IMAGE_HEIGHT), (0, 0, 0), True, crop=False
)
self.face_recognition_model.setInput(blob)
# Predict faces
outs = self.face_recognition_model.forward(
self.face_recognition_model.getUnconnectedOutLayersNames()
)
frame_height = image.shape[0]
frame_width = image.shape[1]
confidences = []
boxes: List[List[int, int, int, int]] = []
recognitions: List[Tuple[Position, float]] = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > conf_threshold:
center_x = int(detection[0] * frame_width)
center_y = int(detection[1] * frame_height)
width = int(detection[2] * frame_width)
height = int(detection[3] * frame_height)
left = int(center_x - width / 2)
top = int(center_y - height / 2)
confidences.append(float(confidence))
boxes.append([left, top, width, height])
# Perform non maximum suppression to eliminate redundant
# overlapping boxes with lower confidences.
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
for i in indices:
box = boxes[i]
left, top, right, bottom = self._refined_box(box[0], box[1], box[2], box[3])
bbox_pos = Position(
max(left, 0), max(top, 0), max(right - left, 0), max(bottom - top, 0)
)
recognitions.append((bbox_pos, confidences[i]))
return recognitions