-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathemotionclassification.py
177 lines (153 loc) · 6.55 KB
/
emotionclassification.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
#!/usr/bin/env python3
# -*- coding = utf-8 -*-
import os
import sys
import json
import argparse
from statistics import mode
import cv2
import numpy as np
from keras.models import load_model
from data.load_data import get_fer2013_data
from models.model_factory import load_keras_model
from util.constant import fer2013_classes
from util.classifyimgops import apply_offsets
from util.classifyimgops import preprocess_input
from util.info import load_info
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--detector", default = "DNN",
help = "The detector used to detect faces: [mtcnn, dnn, cascade]")
ap.add_argument("-i", "--image", default = None,
help = "The images that you want to detect faces from.")
args = vars(ap.parse_args())
# Get Cascade Classifier and DNN Model.
face_detector, net = load_info()
# Choose Cascade vs DNN.
dnn = True
cascade = False
# Bounding.
image_window = 10
if dnn:
emotion_offsets = (45, 40)
elif cascade:
emotion_offsets = (30, 40)
else:
raise ValueError("You must choose either dnn or cascade.")
# Load Emotion Detection Model.
emotion_labels = fer2013_classes
emotion_classifier = load_keras_model('Model-27-0.6631', compile = False)
emotion_target_size = emotion_classifier.input_shape[1:3]
emotion_window = []
# If running from an IDE (not from command line), then enter images here.
savedir = "modded" # Directory to save changed images.
userimages = ["./test_imgs/amoghneutralnew.jpg"]
for image in userimages:
file, extension = os.path.splitext(image)
file = os.path.basename(file)
image = np.array(cv2.imread(image), dtype = 'uint8')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
global faces
if dnn:
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), swapRB=False, crop=False)
net.setInput(blob)
faces = net.forward()
if cascade:
faces = face_detector.detectMultiScale(gray_image, 1.3, 5)
if cascade:
for face_coordinates in faces:
print(face_coordinates)
x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
gray_face = gray_image[y1: y2, x1: x2]
try:
gray_face = cv2.resize(gray_face, (emotion_target_size))
except:
continue
gray_face = preprocess_input(gray_face, False)
gray_face = np.expand_dims(gray_face, 0)
gray_face = np.expand_dims(gray_face, -1)
emotion_prediction = emotion_classifier.predict(gray_face)
emotion_probability = np.max(emotion_prediction)
emotion_label_arg = np.argmax(emotion_prediction)
emotion_text = emotion_labels[emotion_label_arg]
emotion_window.append(emotion_text)
if len(emotion_window) > image_window: emotion_window.pop(0)
try:
emotion_mode = mode(emotion_window)
except:
continue
if emotion_text == 'happy':
color = emotion_probability * np.asarray((255, 255, 0))
elif emotion_text == 'angry':
color = emotion_probability * np.asarray((255, 0, 0))
elif emotion_text == 'sad':
color = emotion_probability * np.asarray((0, 0, 255))
elif emotion_text == 'surprise':
color = emotion_probability * np.asarray((0, 255, 255))
else:
color = emotion_probability * np.asarray((0, 255, 0))
color = color.astype(int)
color = color.tolist()
x, y, w, h = face_coordinates
# cv2.rectangle(image, (x, y), (xe, ye), color, 3)
cv2.rectangle(image, (x, y), (x + w, y + h), color, 3)
cv2.putText(image, emotion_mode, (x + 0, y - 45), cv2.FONT_HERSHEY_SIMPLEX,
1, color, 3, cv2.LINE_AA)
# Print a background behind the text.
bg = np.full((image.shape), (0, 0, 0), dtype = np.uint8)
cv2.putText(bg, emotion_mode, (x + 0, y - 45), cv2.FONT_HERSHEY_SIMPLEX,
1, color, 3, cv2.LINE_AA)
x1, y1, w1, h1 = cv2.boundingRect(bg[:,:,2])
image[y1: y1 + h1, x1: x1 + w1] = bg[y1: y1 + h1, x1: x1 + w1]
if dnn:
(h, w) = image.shape[:2]
for k in range(0, faces.shape[2]):
c = faces[0, 0, k, 2]
if c < 0.5: continue
box = faces[0, 0, k, 3:7] * np.array([w, h, w, h])
(x, y, xe, ye) = box.astype("int")
face_coordinates = (x, y, xe, ye)
x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
gray_face = gray_image[y1: y2, x1: x2]
try:
gray_face = cv2.resize(gray_face, (emotion_target_size))
except:
continue
gray_face = cv2.flip(gray_face, 1)
# gray_face = preprocess_input(gray_face, False)
gray_face = np.expand_dims(gray_face, 0)
gray_face = np.expand_dims(gray_face, -1)
emotion_prediction = emotion_classifier.predict(gray_face)
emotion_probability = np.max(emotion_prediction)
emotion_label_arg = np.argmax(emotion_prediction)
emotion_text = emotion_labels[emotion_label_arg]
emotion_window.append(emotion_text)
if len(emotion_window) > image_window: emotion_window.pop(0)
try:
emotion_mode = mode(emotion_window)
except:
continue
if emotion_text == 'happy':
color = emotion_probability * np.asarray((255, 255, 0))
elif emotion_text == 'angry':
color = emotion_probability * np.asarray((255, 0, 0))
elif emotion_text == 'sad':
color = emotion_probability * np.asarray((0, 0, 255))
elif emotion_text == 'surprise':
color = emotion_probability * np.asarray((0, 255, 255))
else:
color = emotion_probability * np.asarray((0, 255, 0))
color = color.astype(int)
color = color.tolist()
x, y, w, h = face_coordinates
cv2.rectangle(image, (x, y), (xe, ye), color, 3)
# cv2.rectangle(image, (x, y), (x + w, y + h), color, 3)
cv2.putText(image, emotion_mode, (x + 0, y - 80), cv2.FONT_HERSHEY_SIMPLEX,
3, color, 3, cv2.LINE_AA)
# Print a background behind the text.
# bg = np.full((image.shape), (0, 0, 0), dtype = np.uint8)
# cv2.putText(bg, emotion_mode, (x + 0, y - 45), cv2.FONT_HERSHEY_SIMPLEX,
# 1, color, 3, cv2.LINE_AA)
# x1, y1, w1, h1 = cv2.boundingRect(bg[:,:,2])
# print(x1, y1, w1, h1)
# image[y1: y1 + h1, x1: x1 + w1] = bg[y1: y1 + h1, x1: x1 + w1]
cv2.imwrite(os.path.join(savedir or "", f"{file}-detect{extension}"), image)