-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
59 lines (46 loc) · 1.87 KB
/
Copy pathutils.py
File metadata and controls
59 lines (46 loc) · 1.87 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
import math
import screeninfo
def calculate_distance(p1, p2):
"""Calculates Euclidean distance between two points."""
x1, y1 = p1
x2, y2 = p2
return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
def get_screen_size():
"""Gets the primary screen resolution."""
try:
screen = screeninfo.get_monitors()[0]
return screen.width, screen.height
except Exception:
return 1920, 1080 # Default fallback
def map_coordinates(norm_x, norm_y, screen_w, screen_h, calib_min_x=0, calib_max_x=1, calib_min_y=0, calib_max_y=1):
"""
Maps normalized coordinates (relative to eye/calibration) to screen coordinates.
"""
# Inverse interpolation based on calibration range
# screen_x = ((iris_x - min_x) / (max_x - min_x)) * screen_width
range_x = calib_max_x - calib_min_x
range_y = calib_max_y - calib_min_y
if range_x == 0: range_x = 1
if range_y == 0: range_y = 1
rel_x = (norm_x - calib_min_x) / range_x
rel_y = (norm_y - calib_min_y) / range_y
screen_x = int(rel_x * screen_w)
screen_y = int(rel_y * screen_h)
return screen_x, screen_y
def clamp(value, min_val, max_val):
return max(min_val, min(value, max_val))
def calculate_ear(eye_landmarks, landmarks_list):
"""
Calculates Eye Aspect Ratio (EAR).
eye_landmarks: Indices of eye landmarks (p1, p2, p3, p4, p5, p6)
landmarks_list: List of all facial landmarks with (x, y) coordinates
"""
# Vertical distances
A = calculate_distance(landmarks_list[eye_landmarks[1]], landmarks_list[eye_landmarks[5]])
B = calculate_distance(landmarks_list[eye_landmarks[2]], landmarks_list[eye_landmarks[4]])
# Horizontal distance
C = calculate_distance(landmarks_list[eye_landmarks[0]], landmarks_list[eye_landmarks[3]])
if C == 0:
return 0.0
ear = (A + B) / (2.0 * C)
return ear