-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
170 lines (140 loc) · 6.29 KB
/
main.py
File metadata and controls
170 lines (140 loc) · 6.29 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
import cv2
import time
import tkinter as tk
from PIL import Image, ImageTk
from eye_tracker import EyeTracker
from mouse_controller import MouseController
from utils import get_ear, is_dark
import numpy as np
# --- CONFIGURATION ---
BLINK_THRESHOLD = 0.22
DOUBLE_BLINK_INTERVAL = 0.5
LONG_CLOSE_THRESHOLD = 5.0
DARKNESS_THRESHOLD = 25
class EyeMouseApp:
def __init__(self, root):
self.root = root
self.root.title("Eye Tracking Mouse")
self.root.geometry("450x600")
self.root.configure(bg="#FBFBFD")
self.tracker = EyeTracker()
self.mouse = MouseController()
self.is_active = False
# --- UI LAYOUT ---
self.header = tk.Label(root, text="Eye Control", font=("SF Pro Display", 28, "bold"), bg="#FBFBFD", fg="#1D1D1F")
self.header.pack(pady=(60, 10))
self.subhead = tk.Label(root, text="A work-in-progress accessibility tool", font=("SF Pro Display", 14), bg="#FBFBFD", fg="#86868B")
self.subhead.pack(pady=(0, 40))
self.btn_frame = tk.Frame(root, bg="#FBFBFD")
self.btn_frame.pack(pady=20)
self.start_btn = tk.Button(
self.btn_frame,
text="Turn On",
command=self.start_tracking,
font=("SF Pro Display", 16, "bold"),
bg="#0071E3",
fg="white",
padx=40,
pady=15,
relief="flat",
activebackground="#005bb5",
activeforeground="white",
cursor="hand2",
bd=0
)
self.start_btn.pack()
self.info_frame = tk.Frame(root, bg="#FBFBFD")
self.info_frame.pack(pady=40, padx=50)
instructions = [
"• Blink twice rapidly to Click",
"• Keep eyes closed for 5s to Stop",
"• Cover camera to Stop"
]
for text in instructions:
lbl = tk.Label(self.info_frame, text=text, font=("SF Pro Display", 12), bg="#FBFBFD", fg="#424245", justify="left")
lbl.pack(anchor="w", pady=5)
self.status_label = tk.Label(root, text="System Ready", font=("SF Pro Display", 10, "bold"), bg="#FBFBFD", fg="#86868B")
self.status_label.pack(side="bottom", pady=(10, 30))
# --- TEST AREA ---
self.test_area = tk.Label(
root,
text="Test Clicking Here",
font=("SF Pro Display", 12, "bold"),
bg="#F4F4F7",
fg="#1D1D1F",
width=30,
height=4
)
self.test_area.pack(pady=10)
self.last_blink_time = 0
self.eyes_closed_start_time = None
self.center_offset = (0, 0)
self.face_center_offset = (0, 0)
def flash_test_area(self):
self.test_area.config(bg="#34C759", text="CLICK DETECTED!")
self.root.after(500, lambda: self.test_area.config(bg="#F4F4F7", text="Test Clicking Here"))
def start_tracking(self):
if self.is_active: return
self.is_active = True
self.status_label.config(text="Calibration: LOOK AT CENTER OF SCREEN", fg="#0071E3")
self.root.iconify()
self.run_loop()
def run_loop(self):
cap = cv2.VideoCapture(0)
is_calibrated = False
while self.is_active:
ret, frame = cap.read()
if not ret: break
if is_dark(frame, DARKNESS_THRESHOLD):
self.is_active = False
break
data = self.tracker.get_eye_data(frame)
if data:
left_ear = get_ear(data['left_eye'], [0, 1, 2, 3, 4, 5])
right_ear = get_ear(data['right_eye'], [0, 1, 2, 3, 4, 5])
avg_ear = (left_ear + right_ear) / 2.0
left_iris_center = np.mean(data['left_iris'], axis=0)
right_iris_center = np.mean(data['right_iris'], axis=0)
curr_dx = (left_iris_center[0] - data['left_center'][0] + right_iris_center[0] - data['right_center'][0]) / 2.0
curr_dy = (left_iris_center[1] - data['left_center'][1] + right_iris_center[1] - data['right_center'][1]) / 2.0
curr_face_dx = data['face_center'][0]
curr_face_dy = data['face_center'][1]
if not is_calibrated:
self.center_offset = (curr_dx, curr_dy)
self.face_center_offset = (curr_face_dx, curr_face_dy)
is_calibrated = True
self.status_label.config(text="Tracking Active", fg="#34C759")
final_dx = -(curr_dx - self.center_offset[0])
final_dy = curr_dy - self.center_offset[1]
final_face_dx = -(curr_face_dx - self.face_center_offset[0])
final_face_dy = curr_face_dy - self.face_center_offset[1]
avg_eye_width = (data['left_width'] + data['right_width']) / 2.0
self.mouse.move_to_relative((final_dx, final_dy), (final_face_dx, final_face_dy), avg_eye_width)
if avg_ear < BLINK_THRESHOLD:
if self.eyes_closed_start_time is None:
self.eyes_closed_start_time = time.time()
if time.time() - self.eyes_closed_start_time > LONG_CLOSE_THRESHOLD:
self.is_active = False
else:
if self.eyes_closed_start_time:
duration = time.time() - self.eyes_closed_start_time
if 0.05 < duration < 0.3:
current_time = time.time()
if current_time - self.last_blink_time < DOUBLE_BLINK_INTERVAL:
self.mouse.click()
self.flash_test_area()
self.last_blink_time = 0
else:
self.last_blink_time = current_time
self.eyes_closed_start_time = None
if cv2.waitKey(1) & 0xFF == ord('q'):
self.is_active = False
break
cap.release()
cv2.destroyAllWindows()
self.status_label.config(text="Mouse is OFF", fg="black")
self.root.deiconify()
if __name__ == "__main__":
root = tk.Tk()
app = EyeMouseApp(root)
root.mainloop()