-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface_detection.py
More file actions
124 lines (85 loc) · 3.39 KB
/
Copy pathface_detection.py
File metadata and controls
124 lines (85 loc) · 3.39 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import cv2
import mediapipe as mp
import numpy as np
import time
from mss import mss
import streamlit as st
from screeninfo import get_monitors
from mesh_direct import mesh_direct
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(min_detection_confidence=0.3, min_tracking_confidence=0.3, max_num_faces=1)
mp_face_detection = mp.solutions.face_detection
face_detection = mp_face_detection.FaceDetection(
model_selection=1, min_detection_confidence=0.1)
mp_drawing = mp.solutions.drawing_utils
# Gets the dimensions of monitors. You can set which monitor to use here.
monitors = [m for m in get_monitors()]
first_monitor = monitors[0]
bounding_box = {'top': 0, 'left': 0, 'width': first_monitor.width, 'height': first_monitor.height}
sct = mss()
st.title("ReadTheRoom")
output = st.empty()
# points include:
# LEFT_EAR_TRAGION
# LEFT_EYE
# MOUTH_CENTRE
# NOSE TIP
# RIGHT_EAR_TRAGION
# RIGHT_EYE
def calc_distance(p1, p2):
"""
Basic Euclidean Distance Calculation, while changing the data-types of input.
"""
x1 = float(p1.x)
x2 = float(p2.x)
y1 = float(p1.y)
y2 = float(p2.y)
return np.sqrt(((x2 - x1)**2) + ((y2 - y1)**2))
def calc_attention(left_eye_nose, right_eye_nose, left_ear_nose, right_ear_nose):
eye_gaps = min(left_eye_nose, right_eye_nose)
ear_gaps = min(left_ear_nose, right_ear_nose)
# if the ear-eye value is smaller than this fraction, you can assume someone is looking to the side.
# This value was dialed in manually.
if eye_gaps > ear_gaps:
return 1
else:
return 0
while True:
image = np.array(sct.grab(bounding_box))
start = time.time()
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = face_detection.process(image)
# Convert the color space from RGB to BGR
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
detection_list = []
if results.detections:
for detection in results.detections:
mp_drawing.draw_detection(image, detection)
detection_list.append(detection)
#You have to get a count of total number of face objects
# You have to calculate a total
people_list = []
if len(detection_list) > 0:
for i in detection_list:
box = i.location_data.relative_bounding_box
mini_box = {'top': round(box.ymin * (first_monitor.height * 1) - 100), 'left' : round(box.xmin * (first_monitor.width * 1) - 100),
'width': round(box.width * ((first_monitor.width * 1.10) + 200)), 'height': round(box.height * ((first_monitor.height * 1.10) + 200))}
attention = mesh_direct(face_mesh, sct, mini_box)
if attention is not None:
people_list.append(attention)
if len(people_list) > 0:
paying_attention = sum(people_list)
total_faces= len(people_list)
else:
paying_attention = 0
total_faces = 0
# cv2.imshow('Face Detection', image)
attention_status = "Paying Attention: " + str(paying_attention) + "\n Total: " + str(total_faces)
# attention_status_list = [float(paying_attention), float(total_faces)]
with output.container():
st.write(attention_status)
#print("paying attention: " + str(paying_attention) + "\n Total: " + str(total_faces))
if cv2.waitKey(5) & 0xFF == 27:
break
cv2.destroyAllWindows()