-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (52 loc) · 1.86 KB
/
main.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
# Facial Expression Recognition & Analyzer and Report
import cv2
from deepface import DeepFace
emotions = {
"angry": 0,
"disgust": 0,
"fear": 0,
"happy": 0,
"sad": 0,
"surprise": 0,
"neutral": 0
}
def start_analyzing_mimic():
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(1)
if not cap.isOpened():
cap = cv2.VideoCapture(0)
if not cap.isOpened():
raise IOError("No camera detected")
while True:
ret, frame = cap.read()
result_analyzer = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
font = cv2.FONT_HERSHEY_PLAIN
print('Dominant Facial Expression {0}'.format(result_analyzer['dominant_emotion']))
analyze_expressions(result_analyzer['emotion'])
cv2.putText(frame, result_analyzer['dominant_emotion'], (50, 50), font, 3, (0, 0, 255), 2, cv2.LINE_4)
cv2.imshow('Original video', frame)
if cv2.waitKey(2) & 0xFF == ord('q'):
report_expressions()
break
cap.release()
cv2.destroyAllWindows()
def analyze_expressions(emotion):
for key in emotion:
emotions[key] = emotions[key] + emotion[key];
def report_expressions():
total = 0;
print("##### Facial Expression Report #####")
print(emotions)
for key in emotions:
total += emotions[key];
for key in emotions:
percentage = emotions[key] * 100 / total;
#print(key)
#print(percentage)
print('{0} => % {1}'.format(key, round(percentage, 2)))
if __name__ == '__main__':
start_analyzing_mimic()