Pasit commited on
Commit
f247b8c
·
1 Parent(s): fc850c7

initial commit

Browse files
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ from insightface.app import FaceAnalysis
4
+ from hsemotion_onnx.facial_emotions import HSEmotionRecognizer
5
+
6
+
7
+ def facial_emotion_recognition(img):
8
+
9
+ # Get the faces from the model
10
+ faces = face_detector.get(img)
11
+
12
+ if len(faces) > 0:
13
+
14
+ # Put the detected faces in the queue
15
+ highest_score_box = (0, 0, 0, 0) # x, y, w, h
16
+ highest_score = 0
17
+
18
+ for face in faces:
19
+ if face['det_score'] > highest_score:
20
+ highest_score = face['det_score']
21
+ x1, y1, x2, y2 = face['bbox'].astype(int)
22
+ x_margin = int((x2 - x1) * face_margin)
23
+ y_margin = int((y2 - y1) * face_margin)
24
+ x = max(0, x1 - x_margin)
25
+ y = max(0, y1 - y_margin)
26
+ w = min(x2 + x_margin, img.shape[1]) - x
27
+ h = min(y2 + y_margin, img.shape[0]) - y
28
+ highest_score_box = (x, y, w, h)
29
+
30
+ x, y, w, h = highest_score_box
31
+ emotion, _ = hse_emo_model.predict_emotions(img[y:y+h, x:x+w], logits=True)
32
+
33
+ cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)
34
+ cv2.putText(img, emotion, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
35
+
36
+ return img
37
+
38
+ face_margin = 0.1
39
+
40
+ # Load the face detector
41
+ model_name = 'buffalo_sc'
42
+ face_detector = FaceAnalysis(name=model_name, allowed_modules=['detection'], providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
43
+ face_detector.prepare(ctx_id=0, det_size=(640, 640))
44
+
45
+ # Load HSE emotion
46
+ hse_emo_model = HSEmotionRecognizer(model_name='enet_b0_8_best_vgaf')
47
+
48
+ webcam = gr.Image(image_mode='RGB', type='numpy', source='webcam', label='Input Image')
49
+ output = gr.Image(image_mode='RGB', type='numpy', label='Output Image')
50
+ app = gr.Interface(facial_emotion_recognition, inputs=webcam, outputs=output)
51
+ app.launch()
buffalo_sc/det_500m.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5e4447f50245bbd7966bd6c0fa52938c61474a04ec7def48753668a9d8b4ea3a
3
+ size 2524817
buffalo_sc/w600k_mbf.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9cc6e4a75f0e2bf0b1aed94578f144d15175f357bdc05e815e5c4a02b319eb4f
3
+ size 13616099
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ opencv-python
2
+ insightface
3
+ hsemotion_onnx
4
+ gradio