-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemotion.py
245 lines (187 loc) · 9.74 KB
/
emotion.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
245
### General imports ###
from __future__ import division
import numpy as np
import pandas as pd
import cv2
from time import time
from time import sleep
import re
import os
import argparse
from collections import OrderedDict
### Image processing ###
from scipy.ndimage import zoom
from scipy.spatial import distance
import imutils
from scipy import ndimage
import dlib
from tensorflow.keras.models import load_model
from imutils import face_utils
import requests
global shape_x
global shape_y
global input_shape
global nClasses
def show_webcam() :
shape_x = 48
shape_y = 48
input_shape = (shape_x, shape_y, 1)
nClasses = 7
thresh = 0.25
frame_check = 20
def eye_aspect_ratio(eye):
A = distance.euclidean(eye[1], eye[5])
B = distance.euclidean(eye[2], eye[4])
C = distance.euclidean(eye[0], eye[3])
ear = (A + B) / (2.0 * C)
return ear
def detect_face(frame):
#Cascade classifier pre-trained model
cascPath = 'face_landmarks.dat'
faceCascade = cv2.CascadeClassifier(cascPath)
#BGR -> Gray conversion
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#Cascade MultiScale classifier
detected_faces = faceCascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=6,
minSize=(shape_x, shape_y),
flags=cv2.CASCADE_SCALE_IMAGE)
coord = []
for x, y, w, h in detected_faces :
if w > 100 :
sub_img=frame[y:y+h,x:x+w]
cv2.rectangle(frame,(x,y),(x+w,y+h),(0, 255,255),1)
coord.append([x,y,w,h])
return gray, detected_faces, coord
def extract_face_features(faces, offset_coefficients=(0.075, 0.05)):
gray = faces[0]
detected_face = faces[1]
new_face = []
for det in detected_face :
#Region dans laquelle la face est détectée
x, y, w, h = det
#X et y correspondent à la conversion en gris par gray, et w, h correspondent à la hauteur/largeur
#Offset coefficient, np.floor takes the lowest integer (delete border of the image)
horizontal_offset = np.int(np.floor(offset_coefficients[0] * w))
vertical_offset = np.int(np.floor(offset_coefficients[1] * h))
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#gray transforme l'image
extracted_face = gray[y+vertical_offset:y+h, x+horizontal_offset:x-horizontal_offset+w]
#Zoom sur la face extraite
new_extracted_face = zoom(extracted_face, (shape_x / extracted_face.shape[0],shape_y / extracted_face.shape[1]))
#cast type float
new_extracted_face = new_extracted_face.astype(np.float32)
#scale
new_extracted_face /= float(new_extracted_face.max())
#print(new_extracted_face)
new_face.append(new_extracted_face)
return new_face
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
(nStart, nEnd) = face_utils.FACIAL_LANDMARKS_IDXS["nose"]
(mStart, mEnd) = face_utils.FACIAL_LANDMARKS_IDXS["mouth"]
(jStart, jEnd) = face_utils.FACIAL_LANDMARKS_IDXS["jaw"]
(eblStart, eblEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eyebrow"]
(ebrStart, ebrEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eyebrow"]
model = load_model('video.h5')
face_detect = dlib.get_frontal_face_detector()
predictor_landmarks = dlib.shape_predictor("face_landmarks.dat")
#Lancer la capture video
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
face_index = 0
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = face_detect(gray, 1)
#gray, detected_faces, coord = detect_face(frame)
for (i, rect) in enumerate(rects):
try:
shape = predictor_landmarks(gray, rect)
shape = face_utils.shape_to_np(shape)
# Identify face coordinates
(x, y, w, h) = face_utils.rect_to_bb(rect)
face = gray[y:y+h,x:x+w]
#Zoom on extracted face
face = zoom(face, (shape_x / face.shape[0],shape_y / face.shape[1]))
#Cast type float
face = face.astype(np.float32)
#Scale
face /= float(face.max())
face = np.reshape(face.flatten(), (1, 48, 48, 1))
#Make Prediction
prediction = model.predict(face)
prediction_result = np.argmax(prediction)
# Rectangle around the face
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, "Face #{}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
for (j, k) in shape:
cv2.circle(frame, (j, k), 1, (0, 0, 255), -1)
# 1. Add prediction probabilities
cv2.putText(frame, "----------------",(40,100 + 180*i), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 155, 0)
cv2.putText(frame, "Emotional report : Face #" + str(i+1),(40,120 + 180*i), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 155, 0)
cv2.putText(frame, "Angry : " + str(round(prediction[0][0],3)),(40,140 + 180*i), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 155, 0)
cv2.putText(frame, "Disgust : " + str(round(prediction[0][1],3)),(40,160 + 180*i), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 155, 0)
cv2.putText(frame, "Fear : " + str(round(prediction[0][2],3)),(40,180 + 180*i), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 155, 1)
cv2.putText(frame, "Happy : " + str(round(prediction[0][3],3)),(40,200 + 180*i), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 155, 1)
cv2.putText(frame, "Sad : " + str(round(prediction[0][4],3)),(40,220 + 180*i), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 155, 1)
cv2.putText(frame, "Surprise : " + str(round(prediction[0][5],3)),(40,240 + 180*i), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 155, 1)
cv2.putText(frame, "Neutral : " + str(round(prediction[0][6],3)),(40,260 + 180*i), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 155, 1)
# 2. Annotate main image with a label
if prediction_result == 0 :
cv2.putText(frame, "Angry",(x+w-10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
elif prediction_result == 1 :
cv2.putText(frame, "Disgust",(x+w-10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
elif prediction_result == 2 :
cv2.putText(frame, "Fear",(x+w-10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
elif prediction_result == 3 :
cv2.putText(frame, "Happy",(x+w-10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
elif prediction_result == 4 :
cv2.putText(frame, "Sad",(x+w-10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
elif prediction_result == 5 :
cv2.putText(frame, "Surprise",(x+w-10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
else :
cv2.putText(frame, "Neutral",(x+w-10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# 3. Eye Detection and Blink Count
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
# Compute Eye Aspect Ratio
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
ear = (leftEAR + rightEAR) / 2.0
# And plot its contours
leftEyeHull = cv2.convexHull(leftEye)
rightEyeHull = cv2.convexHull(rightEye)
cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
# 4. Detect Nose
nose = shape[nStart:nEnd]
noseHull = cv2.convexHull(nose)
cv2.drawContours(frame, [noseHull], -1, (0, 255, 0), 1)
# 5. Detect Mouth
mouth = shape[mStart:mEnd]
mouthHull = cv2.convexHull(mouth)
cv2.drawContours(frame, [mouthHull], -1, (0, 255, 0), 1)
# 6. Detect Jaw
jaw = shape[jStart:jEnd]
jawHull = cv2.convexHull(jaw)
cv2.drawContours(frame, [jawHull], -1, (0, 255, 0), 1)
# 7. Detect Eyebrows
ebr = shape[ebrStart:ebrEnd]
ebrHull = cv2.convexHull(ebr)
cv2.drawContours(frame, [ebrHull], -1, (0, 255, 0), 1)
ebl = shape[eblStart:eblEnd]
eblHull = cv2.convexHull(ebl)
cv2.drawContours(frame, [eblHull], -1, (0, 255, 0), 1)
except:
pass
cv2.putText(frame,'Number of Faces : ' + str(len(rects)),(40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, 155, 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
def main():
show_webcam()
if __name__ == "__main__":
main()