-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathface_tracking.py
More file actions
31 lines (24 loc) · 929 Bytes
/
Copy pathface_tracking.py
File metadata and controls
31 lines (24 loc) · 929 Bytes
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
import logging
from deepface import DeepFace
import os
import sys
gender_labels = ['Man', 'Woman']
emotion_labels = ['neutral', 'happy', 'surprise', 'fear', 'sad', 'angry', 'disgust']
class HiddenPrints:
def __enter__(self):
self._original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
pass
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._original_stdout
pass
def get_face(img):
with HiddenPrints():
try:
face = DeepFace.analyze(img, actions=('emotion', 'gender'), enforce_detection=False, silent=True,
detector_backend='mtcnn')[0]
except Exception as e:
logging.warning('Face analysis failed.')
return None
return face['dominant_gender'], face['dominant_emotion'], face['region'], face['emotion'][face['dominant_emotion']]