回答編集履歴
2
修正
test
CHANGED
@@ -174,12 +174,6 @@
|
|
174
174
|
|
175
175
|
label = EMOTIONS[preds.argmax()]
|
176
176
|
|
177
|
-
###
|
178
|
-
|
179
|
-
time.sleep(1)
|
180
|
-
|
181
|
-
###
|
182
|
-
|
183
177
|
else: continue
|
184
178
|
|
185
179
|
|
1
修正
test
CHANGED
@@ -25,3 +25,219 @@
|
|
25
25
|
prev_time = curr_time
|
26
26
|
|
27
27
|
```
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
## 追記
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
「前回表情検出を行った時刻から 0.1 秒以上経過しているかどうか」という判定をする処理だったので、以下のように修正していただく意図でした。
|
36
|
+
|
37
|
+
動かしての確認はしていません。
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
```
|
42
|
+
|
43
|
+
from keras.preprocessing.image import img_to_array
|
44
|
+
|
45
|
+
import imutils
|
46
|
+
|
47
|
+
import cv2
|
48
|
+
|
49
|
+
from keras.models import load_model
|
50
|
+
|
51
|
+
import numpy as np
|
52
|
+
|
53
|
+
import time
|
54
|
+
|
55
|
+
###
|
56
|
+
|
57
|
+
import os
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
filename = "MMM"
|
62
|
+
|
63
|
+
filenameMP4 = filename + ".mp4"
|
64
|
+
|
65
|
+
filenameTXT = filename + ".txt"
|
66
|
+
|
67
|
+
print(filenameTXT)
|
68
|
+
|
69
|
+
###
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
# parameters for loading data and images
|
74
|
+
|
75
|
+
detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
|
76
|
+
|
77
|
+
emotion_model_path = 'models/_mini_XCEPTION.102-0.66.hdf5'
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
# hyper-parameters for bounding boxes shape
|
82
|
+
|
83
|
+
# loading models
|
84
|
+
|
85
|
+
face_detection = cv2.CascadeClassifier(detection_model_path)
|
86
|
+
|
87
|
+
emotion_classifier = load_model(emotion_model_path, compile=False)
|
88
|
+
|
89
|
+
EMOTIONS = ["angry" ,"disgust","scared", "happy", "sad", "surprised",
|
90
|
+
|
91
|
+
"neutral"]
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
#feelings_faces = []
|
98
|
+
|
99
|
+
#for index, emotion in enumerate(EMOTIONS):
|
100
|
+
|
101
|
+
# feelings_faces.append(cv2.imread('emojis/' + emotion + '.png', -1))
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
# starting video streaming
|
106
|
+
|
107
|
+
cv2.namedWindow('your_face')
|
108
|
+
|
109
|
+
camera = cv2.VideoCapture(r'C:\Users\yukak\OneDrive\experiment_videos\Mari_Elka_Pangestu_M.mp4')
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
prev_time = time.time()
|
114
|
+
|
115
|
+
while True:
|
116
|
+
|
117
|
+
frame = camera.read()[1]
|
118
|
+
|
119
|
+
#reading the frame
|
120
|
+
|
121
|
+
frame = imutils.resize(frame,width=300)
|
122
|
+
|
123
|
+
################################################################
|
124
|
+
|
125
|
+
curr_time = time.time()
|
126
|
+
|
127
|
+
if curr_time - prev_time >= 0.1:
|
128
|
+
|
129
|
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
130
|
+
|
131
|
+
faces = face_detection.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
canvas = np.zeros((250, 300, 3), dtype="uint8")
|
136
|
+
|
137
|
+
frameClone = frame.copy()
|
138
|
+
|
139
|
+
if len(faces) > 0:
|
140
|
+
|
141
|
+
faces = sorted(faces, reverse=True,
|
142
|
+
|
143
|
+
key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
|
144
|
+
|
145
|
+
(fX, fY, fW, fH) = faces
|
146
|
+
|
147
|
+
# Extract the ROI of the face from the grayscale image, resize it to a fixed 28x28 pixels, and then prepare
|
148
|
+
|
149
|
+
# the ROI for classification via the CNN
|
150
|
+
|
151
|
+
roi = gray[fY:fY + fH, fX:fX + fW]
|
152
|
+
|
153
|
+
roi = cv2.resize(roi, (64, 64))
|
154
|
+
|
155
|
+
roi = roi.astype("float") / 255.0
|
156
|
+
|
157
|
+
roi = img_to_array(roi)
|
158
|
+
|
159
|
+
roi = np.expand_dims(roi, axis=0)
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
preds = emotion_classifier.predict(roi)[0]
|
166
|
+
|
167
|
+
with open(filenameTXT, 'a') as f:
|
168
|
+
|
169
|
+
#print(preds)
|
170
|
+
|
171
|
+
print(preds, file=f)
|
172
|
+
|
173
|
+
emotion_probability = np.max(preds)
|
174
|
+
|
175
|
+
label = EMOTIONS[preds.argmax()]
|
176
|
+
|
177
|
+
###
|
178
|
+
|
179
|
+
time.sleep(1)
|
180
|
+
|
181
|
+
###
|
182
|
+
|
183
|
+
else: continue
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
for (i, (emotion, prob)) in enumerate(zip(EMOTIONS, preds)):
|
190
|
+
|
191
|
+
# construct the label text
|
192
|
+
|
193
|
+
text = "{}: {:.2f}%".format(emotion, prob * 100)
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
# draw the label + probability bar on the canvas
|
198
|
+
|
199
|
+
# emoji_face = feelings_faces[np.argmax(preds)]
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
w = int(prob * 300)
|
206
|
+
|
207
|
+
cv2.rectangle(canvas, (7, (i * 35) + 5),
|
208
|
+
|
209
|
+
(w, (i * 35) + 35), (0, 0, 255), -1)
|
210
|
+
|
211
|
+
cv2.putText(canvas, text, (10, (i * 35) + 23),
|
212
|
+
|
213
|
+
cv2.FONT_HERSHEY_SIMPLEX, 0.45,
|
214
|
+
|
215
|
+
(255, 255, 255), 2)
|
216
|
+
|
217
|
+
cv2.putText(frameClone, label, (fX, fY - 10),
|
218
|
+
|
219
|
+
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
|
220
|
+
|
221
|
+
cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH),
|
222
|
+
|
223
|
+
(0, 0, 255), 2)
|
224
|
+
|
225
|
+
prev_time = curr_time
|
226
|
+
|
227
|
+
##########################################
|
228
|
+
|
229
|
+
cv2.imshow('your_face', frameClone)
|
230
|
+
|
231
|
+
cv2.imshow("Probabilities", canvas)
|
232
|
+
|
233
|
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
234
|
+
|
235
|
+
break
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
camera.release()
|
240
|
+
|
241
|
+
cv2.destroyAllWindows()
|
242
|
+
|
243
|
+
```
|