-
Notifications
You must be signed in to change notification settings - Fork 0
/
exam_servillence.py
93 lines (75 loc) · 2.37 KB
/
exam_servillence.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import dlib
import cv2
import numpy as np
import math
import requests
import time
# import os
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("./datasets/shape_predictor_68_face_landmarks.dat")
# saved_data = np.load("saved_data.npy")
# print(saved_data.shape)
# Name of candidate
name = input("Enter Your Name: ")
startt = time.time() / 60
# tracking status
moving, talking, okay = 0, 0, 0
# initialize record
url = 'http://127.0.0.1:5000/surveillance'
infoo = {"name": name, "moved": moving, "talked": talking}
foo = requests.post(url, json=infoo)
# distance between two points
def dist(point1, point2):
return math.sqrt((point1.x - point2.x) ** 2 + (point1.y - point2.y) ** 2)
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
for face in faces:
landmarks = predictor(gray, face).parts()
for point in landmarks:
cv2.circle(frame, (point.x, point.y), 2, (255, 0, 0), 2)
# points 1, 29, 17 in landmarks reference
# ---------------------------1
r1 = dist(landmarks[0], landmarks[28])
r2 = dist(landmarks[28], landmarks[16])
if math.floor(abs(r1 - r2)) > 9:
# print("Moving", moving)
moving += 1
else:
# print("okay-0")
okay += 1
# mouth open/ close - 63, 67
# ---------------------------2
r3 = dist(landmarks[62], landmarks[66])
# x<3 not talking | x>15 yawning
if math.ceil(3 < r3 < 15):
# print("talking", talking)
talking += 1
else:
# print("okay-1")
okay += 1
# ---------------------------
if ret:
cv2.imshow("My Screen", frame)
key = cv2.waitKey(1)
# -_-_-_-_-
infoo = {"name": name, "moved": moving, "talked": talking}
current = time.time() / 60
time_passed = current - startt # minutes
print(time_passed)
if time_passed >= 0.5:
startt = current
foo = requests.put(url, json=infoo)
# print("Moving: ",moving)
foo = requests.put(url, json=infoo)
# print("Talking: ",talking)
# -_-_-_-_-
if key == ord('q'):
print("Moving: ", moving)
print("talking: ", talking)
print("okay: ", okay)
break
cap.release()
cv2.destroyAllWindows()