EYE AND FACIAL GESTURE CONTROLLED
CURSOR
SYSTEM USING OPENCV AND MEDIAPIPE
Sample code
import cv2
import mediapipe as mp
import pyautogui
import subprocess
import time
import numpy as np
# -------------------------------
# Initialize Mediapipe FaceMesh
# -------------------------------
mp_face_mesh = [Link].face_mesh
face_mesh = mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.6,
min_tracking_confidence=0.6
# -------------------------------
# Initialize Camera
# -------------------------------
cap = [Link](0)
[Link](3, 640) # width
[Link](4, 480) # height
screen_w, screen_h = [Link]()
screen_x, screen_y = screen_w // 2, screen_h // 2 # start cursor in center
# -------------------------------
# Action Cooldowns & States
# -------------------------------
last_action_time = {
"blink": 0,
"mouth": 0,
"gaze_window": 0
cooldown = 1.0 # seconds between repeated actions
gaze_state = "center" # "center", "left", "right"
# -------------------------------
# Eye Cursor Calibration
# -------------------------------
print("Calibration: Look at the center of the screen for 2 seconds...")
calibration_points = []
start_time = [Link]()
while [Link]() - start_time < 2:
ret, frame = [Link]()
if not ret:
continue
frame = [Link](frame, 1)
rgb = [Link](frame, cv2.COLOR_BGR2RGB)
results = face_mesh.process(rgb)
if results.multi_face_landmarks:
lm = results.multi_face_landmarks[0].landmark
iris_x = lm[474].x
iris_y = lm[474].y
calibration_points.append((iris_x, iris_y))
[Link]("Calibration", frame)
if [Link](1) & 0xFF == 27:
break
[Link]("Calibration")
if calibration_points:
calib_iris_x = [Link]([p[0] for p in calibration_points])
calib_iris_y = [Link]([p[1] for p in calibration_points])
else:
calib_iris_x, calib_iris_y = 0.5, 0.5 # fallback
print(" Calibration complete!")
# -------------------------------
# Main Loop
# -------------------------------
while True:
ret, frame = [Link]()
if not ret:
break
frame = [Link](frame, 1)
rgb = [Link](frame, cv2.COLOR_BGR2RGB)
results = face_mesh.process(rgb)
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
h, w, _ = [Link]
lm = face_landmarks.landmark
now = [Link]()
# -------------------------------
# Directional Eye Gaze → Cursor Movement
# -------------------------------
iris = lm[474]
dx = iris.x - calib_iris_x
dy = iris.y - calib_iris_y
# Apply threshold to ignore small movements
threshold = 0.01
if abs(dx) > threshold or abs(dy) > threshold:
speed = 50 # max pixels per frame
move_x = [Link](dx * speed, -speed, speed)
move_y = [Link](dy * speed, -speed, speed)
screen_x += move_x
screen_y += move_y
screen_x = [Link](screen_x, 0, screen_w - 1)
screen_y = [Link](screen_y, 0, screen_h - 1)
[Link](screen_x, screen_y)
# Draw iris position
[Link](frame, (int(iris.x * w), int(iris.y * h)), 3, (0, 255, 0), -1)
# -------------------------------
# Blink Detection → Left Click
# -------------------------------
left_eye_top = lm[159].y
left_eye_bottom = lm[145].y
eye_ratio = abs(left_eye_top - left_eye_bottom)
if eye_ratio < 0.004:
if now - last_action_time["blink"] > 0.6:
[Link]()
print(" Blink → Left Click")
last_action_time["blink"] = now
# -------------------------------
# Mouth Open Detection → Open This PC
# -------------------------------
mouth_open = (lm[14].y - lm[13].y) * h
if mouth_open > 25:
if now - last_action_time["mouth"] > cooldown:
[Link]("explorer")
print(" Mouth Open → This PC")
last_action_time["mouth"] = now
# -------------------------------
# Gaze-based Window Switching
# -------------------------------
gaze_threshold = 0.03 # Adjust sensitivity
if dx < -gaze_threshold and gaze_state !
= "left" and now - last_action_time["gaze_window"] > cooldown:
[Link]("alt", "tab") # Switch to previous window
print(" Gaze Left → Switch Window Left")
gaze_state = "left"
last_action_time["gaze_window"] = now
elif dx > gaze_threshold and gaze_state !
= "right" and now - last_action_time["gaze_window"] > cooldown:
[Link]("alt", "tab") # Switch to next window
print(" Gaze Right → Switch Window Right")
gaze_state = "right"
last_action_time["gaze_window"] = now
elif abs(dx) <= gaze_threshold:
gaze_state = "center" # reset when looking straight
[Link]("Facial + Eye Control", frame)
if [Link](1) & 0xFF == 27:
break
[Link]()
[Link]()