Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -1,114 +1,119 @@
|
|
1 |
-
import math
|
2 |
-
import numpy as np
|
3 |
-
import cv2 as cv
|
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 |
-
# Initialize
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import numpy as np
|
3 |
+
import cv2 as cv
|
4 |
+
|
5 |
+
LEFT_EYE = [362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385, 384, 398]
|
6 |
+
RIGHT_EYE = [33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161, 246]
|
7 |
+
LEFT_IRIS = [474, 475, 476, 477]
|
8 |
+
RIGHT_IRIS = [469, 470, 471, 472]
|
9 |
+
|
10 |
+
def valid_float(n):
|
11 |
+
if not n.isfloat():
|
12 |
+
raise argparse.ArgumentTypeError('Invalid integer value: {}'.format(n))
|
13 |
+
return float(n)
|
14 |
+
|
15 |
+
|
16 |
+
def euclaideanDistance(point, point1):
|
17 |
+
x, y = point
|
18 |
+
x1, y1 = point1
|
19 |
+
distance = math.sqrt((x1 - x)**2 + (y1 - y)**2)
|
20 |
+
return distance
|
21 |
+
|
22 |
+
# Blinking Ratio
|
23 |
+
def blinkRatio(img, landmarks, right_indices, left_indices):
|
24 |
+
# Right eyes
|
25 |
+
# horizontal line
|
26 |
+
rh_right = landmarks[right_indices[0]]
|
27 |
+
rh_left = landmarks[right_indices[8]]
|
28 |
+
# vertical line
|
29 |
+
rv_top = landmarks[right_indices[12]]
|
30 |
+
rv_bottom = landmarks[right_indices[4]]
|
31 |
+
# draw lines on right eyes
|
32 |
+
# cv.line(img, rh_right, rh_left, utils.GREEN, 2)
|
33 |
+
# cv.line(img, rv_top, rv_bottom, utils.WHITE, 2) # LEFT_EYE
|
34 |
+
# horizontal line
|
35 |
+
lh_right = landmarks[left_indices[0]]
|
36 |
+
lh_left = landmarks[left_indices[8]] # vertical line
|
37 |
+
lv_top = landmarks[left_indices[12]]
|
38 |
+
lv_bottom = landmarks[left_indices[4]] # Finding Distance Right Eye
|
39 |
+
rhDistance = euclaideanDistance(rh_right, rh_left)
|
40 |
+
rvDistance = euclaideanDistance(rv_top, rv_bottom)
|
41 |
+
# Finding Distance Left Eye
|
42 |
+
lvDistance = euclaideanDistance(lv_top, lv_bottom)
|
43 |
+
lhDistance = euclaideanDistance(lh_right, lh_left) # Finding ratio of LEFT and Right Eyes
|
44 |
+
reRatio=0.0
|
45 |
+
leRatio=0.0
|
46 |
+
if (rvDistance > 0.0) & (lvDistance > 0.0):
|
47 |
+
reRatio = rhDistance/rvDistance
|
48 |
+
leRatio = lhDistance/lvDistance
|
49 |
+
|
50 |
+
ratio = (reRatio+leRatio)/2
|
51 |
+
return ratio, reRatio, leRatio
|
52 |
+
|
53 |
+
def process_frame(frame, overlay, LEFT_EYE, RIGHT_EYE, LEFT_IRIS, RIGHT_IRIS,
|
54 |
+
mp_face_mesh, min_detection_confidence, min_tracking_confidence,alpha):
|
55 |
+
with mp_face_mesh.FaceMesh(
|
56 |
+
max_num_faces=1,
|
57 |
+
refine_landmarks=True,
|
58 |
+
min_detection_confidence=min_detection_confidence,
|
59 |
+
min_tracking_confidence=min_tracking_confidence
|
60 |
+
) as face_mesh:
|
61 |
+
# Convert frame to RGB
|
62 |
+
rgb_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
|
63 |
+
# Convert RGB frame to RGBA
|
64 |
+
rgba_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGBA)
|
65 |
+
# Get frame dimensions
|
66 |
+
height, width = rgba_frame.shape[:2]
|
67 |
+
# Process frame with face mesh
|
68 |
+
results = face_mesh.process(rgb_frame)
|
69 |
+
if results.multi_face_landmarks:
|
70 |
+
# Initialize overlay with zeros
|
71 |
+
zero_overlay = np.zeros_like(rgba_frame)
|
72 |
+
# Get mesh points
|
73 |
+
mesh_points = np.array([np.multiply([p.x, p.y],
|
74 |
+
[width, height]).astype(int) for p in results.multi_face_landmarks[0].landmark])
|
75 |
+
# Initialize iris masks
|
76 |
+
iris_mask_left = np.zeros(rgba_frame.shape, dtype=np.uint8)
|
77 |
+
iris_mask_right = np.zeros(rgba_frame.shape, dtype=np.uint8)
|
78 |
+
# Get blink ratio
|
79 |
+
_, re_ratio, le_ratio = blinkRatio(rgb_frame, mesh_points, RIGHT_EYE, LEFT_EYE)
|
80 |
+
# Get iris centers and radii
|
81 |
+
(l_cx, l_cy), l_radius = cv.minEnclosingCircle(mesh_points[LEFT_IRIS])
|
82 |
+
(r_cx, r_cy), r_radius = cv.minEnclosingCircle(mesh_points[RIGHT_IRIS])
|
83 |
+
center_left = (int(l_cx), int(l_cy))
|
84 |
+
center_right = (int(r_cx), int(r_cy))
|
85 |
+
# Draw circles on iris masks
|
86 |
+
cv.circle(iris_mask_left, center_left, int(l_radius), (255, 0, 0, 255), -1, cv.LINE_AA)
|
87 |
+
cv.circle(iris_mask_right, center_right, int(r_radius), (255, 0, 0, 255), -1, cv.LINE_AA)
|
88 |
+
# Get bounding box sizes
|
89 |
+
bbx_size_l = int((l_radius * 2) / 2)
|
90 |
+
bbx_size_r = int((r_radius * 2) / 2)
|
91 |
+
# Resize overlay
|
92 |
+
resized_overlay_l = cv.resize(overlay, (bbx_size_l * 2, bbx_size_l * 2), interpolation=cv.INTER_CUBIC)
|
93 |
+
resized_overlay_r = cv.resize(overlay, (bbx_size_r * 2, bbx_size_r * 2), interpolation=cv.INTER_CUBIC)
|
94 |
+
# Get bounding box coordinates
|
95 |
+
y1_r = center_right[1] - bbx_size_r
|
96 |
+
y2_r = center_right[1] + bbx_size_r
|
97 |
+
x1_r = center_right[0] - bbx_size_r
|
98 |
+
x2_r = center_right[0] + bbx_size_r
|
99 |
+
y1_l = center_left[1] - bbx_size_l
|
100 |
+
y2_l = center_left[1] + bbx_size_l
|
101 |
+
x1_l = center_left[0] - bbx_size_l
|
102 |
+
x2_l = center_left[0] + bbx_size_l
|
103 |
+
# Add resized overlay to zero overlay if conditions are met
|
104 |
+
if (resized_overlay_l.shape == zero_overlay[y1_l:y2_l, x1_l:x2_l].shape) & (le_ratio < 5.0) & (le_ratio > 2.0):
|
105 |
+
zero_overlay[y1_l:y2_l, x1_l:x2_l] = resized_overlay_l
|
106 |
+
if (resized_overlay_r.shape == zero_overlay[y1_r:y2_r, x1_r:x2_r].shape) & (re_ratio < 5.0) & (re_ratio > 2.0):
|
107 |
+
zero_overlay[y1_r:y2_r, x1_r:x2_r] = resized_overlay_r
|
108 |
+
# Initialize eye masks
|
109 |
+
eye_mask_left = np.zeros(rgba_frame.shape, dtype=np.uint8)
|
110 |
+
eye_mask_right = np.zeros(rgba_frame.shape, dtype=np.uint8)
|
111 |
+
# Fill eye masks with polygons
|
112 |
+
cv.fillPoly(eye_mask_left, [mesh_points[LEFT_EYE]], (255, 0, 0, 255))
|
113 |
+
cv.fillPoly(eye_mask_right, [mesh_points[RIGHT_EYE]], (255, 0, 0, 255))
|
114 |
+
# Use the 4-channel masks to create zero_overlay
|
115 |
+
zero_overlay[np.where((iris_mask_left[:, :, 3] > 0) & (eye_mask_left[:, :, 3] == 0))] = 0
|
116 |
+
zero_overlay[np.where((iris_mask_right[:, :, 3] > 0) & (eye_mask_right[:, :, 3] == 0))] = 0
|
117 |
+
# Add weighted overlay to frame
|
118 |
+
rgba_frame = cv.addWeighted(rgba_frame, 1, zero_overlay, alpha, 0)
|
119 |
+
return rgba_frame
|