-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
442 lines (347 loc) · 15.1 KB
/
Copy pathfunctions.py
File metadata and controls
442 lines (347 loc) · 15.1 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import cv2
import mediapipe as mp
import time
import numpy as np
from numpy import greater
import utils
import math
import pandas as pd
import pyttsx3
from ultralytics import YOLO
from ultralytics import YOLOWorld
import cvzone
from datetime import datetime, timedelta
# variables for direction alert
change_dir_counter = 0
dir_warning_counter = 0
vis_warning_counter = 0
warning_count = 0
visibility_counter = 0
# variables
frame_counter =0
TOTAL_BLINKS =0
frame_counter =0
# constants
CLOSED_EYES_FRAME =1
FONTS =cv2.FONT_HERSHEY_COMPLEX
map_face_mesh = mp.solutions.face_mesh
face_mesh = mp.solutions.face_mesh.FaceMesh(refine_landmarks=True)
mp_drawing = mp.solutions.drawing_utils
drawing_spec = mp_drawing.DrawingSpec(color=(255, 255, 255),thickness=1,circle_radius=1)
start_time = time.time()
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
# landmark detection function
def landmarksDetection(img, results, draw=False):
img_height, img_width= img.shape[:2]
# list[(x,y), (x,y)....]
mesh_coord = [(int(point.x * img_width), int(point.y * img_height)) for point in results.multi_face_landmarks[0].landmark]
if draw :
[cv2.circle(img, p, 2, utils.GREEN, -1) for p in mesh_coord]
# returning the list of tuples for each landmarks
return mesh_coord
# Euclaidean distance
def euclaideanDistance(point, point1):
x, y = point
x1, y1 = point1
distance = math.sqrt((x1 - x)**2 + (y1 - y)**2)
return distance
# Blinking Ratio
def blinkRatio(img, landmarks, right_indices, left_indices):
# Right eyes
# horizontal line
rh_right = landmarks[right_indices[0]]
rh_left = landmarks[right_indices[8]]
# vertical line
rv_top = landmarks[right_indices[12]]
rv_bottom = landmarks[right_indices[4]]
# LEFT_EYE
# horizontal line
lh_right = landmarks[left_indices[0]]
lh_left = landmarks[left_indices[8]]
# vertical line
lv_top = landmarks[left_indices[12]]
lv_bottom = landmarks[left_indices[4]]
rhDistance = euclaideanDistance(rh_right, rh_left)
rvDistance = euclaideanDistance(rv_top, rv_bottom)
lvDistance = euclaideanDistance(lv_top, lv_bottom)
lhDistance = euclaideanDistance(lh_right, lh_left)
reRatio = rhDistance/rvDistance
leRatio = lhDistance/lvDistance
ratio = (reRatio+leRatio)/2
return ratio
def direction_estimator_1(extreme_right_circle_right_eye, extreme_left_circle_right_eye, gaze_center, l_eye_threshold, r_eye_threshold):
# input :- takes 3 tuples
# output :- returns the direction
dist_gaze_and_rightOfRight = extreme_right_circle_right_eye[0] - gaze_center[0]
dist_gaze_and_leftOfRight = gaze_center[0] - extreme_left_circle_right_eye[0]
eye_width = extreme_right_circle_right_eye[0] - extreme_left_circle_right_eye[0]
if dist_gaze_and_rightOfRight < (eye_width * r_eye_threshold):
direction = "Right"
elif dist_gaze_and_leftOfRight < (eye_width * l_eye_threshold):
direction = "Left"
else:
direction = "Center"
return direction
def direction_estimator_2(r_eye_pts, gaze_center):
distance = {}
for i in range(0, 16):
dist = abs(gaze_center[0] - r_eye_pts[i][0])
distance[i] = dist
top_5_smallest = sorted(distance.items(), key=lambda x: x[1])[:5]
print(top_5_smallest)
keys = [item[0] for item in top_5_smallest]
required_keys_for_left = {2, 3, 13, 14}
required_keys_for_right = {6, 7, 10, 11}
if required_keys_for_left.issubset(keys):
direction = "Left"
elif required_keys_for_right.issubset(keys):
direction = "Right"
else:
direction = "Center"
return direction
def points_on_circle(center, radius, num_points):
points = []
for i in range(num_points):
angle = i * (2 * np.pi / num_points)
x = int(center[0] + radius * np.cos(angle))
y = int(center[1] + radius * np.sin(angle))
points.append((x, y))
return points
def draw_sharingan(frame, center, radius):
sharingan_clr = (19, 19, 175)
black_clr = (9, 9, 9)
cv2.circle(frame, center, radius, black_clr, int(radius * 0.125))
# Extra cirlces just for fun
cv2.circle(frame, center, int(radius * 0.875), sharingan_clr, int(radius * 0.15))
cv2.circle(frame, center, int(radius * 0.725), sharingan_clr, int(radius * 0.15)) # sharingan points
cv2.circle(frame, center, int(radius * 0.575), sharingan_clr, int(radius * 0.15))
cv2.circle(frame, center, int(radius * 0.425), black_clr, int(radius * 0.1))
cv2.circle(frame, center, int(radius * 0.325), sharingan_clr, -1)
# Get points on the border of the circle (sharingan points)
border_points = points_on_circle(center, int(radius * 0.5), 3)
# Draw the sharingan points
for point in border_points:
cv2.circle(frame, point, int(radius * 0.075) + 1, black_clr, -1)
def draw_mesh(frame, r_eye_pts, gaze_center):
distance = {}
for i in range(0, 16):
dist = abs(gaze_center[0] - r_eye_pts[i][0])
cv2.line(frame, gaze_center, r_eye_pts[i], (19, 19, 175), 1)
distance[i] = dist
def eye_track(ret, frame, rgb_frame, results):
global frame_counter, CEF_COUNTER, TOTAL_BLINKS, frame_counter, eye_points
# Left eyes indices
LEFT_EYE =[ 362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385,384, 398 ]
# right eyes indices
RIGHT_EYE=[ 33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161 , 246 ]
frame_counter +=1 # frame counter
mesh_coords = landmarksDetection(frame, results, False)
l_eye_pts = []
for i in range(0, 16):
pt = mesh_coords[RIGHT_EYE[i]]
l_eye_pts.append(pt)
# Draw a line connecting all points
pts_array = np.array(l_eye_pts, np.int32)
pts_array = pts_array.reshape((-1, 1, 2))
r_eye_pts = []
for i in range(0, 16):
pt = mesh_coords[LEFT_EYE[i]]
r_eye_pts.append(pt)
# Draw a line connecting all points
pts_array = np.array(r_eye_pts, np.int32)
pts_array = pts_array.reshape((-1, 1, 2))
ratio = blinkRatio(frame, mesh_coords, RIGHT_EYE, LEFT_EYE)
# if ratio > 5.5:
# counter_threshold +=1
# else:
# if counter_threshold > CLOSED_EYES_FRAME:
# TOTAL_BLINKS +=1
# counter_threshold = 0
frame_h, frame_w, _ = frame.shape
output = face_mesh.process(rgb_frame)
landmark_points = output.multi_face_landmarks
if landmark_points:
landmarks = landmark_points[0].landmark
# Get coordinates of the four points around the eye
eye_points = [(int(landmarks[i].x * frame_w), int(landmarks[i].y * frame_h)) for i in range(474, 478)]
# Draw circle approximating the eye
if len(eye_points) == 4:
center, radius = cv2.minEnclosingCircle(np.array(eye_points))
center = (int(center[0]), int(center[1]))
radius = int(radius * 0.75)
# draw_sharingan(frame, center, radius)
direction = direction_estimator_1(r_eye_pts[8], r_eye_pts[0], center, 0.4, 0.3)
return direction
def head_pose(frame, results):
img_h, img_w, Img_c = frame.shape
face_3d = []
face_2d = []
for face_landmarks in results.multi_face_landmarks:
for idx, lm in enumerate(face_landmarks.landmark):
if idx == 33 or idx == 263 or idx == 1 or idx == 61 or idx == 291 or idx == 199:
if idx == 1:
nose_2d = (lm.x * img_w, lm.y * img_h)
nose_3d = (lm.x * img_w, lm.y * img_h, lm.z * 3000)
x, y = int(lm.x * img_w), int(lm.y * img_h)
face_2d.append([x, y])
face_3d.append([x, y, lm.z])
face_2d = np.array(face_2d, dtype=np.float64)
face_3d = np.array(face_3d, dtype=np.float64)
focal_length = 1 * img_w
cam_matrix = np.array([[focal_length, 0, img_h / 2],
[0, focal_length, img_w / 2],
[0, 0, 1]])
dist_matrix = np.zeros((4, 1), dtype=np.float64)
success, rot_vec, trans_vec = cv2.solvePnP(face_3d, face_2d, cam_matrix, dist_matrix)
rmat, jac = cv2.Rodrigues(rot_vec)
angles, mtxR, mtxQ, Qx, Qy, Qz = cv2.RQDecomp3x3(rmat)
x = angles[0] * 360
y = angles[1] * 360
z = angles[2] * 360
if y < -10:
text = "Left"
elif y > 10:
text = "Right"
elif x < -18:
text = "Down"
elif x > 15:
text = "Up"
else:
text = "Center"
return text
def calculate_distance(distance_pixel, distance_cm, success, image):
# get correlation coefficients
coff = np.polyfit(distance_pixel, distance_cm, 2)
# perform face detection
mp_face_detection = mp.solutions.face_detection.FaceDetection(
model_selection=0, min_detection_confidence=0.75)
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = mp_face_detection.process(image)
bbox_list, eyes_list = [], []
if results.detections:
for detection in results.detections:
# get bbox data
bboxc = detection.location_data.relative_bounding_box
ih, iw, ic = image.shape
bbox = int(bboxc.xmin * iw), int(bboxc.ymin * ih), int(bboxc.width * iw), int(bboxc.height * ih)
bbox_list.append(bbox)
# get the eyes landmark
left_eye = detection.location_data.relative_keypoints[0]
right_eye = detection.location_data.relative_keypoints[1]
eyes_list.append([(int(left_eye.x * iw), int(left_eye.y * ih)),
(int(right_eye.x * iw), int(right_eye.y * ih))])
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
for bbox, eye in zip(bbox_list, eyes_list):
# calculate distance between left and right eye
dist_between_eyes = np.sqrt(
(eye[0][1] - eye[1][1]) ** 2 + (eye[0][0] - eye[1][0]) ** 2)
# calculate distance in cm
a, b, c = coff
distance_cm = a * dist_between_eyes ** 2 + b * dist_between_eyes + c
distance_cm -= 0
return distance_cm
alerts = {"visibility": ["Attention: Your face is not visible to the camera."],
"direction": ["Alert: It seems you are not facing the camera."],
"object": ["Warning: An important object has been detected."] }
################################################################################################################################################
model = YOLO('yolov8s.pt')
# Define the class names
classNames = [
"person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
"dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
"handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
"baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
"fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
"carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
"diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
"microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
"teddy bear", "hair drier", "toothbrush"
]
# Define the desired features
desired_features = ["person", "book", "cell phone"]
# Initialize the timer
alert_timer = 0
alert_triggered = False
# Object detection function with simplified calculations
def obj_detect(ret, image):
global alert_timer, alert_triggered, start_time
results = model.predict(image, device='cpu')
count = [0] * len(desired_features) # Initialize count for desired features
for r in results:
boxes = r.boxes
for box in boxes:
cls_id = int(box.cls[0]) # Get the class ID
class_name = classNames[cls_id]
if class_name in desired_features:
count[desired_features.index(class_name)] += 1 # Increment count for detected class
# Calculate FPS based on time elapsed
end = time.time()
totalTime = end - start_time
fps = 1 / totalTime if totalTime > 0 else 0
start_time = end
# Trigger alert if thresholds are exceeded
if count[0] > 1 or count[1] > 0 or count[2] > 0:
alert_timer += 1
if alert_timer > 15:
alert_triggered = True
alert_timer = 0
return False # Alert triggered
return True # No alert
################################################################################################################################################
def run(camera):
global change_dir_counter, start_time, dir_warning_counter, visibility_counter, vis_warning_counter, warning_count, alerts
ret, frame = camera.read()
frame = cv2.flip(frame, 1) # Flip the frame horizontally
frame = cv2.resize(frame, None, fx=1.5, fy=1.5, interpolation=cv2.INTER_CUBIC) # Resize for uniform input
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # Convert frame to RGB
results = face_mesh.process(rgb_frame) # Process the frame for face landmarks
direction, head_direction = '', ''
obj_d = True
fps = 0
# If face landmarks are detected
if results.multi_face_landmarks:
head_direction = head_pose(rgb_frame, results) # Get head direction
if head_direction in ["Center", "Up"]:
eye_direction = eye_track(ret, frame, rgb_frame, results) # Track eye direction
direction = eye_direction
else:
direction = head_direction
# Calculate FPS
end = time.time()
totalTime = end - start_time
fps = 1 / totalTime if totalTime > 0 else 0
start_time = end
# Monitor direction changes
if direction in ["Right", "Left", "Up"]:
change_dir_counter += 1
if change_dir_counter > 20:
change_dir_counter = 0
dir_warning_counter += 1
warning_count += 1
return False, direction, head_direction, fps, obj_d, alerts["direction"][0]
return True, direction, head_direction, fps, obj_d, None
else:
obj_d = obj_detect(ret, frame)
if not obj_d:
return False, direction, head_direction, fps, obj_d, alerts["object"][0]
return True, direction, head_direction, fps, obj_d, None
else:
# If no face detected, increment visibility counter
end = time.time()
totalTime = end - start_time
fps = 1 / totalTime if totalTime > 0 else 0
start_time = end
visibility_counter += 1
if visibility_counter > 20:
visibility_counter = 0
change_dir_counter = 0
vis_warning_counter += 1
warning_count += 1
return False, direction, head_direction, fps, obj_d, alerts["visibility"][0]
return True, direction, head_direction, fps, obj_d, None