-
Notifications
You must be signed in to change notification settings - Fork 336
/
mouth_opening_detector.py
67 lines (60 loc) · 2.34 KB
/
mouth_opening_detector.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
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 31 01:04:44 2020
@author: hp
"""
import cv2
from face_detector import get_face_detector, find_faces
from face_landmarks import get_landmark_model, detect_marks, draw_marks
face_model = get_face_detector()
landmark_model = get_landmark_model()
outer_points = [[49, 59], [50, 58], [51, 57], [52, 56], [53, 55]]
d_outer = [0]*5
inner_points = [[61, 67], [62, 66], [63, 65]]
d_inner = [0]*3
font = cv2.FONT_HERSHEY_SIMPLEX
def mouth_opening_detector(video_path):
cap = cv2.VideoCapture(video_path)
while(True):
ret, img = cap.read()
rects = find_faces(img, face_model)
for rect in rects:
shape = detect_marks(img, landmark_model, rect)
draw_marks(img, shape)
cv2.putText(img, 'Press r to record Mouth distances', (30, 30), font,
1, (0, 255, 255), 2)
cv2.imshow("Output", img)
if cv2.waitKey(1) & 0xFF == ord('r'):
for i in range(100):
for i, (p1, p2) in enumerate(outer_points):
d_outer[i] += shape[p2][1] - shape[p1][1]
for i, (p1, p2) in enumerate(inner_points):
d_inner[i] += shape[p2][1] - shape[p1][1]
break
cv2.destroyAllWindows()
d_outer[:] = [x / 100 for x in d_outer]
d_inner[:] = [x / 100 for x in d_inner]
while(True):
ret, img = cap.read()
rects = find_faces(img, face_model)
for rect in rects:
shape = detect_marks(img, landmark_model, rect)
cnt_outer = 0
cnt_inner = 0
draw_marks(img, shape[48:])
for i, (p1, p2) in enumerate(outer_points):
if d_outer[i] + 3 < shape[p2][1] - shape[p1][1]:
cnt_outer += 1
for i, (p1, p2) in enumerate(inner_points):
if d_inner[i] + 2 < shape[p2][1] - shape[p1][1]:
cnt_inner += 1
if cnt_outer > 3 and cnt_inner > 2:
print('Mouth open')
cv2.putText(img, 'Mouth open', (30, 30), font,
1, (0, 255, 255), 2)
# show the output image with the face detections + facial landmarks
cv2.imshow("Output", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()