-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
244 lines (189 loc) · 6.78 KB
/
utils.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import cv2
import numpy as np
from constants import *
face_cascade = cv2.CascadeClassifier(CASC_PATH)
def draw_rectangle(frame, faces):
for i, face in enumerate(faces):
cv2.rectangle(frame, \
(face[0], face[1]), \
(face[0] + face[2], face[1] + face[3]), \
(255, 0, 0), \
2)
return frame
def predict_emotions(image_faces, fer_model):
results = []
for i, image_face in enumerate(image_faces):
face = image_face.reshape([-1, FACE_SIZE, FACE_SIZE, 1])
results.append(fer_model.predict(face))
return results
def draw_emotions(frame, emotion, results, faces, emoji_codes):
for i, face in enumerate(faces):
emotion_index = np.argmax(results[i])
half_width = int(face[2] / 2)
es = EMOTION_SIZE
for x in range(es):
for y in range(es):
if emoji_codes[emotion_index][y][x] == 1:
continue
try:
frame[y + face[1] - es, x + face[0] + half_width - int(es / 2)] = emotion[emotion_index][y, x]
except Exception:
print("out of range")
return frame
def format_image(image, max_face=1):
if len(image.shape) > 2 and image.shape[2] == 3:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
image = cv2.imdecode(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
faces = face_cascade.detectMultiScale(
image,
scaleFactor=1.3,
minNeighbors=5
)
# None is we don't found an image
if not len(faces) > 0:
return None, None
image_faces = []
count = 0
for face in faces:
image_face = image[face[1]: face[1] + face[2], face[0] : face[0] + face[3]]
image_face = _resize_face_img(image_face)
image_faces.append(image_face)
count += 1
if count == max_face:
break
# try:
# cv2.imshow("face1", image_faces[0])
# except Exception:
# print("no face 1")
#
# try:
# cv2.imshow("face2", image_faces[1])
# except Exception:
# print("no face 2")
return image_faces, faces
# ============= Old ==================
cv2.imshow("gray", image)
image = _parsing_max_area_face(faces, image)
# Resize image to network size
image = _resize_face_img(image)
# cv2.imshow("Lol", image)
# cv2.waitKey(0)
return image, faces
# ====================================
def find_max_area_face(faces):
max_area_face = faces[0]
for face in faces:
if face[2] * face[3] > max_area_face[2] * max_area_face[3]:
max_area_face = face
return max_area_face
def _parsing_face(face, image):
# Chop image to face
image = image[face[1]:(face[1] + face[2]), face[0]:(face[0] + face[3])]
return image
def _resize_face_img(image):
try:
image = cv2.resize(image, (FACE_SIZE, FACE_SIZE),
interpolation=cv2.INTER_CUBIC) / 255.
except Exception:
print("[+] Problem during resize")
return None
# cv2.imshow("Lol", image)
# cv2.waitKey(0)
return image
def post_process(frame, outs, conf_threshold, nms_threshold):
frame_height = frame.shape[0]
frame_width = frame.shape[1]
# Scan through all the bounding boxes output from the network and keep only
# the ones with high confidence scores. Assign the box's class label as the
# class with the highest score.
confidences = []
boxes = []
final_boxes = []
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)
if not len(indices) > 0:
return None, None
# for i in indices:
# i = i[0]
# box = boxes[i]
# left = box[0]
# top = box[1]
# width = box[2]
# height = box[3]
# final_boxes.append(box)
# draw_predict(frame, confidences[i], left, top, left + width,
# top + height)
#
# box = boxes[indices[0][0]]
# left = box[0]
# top = box[1]
# width = box[2]
# height = box[3]
# draw_predict(frame, confidences[indices[0][0]], left, top, left + width,
# top + height)
#
if len(frame.shape) > 2 and frame.shape[2] == 3:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
frame = cv2.imdecode(frame, cv2.CV_LOAD_IMAGE_GRAYSCALE)
resized_faces = []
for box in boxes:
box[2], box[3] = box[3], box[2]
face = _parsing_face(box, frame)
resized_faces.append(_resize_face_img(face))
box[2], box[3] = box[3], box[2]
try:
cv2.imshow("yolo_camera", resized_faces[0])
except Exception:
print("failed to show")
return boxes, resized_faces
# image = _parsing_max_area_yolo(box, frame)
#
# try:
# cv2.imshow("yolo_camera", image)
# except Exception:
# print("face not in frame")
# # Resize image to network size
# resized_face = _resize_face_img(image)
#
# return final_boxes, resized_face
def _parsing_max_area_yolo(box, frame):
left = box[0]
top = box[1]
width = box[2]
height = box[3]
# print(left, top, width, height)
image = frame[box[1]:(box[1] + box[3]), box[0]:(box[0] + box[2])]
return image
def draw_predict(frame, conf, left, top, right, bottom):
# Draw a bounding box.
cv2.rectangle(frame, (left, top), (right, bottom), COLOR_YELLOW, 2)
text = '{:.2f}'.format(conf)
# Display the label at the top of the bounding box
label_size, base_line = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
top = max(top, label_size[1])
cv2.putText(frame, text, (left, top - 4), cv2.FONT_HERSHEY_SIMPLEX, 0.4,
COLOR_WHITE, 1)
def get_outputs_names(net):
# Get the names of all the layers in the network
layers_names = net.getLayerNames()
# Get the names of the output layers, i.e. the layers with unconnected
# outputs
return [layers_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]