Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
vardanagarwal
GitHub Repository: vardanagarwal/Proctoring-AI
Path: blob/master/mouth_opening_detector.py
453 views
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Fri Jul 31 01:04:44 2020
4
5
@author: hp
6
"""
7
8
import cv2
9
from face_detector import get_face_detector, find_faces
10
from face_landmarks import get_landmark_model, detect_marks, draw_marks
11
face_model = get_face_detector()
12
landmark_model = get_landmark_model()
13
outer_points = [[49, 59], [50, 58], [51, 57], [52, 56], [53, 55]]
14
d_outer = [0]*5
15
inner_points = [[61, 67], [62, 66], [63, 65]]
16
d_inner = [0]*3
17
font = cv2.FONT_HERSHEY_SIMPLEX
18
19
20
def mouth_opening_detector(video_path):
21
cap = cv2.VideoCapture(video_path)
22
23
while(True):
24
ret, img = cap.read()
25
rects = find_faces(img, face_model)
26
for rect in rects:
27
shape = detect_marks(img, landmark_model, rect)
28
draw_marks(img, shape)
29
cv2.putText(img, 'Press r to record Mouth distances', (30, 30), font,
30
1, (0, 255, 255), 2)
31
cv2.imshow("Output", img)
32
if cv2.waitKey(1) & 0xFF == ord('r'):
33
for i in range(100):
34
for i, (p1, p2) in enumerate(outer_points):
35
d_outer[i] += shape[p2][1] - shape[p1][1]
36
for i, (p1, p2) in enumerate(inner_points):
37
d_inner[i] += shape[p2][1] - shape[p1][1]
38
break
39
cv2.destroyAllWindows()
40
d_outer[:] = [x / 100 for x in d_outer]
41
d_inner[:] = [x / 100 for x in d_inner]
42
43
while(True):
44
ret, img = cap.read()
45
rects = find_faces(img, face_model)
46
for rect in rects:
47
shape = detect_marks(img, landmark_model, rect)
48
cnt_outer = 0
49
cnt_inner = 0
50
draw_marks(img, shape[48:])
51
for i, (p1, p2) in enumerate(outer_points):
52
if d_outer[i] + 3 < shape[p2][1] - shape[p1][1]:
53
cnt_outer += 1
54
for i, (p1, p2) in enumerate(inner_points):
55
if d_inner[i] + 2 < shape[p2][1] - shape[p1][1]:
56
cnt_inner += 1
57
if cnt_outer > 3 and cnt_inner > 2:
58
print('Mouth open')
59
cv2.putText(img, 'Mouth open', (30, 30), font,
60
1, (0, 255, 255), 2)
61
# show the output image with the face detections + facial landmarks
62
cv2.imshow("Output", img)
63
if cv2.waitKey(1) & 0xFF == ord('q'):
64
break
65
66
cap.release()
67
cv2.destroyAllWindows()
68
69