-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.py
More file actions
69 lines (61 loc) · 2.05 KB
/
Copy pathUtility.py
File metadata and controls
69 lines (61 loc) · 2.05 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
# Calculate the distance between two vector:
def distance(point_1, point_2):
sqr_sum = 0
for i, j in zip(point_1, point_2):
sqr_sum += (i - j)**2
dist = sqr_sum ** 0.5
return dist
# Function to denormalize the cordinates:
def denormalize_cordinates(norm_x, norm_y, denorm_w, denorm_h):
return [norm_x * denorm_w, norm_y * denorm_h]
# Function for calculate eye aspect ration for a particular eye:
def get_EAR(landmarks, refer_index, img_width, img_height):
cords_points = []
for i in refer_index:
landmark = landmarks[i]
cord = denormalize_cordinates(
norm_x = landmark.x,
norm_y = landmark.y,
denorm_w = img_width,
denorm_h = img_height
)
cords_points.append(cord)
p2_p6 = distance(cords_points[1], cords_points[5]) ** 2
p3_p5 = distance(cords_points[2], cords_points[4]) ** 2
p1_p4 = distance(cords_points[0], cords_points[3]) ** 2
ear = (p2_p6 + p3_p5) / (2.0 * p1_p4)
return ear * 10
# Function to get avg ear:
def get_avg_EAR(landmarks, left_eye_inds, right_eye_inds, image_width, image_height):
ear_left = get_EAR(
landmarks = landmarks,
refer_index = left_eye_inds,
img_width = image_width,
img_height = image_height
)
ear_right = get_EAR(
landmarks = landmarks,
refer_index = right_eye_inds,
img_width = image_width,
img_height = image_height
)
ear_avg = (ear_left + ear_right) / 2.0
return ear_avg
# Function to get face bounding box:
def get_bounding_box(landmarks, face_inds, image_width, image_height):
cords_points = []
for i in face_inds:
landmark = landmarks[i]
cord = denormalize_cordinates(
norm_x = landmark.x,
norm_y = landmark.y,
denorm_w = image_width,
denorm_h = image_height
)
cords_points.append(cord)
return (
int(cords_points[0][0])-10,
int(cords_points[1][1]),
int(cords_points[2][0])+10,
int(cords_points[3][1])
)