-
Notifications
You must be signed in to change notification settings - Fork 0
/
emotion.py
76 lines (62 loc) · 3 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
!if not exist "./files" mkdir files
# Download Face detection XML
!curl -L -o ./files/haarcascade_frontalface_default.xml https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml
# Download emotion trained data
!curl -L -o ./files/emotion_model.hdf5 https://mechasolution.vn/source/blog/AI-tutorial/Emotion_Recognition/emotion_model.hdf5
import cv2
import numpy as np
from keras.preprocessing.image import img_to_array
from keras.models import load_model
# Face detection XML load and trained model loading
face_detection = cv2.CascadeClassifier('files/haarcascade_frontalface_default.xml')
emotion_classifier = load_model('files/emotion_model.hdf5', compile=False)
EMOTIONS = ["Angry" ,"Disgusting","Fearful", "Happy", "Sad", "Surpring", "Neutral"]
# Video capture using webcam
camera = cv2.VideoCapture(0)
while True:
# Capture image from camera
ret, frame = camera.read()
# Convert color to gray scale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Face detection in frame
faces = face_detection.detectMultiScale(gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30,30))
# Create empty image
canvas = np.zeros((250, 300, 3), dtype="uint8")
# Perform emotion recognition only when face is detected
if len(faces) > 0:
# For the largest image
face = sorted(faces, reverse=True, key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
(fX, fY, fW, fH) = face
# Resize the image to 48x48 for neural network
roi = gray[fY:fY + fH, fX:fX + fW]
roi = cv2.resize(roi, (48, 48))
roi = roi.astype("float") / 255.0
roi = img_to_array(roi)
roi = np.expand_dims(roi, axis=0)
# Emotion predict
preds = emotion_classifier.predict(roi)[0]
emotion_probability = np.max(preds)
label = EMOTIONS[preds.argmax()]
# Assign labeling
cv2.putText(frame, label, (fX, fY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
cv2.rectangle(frame, (fX, fY), (fX + fW, fY + fH), (0, 0, 255), 2)
# Label printing
for (i, (emotion, prob)) in enumerate(zip(EMOTIONS, preds)):
text = "{}: {:.2f}%".format(emotion, prob * 100)
w = int(prob * 300)
cv2.rectangle(canvas, (7, (i * 35) + 5), (w, (i * 35) + 35), (0, 0, 255), -1)
cv2.putText(canvas, text, (10, (i * 35) + 23), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (255, 255, 255), 2)
# Open two windows
## Display image ("Emotion Recognition")
## Display probabilities of emotion
cv2.imshow('Emotion Recognition', frame)
cv2.imshow("Probabilities", canvas)
# q to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Clear program and close windows
camera.release()
cv2.destroyAllWindows()