-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
88 lines (71 loc) · 2.84 KB
/
python.py
File metadata and controls
88 lines (71 loc) · 2.84 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
import cv2
import numpy as np
from scipy.spatial import distance as dist
import dlib
# Initialize text-to-speech engine
# Load the pre-trained face detection and landmark models
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# Function to detect eyes and calculate eye aspect ratio (EAR)
def eye_aspect_ratio(eye):
A = dist.euclidean(eye[1], eye[5])
B = dist.euclidean(eye[2], eye[4])
C = dist.euclidean(eye[0], eye[3])
ear = (A + B) / (2.0 * C)
return ear
# Function to calibrate eye movements
def calibrate_eye():
# Capture and process frames for calibration (simplified for illustration)
# Implement a proper calibration routine for accurate results
# You can collect eye positions for different screen points here
# This example assumes you store these positions for later use
eye_positions = {"top_left": [], "top_right": [], "bottom_left": [], "bottom_right": []}
# Capture eye positions here...
return eye_positions
# Function to detect gaze location
def detect_gaze(eye_positions):
# Capture frame from webcam
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
for rect in rects:
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
leftEye = shape[face_utils.FACIAL_LANDMARKS_IDXS["left_eye"][0]:face_utils.FACIAL_LANDMARKS_IDXS["left_eye"][1]]
rightEye = shape[face_utils.FACIAL_LANDMARKS_IDXS["right_eye"][0]:face_utils.FACIAL_LANDMARKS_IDXS["right_eye"][1]]
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
# Implement gaze detection logic using calibrated positions
# Determine gaze location based on eye aspect ratio or other features
# This is a simplified placeholder example
if leftEAR < 0.3 and rightEAR < 0.3:
gaze_location = "center"
else:
gaze_location = "unknown"
return gaze_location
# Function to convert text to speech
def text_to_speech(text):
engine.say(text)
engine.runAndWait()
# Initialize webcam
cap = cv2.VideoCapture(0)
# Calibrate eye movements
eye_positions = calibrate_eye()
# Display eye-tracking keyboard (simplified for illustration)
print("Eye-tracking keyboard activated. Look at a letter to type:")
print("[A] [B] [C]")
# Main loop for gaze detection and typing
while True:
gaze_location = detect_gaze(eye_positions)
# Implement typing logic based on gaze location
# This is a simplified placeholder example
if gaze_location == "center":
text = "A"
print("Typed:", text)
text_to_speech(text)
# Exit condition for the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()