Update utils/emotion_detection_brainai.py
Browse files
utils/emotion_detection_brainai.py
CHANGED
|
@@ -12,8 +12,15 @@ class EmotionModel:
|
|
| 12 |
def __init__(self):
|
| 13 |
self.face_compiled_model, self.face_input_layer, self.face_output_layer = self.load_model('face-detection-adas-0001')
|
| 14 |
self.emotion_compiled_model, self.emotion_input_layer, self.emotion_output_layer = self.load_model('emotions-recognition-retail-0003')
|
| 15 |
-
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def load_model(self, model_name):
|
| 18 |
model_path = "models/" + model_name + ".xml"
|
| 19 |
core = ov.Core()
|
|
@@ -32,6 +39,11 @@ class EmotionModel:
|
|
| 32 |
|
| 33 |
return input_img
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def post_process_face(self, result_face, img, conf=0.5):
|
| 37 |
boxes = []
|
|
@@ -49,19 +61,19 @@ class EmotionModel:
|
|
| 49 |
|
| 50 |
return boxes
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def post_process_emotion(self, result_emotion, img, face_position):
|
| 53 |
|
| 54 |
-
emotions = {
|
| 55 |
-
0:"neutral",
|
| 56 |
-
1:"happy",
|
| 57 |
-
2:"sad",
|
| 58 |
-
3:"surprise",
|
| 59 |
-
4:"anger"
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
predictions = result_emotion[0,:,0,0]
|
| 63 |
topresult_index = np.argmax(predictions)
|
| 64 |
-
emotion = emotions[topresult_index]
|
| 65 |
|
| 66 |
font_size = img.shape[0]/1000
|
| 67 |
font_thickness = int(img.shape[0]/500)
|
|
@@ -82,19 +94,9 @@ class EmotionModel:
|
|
| 82 |
uploaded_img = PIL.Image.open(img)
|
| 83 |
uploaded_img_cv = np.array(uploaded_img)
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
if boxes is not None:
|
| 90 |
-
|
| 91 |
-
for box in boxes:
|
| 92 |
-
xmin, ymin, xmax, ymax = box
|
| 93 |
-
emotion_input = uploaded_img_cv[ymin:ymax,xmin:xmax]
|
| 94 |
-
input_img = self.preprocess(emotion_input, self.emotion_input_layer)
|
| 95 |
-
result_emotion = self.emotion_compiled_model([input_img])[self.emotion_output_layer]
|
| 96 |
-
self.post_process_emotion(result_emotion, uploaded_img_cv, box)
|
| 97 |
-
|
| 98 |
return uploaded_img_cv
|
| 99 |
|
| 100 |
emotion_model = EmotionModel()
|
|
|
|
| 12 |
def __init__(self):
|
| 13 |
self.face_compiled_model, self.face_input_layer, self.face_output_layer = self.load_model('face-detection-adas-0001')
|
| 14 |
self.emotion_compiled_model, self.emotion_input_layer, self.emotion_output_layer = self.load_model('emotions-recognition-retail-0003')
|
|
|
|
| 15 |
|
| 16 |
+
self.emotions = {
|
| 17 |
+
0:"neutral",
|
| 18 |
+
1:"happy",
|
| 19 |
+
2:"sad",
|
| 20 |
+
3:"surprise",
|
| 21 |
+
4:"anger"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
def load_model(self, model_name):
|
| 25 |
model_path = "models/" + model_name + ".xml"
|
| 26 |
core = ov.Core()
|
|
|
|
| 39 |
|
| 40 |
return input_img
|
| 41 |
|
| 42 |
+
def detect_faces(self, img, conf = 0.5)
|
| 43 |
+
result_face = self.face_compiled_model([input_img])[self.face_output_layer]
|
| 44 |
+
boxes = self.post_process_face(result_face, uploaded_img_cv, conf=0.5)
|
| 45 |
+
|
| 46 |
+
return boxes
|
| 47 |
|
| 48 |
def post_process_face(self, result_face, img, conf=0.5):
|
| 49 |
boxes = []
|
|
|
|
| 61 |
|
| 62 |
return boxes
|
| 63 |
|
| 64 |
+
def detect_emotions(self, img, boxes)
|
| 65 |
+
for box in boxes:
|
| 66 |
+
xmin, ymin, xmax, ymax = box
|
| 67 |
+
emotion_input = uploaded_img_cv[ymin:ymax,xmin:xmax]
|
| 68 |
+
input_img = self.preprocess(emotion_input, self.emotion_input_layer)
|
| 69 |
+
result_emotion = self.emotion_compiled_model([input_img])[self.emotion_output_layer]
|
| 70 |
+
self.post_process_emotion(result_emotion, uploaded_img_cv, box)
|
| 71 |
+
|
| 72 |
def post_process_emotion(self, result_emotion, img, face_position):
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
predictions = result_emotion[0,:,0,0]
|
| 75 |
topresult_index = np.argmax(predictions)
|
| 76 |
+
emotion = self.emotions[topresult_index]
|
| 77 |
|
| 78 |
font_size = img.shape[0]/1000
|
| 79 |
font_thickness = int(img.shape[0]/500)
|
|
|
|
| 94 |
uploaded_img = PIL.Image.open(img)
|
| 95 |
uploaded_img_cv = np.array(uploaded_img)
|
| 96 |
|
| 97 |
+
boxes = detect_faces(uploaded_img_cv, conf = 0.5)
|
| 98 |
+
detect_emotions(uploaded_img_cv)
|
| 99 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
return uploaded_img_cv
|
| 101 |
|
| 102 |
emotion_model = EmotionModel()
|