Medvira commited on
Commit
3c2d542
·
verified ·
1 Parent(s): ef5def8

Update utils.py

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