forked from Furkan-Gulsen/face-classification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemotionRecognitionFa.py
More file actions
131 lines (113 loc) · 3.7 KB
/
Copy pathemotionRecognitionFa.py
File metadata and controls
131 lines (113 loc) · 3.7 KB
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
# Importing required packages
from keras.models import load_model
import numpy as np
import argparse
import dlib
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-vw", "--isVideoWriter", type=bool, default=False)
args = vars(ap.parse_args())
emotion_offsets = (20, 40)
emotions = {
0: {
"emotion": "Asabi",
"color": (193, 69, 42)
},
1: {
"emotion": "Chendesh",
"color": (164, 175, 49)
},
2: {
"emotion": "Tars",
"color": (40, 52, 155)
},
3: {
"emotion": "Khoshhal",
"color": (23, 164, 28)
},
4: {
"emotion": "Narahat",
"color": (164, 93, 23)
},
5: {
"emotion": "Taajob",
"color": (218, 229, 97)
},
6: {
"emotion": "Tabiee",
"color": (108, 72, 200)
}
}
def shapePoints(shape):
coords = np.zeros((68, 2), dtype="int")
for i in range(0, 68):
coords[i] = (shape.part(i).x, shape.part(i).y)
return coords
def rectPoints(rect):
x = rect.left()
y = rect.top()
w = rect.right() - x
h = rect.bottom() - y
return (x, y, w, h)
faceLandmarks = "faceDetection/models/dlib/shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(faceLandmarks)
emotionModelPath = 'models/emotionModel.hdf5' # fer2013_mini_XCEPTION.110-0.65
emotionClassifier = load_model(emotionModelPath, compile=False)
emotionTargetSize = emotionClassifier.input_shape[1:3]
cap = cv2.VideoCapture(0)
if args["isVideoWriter"] == True:
fourrcc = cv2.VideoWriter_fourcc("M", "J", "P", "G")
capWidth = int(cap.get(3))
capHeight = int(cap.get(4))
videoWrite = cv2.VideoWriter("output.avi", fourrcc, 22,
(capWidth, capHeight))
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.resize(frame, (720, 480))
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(grayFrame, 0)
for rect in rects:
shape = predictor(grayFrame, rect)
points = shapePoints(shape)
(x, y, w, h) = rectPoints(rect)
grayFace = grayFrame[y:y + h, x:x + w]
try:
grayFace = cv2.resize(grayFace, (emotionTargetSize))
except:
continue
grayFace = grayFace.astype('float32')
grayFace = grayFace / 255.0
grayFace = (grayFace - 0.5) * 2.0
grayFace = np.expand_dims(grayFace, 0)
grayFace = np.expand_dims(grayFace, -1)
emotion_prediction = emotionClassifier.predict(grayFace)
emotion_probability = np.max(emotion_prediction)
if (emotion_probability > 0.36):
emotion_label_arg = np.argmax(emotion_prediction)
color = emotions[emotion_label_arg]['color']
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.line(frame, (x, y + h), (x + 20, y + h + 20),
color,
thickness=2)
cv2.rectangle(frame, (x + 20, y + h + 20), (x + 110, y + h + 40),
color, -1)
cv2.putText(frame, emotions[emotion_label_arg]['emotion'],
(x + 25, y + h + 36), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(255, 255, 255), 1, cv2.LINE_AA)
print(emotions[emotion_label_arg]['emotion'])
else:
color = (255, 255, 255)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
if args["isVideoWriter"] == True:
videoWrite.write(frame)
cv2.imshow("Emotion Recognition", frame)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
cap.release()
if args["isVideoWriter"] == True:
videoWrite.release()
cv2.destroyAllWindows()