Spaces:
Sleeping
Sleeping
maxinethegreat
commited on
Commit
•
7b2403a
1
Parent(s):
2d9f1e0
fix function
Browse files
app.py
CHANGED
@@ -16,23 +16,27 @@ no_face_detection_alert = "Cannot Detect Face"
|
|
16 |
low_confidence_alert = "Cannot Detect Emotion"
|
17 |
|
18 |
# Define the predict_emotion function
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
face =
|
25 |
-
face =
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
else:
|
32 |
-
cv2.putText(frame,
|
33 |
-
2)
|
34 |
-
else:
|
35 |
-
cv2.putText(frame, no_face_detection_alert, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 69, 255), 2)
|
36 |
|
37 |
return frame
|
38 |
|
|
|
16 |
low_confidence_alert = "Cannot Detect Emotion"
|
17 |
|
18 |
# Define the predict_emotion function
|
19 |
+
|
20 |
+
def predict_emotion(frame):
|
21 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
22 |
+
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
23 |
+
for (x, y, w, h) in faces:
|
24 |
+
face = gray[y:y+h, x:x+w]
|
25 |
+
face = cv2.resize(face, (48, 48), interpolation = cv2.INTER_AREA)
|
26 |
+
if np.sum([face])!=0:
|
27 |
+
face = face.astype('float')/255.0
|
28 |
+
face = tf.keras.utils.img_to_array(face)
|
29 |
+
face = np.expand_dims(face, axis=0)
|
30 |
+
prediction = model.predict(face)
|
31 |
+
if any(prob >.5 for prob in prediction[0]):
|
32 |
+
emotion = emotions[np.argmax(prediction)]
|
33 |
+
cv2.putText(frame, emotion, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (128, 128, 0), 2)
|
34 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 0), 2)
|
35 |
+
else:
|
36 |
+
cv2.putText(frame, low_confidence_alert, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 69, 255),
|
37 |
+
2)
|
38 |
else:
|
39 |
+
cv2.putText(frame, no_face_detection_alert, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 69, 255), 2)
|
|
|
|
|
|
|
40 |
|
41 |
return frame
|
42 |
|