-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (54 loc) · 2.06 KB
/
main.py
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
import cv2
import numpy as np
import pyautogui
# Initialize the webcam
cap = cv2.VideoCapture(0)
# Set the dimensions of the screen
screen_width, screen_height = pyautogui.size()
# Set up haarcascade_eye.xml eye detector model
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
# To use local haarcascade_eye.xml file
# eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
# Parameters for click and scroll actions
click_threshold = 30
scroll_threshold = 50
prev_x, prev_y = 0, 0
scrolling = False
while True:
# Read a frame from the webcam
ret, frame = cap.read()
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect eyes in the frame
eyes = eye_cascade.detectMultiScale(gray, 1.3, 5)
# If eyes are detected, find the center of the first detected eye
if len(eyes) > 0:
(ex, ey, ew, eh) = eyes[0]
eye_center = (ex + ew // 2, ey + eh // 2)
# Scale eye coordinates to screen resolution
x = int(eye_center[0] * screen_width / cap.get(3))
y = int(eye_center[1] * screen_height / cap.get(4))
# Move the cursor
pyautogui.moveTo(x, y)
# Check for click action
if abs(x - prev_x) < click_threshold and abs(y - prev_y) < click_threshold:
pyautogui.click()
# Check for scroll action
if abs(x - prev_x) > scroll_threshold or abs(y - prev_y) > scroll_threshold:
if not scrolling:
scrolling = True
pyautogui.mouseDown(button='middle')
pyautogui.moveRel(x - prev_x, y - prev_y)
else:
scrolling = False
pyautogui.mouseUp(button='middle')
# Update previous position
prev_x, prev_y = x, y
# Display the frame
cv2.imshow('Gaze Tracking', frame)
# Exit if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcam and close the OpenCV window
cap.release()
cv2.destroyAllWindows()