AI-Virtual-Mouse: Sub-Pixel Touchless Windows Controller
A real-time vision pipeline translating hand gestures captured from standard webcams into fluid OS cursor movements. Solves the ubiquitous optical tremor problem through an Exponential Moving Average (EMA) adaptive filter coupled directly with Win32 hardware event queues.
1. The Problem of Optical Jitter in HCI
Naïve implementations of webcam-based mouse controllers directly map the pixel coordinates of an index fingertip to screen pixels. Because webcam sensor noise and natural micro-muscular tremors cause coordinates to oscillate by 2-5 pixels each frame, the on-screen cursor suffers from intense jitter, making precision clicks on small desktop buttons or close links impossible.
AI-Virtual-Mouse addresses this through a multi-stage filtering pipeline combining confidence thresholding, dynamic dead-zone thresholds, and velocity-adaptive Exponential Moving Average (EMA) smoothing.
2. The Adaptive Smoothing Mathematical Model
When hand speed is high, smoothing is relaxed to eliminate perceptual drag; when hand speed drops near zero, the filter coefficient scales up to lock the cursor into a dead-zone:
# Dynamic EMA Smoothing Formulation
distance = math.hypot(curr_x - prev_x, curr_y - prev_y)
# Adaptive alpha: higher alpha when fast movement, lower alpha when micro-adjusting
alpha = min(1.0, max(0.15, distance / 40.0))
smooth_x = prev_x + alpha * (curr_x - prev_x)
smooth_y = prev_y + alpha * (curr_y - prev_y)
# Direct Win32 Cursor Dispatch (Bypasses slow third-party wrappers)
ctypes.windll.user32.SetCursorPos(int(smooth_x), int(smooth_y))
3. Gesture State Machine Architecture
Instead of ambiguous single-frame triggers, gestures are governed by a finite state machine:
Tracking Mode (Pointing)
Index finger extended, middle/ring/pinky curled. The index tip coordinates drive smoothed cursor navigation without triggering clicks.
Left Click / Drag (Pinch Gesture)
Euclidean distance between Index Tip (Landmark 8) and Thumb Tip (Landmark 4) drops below calibrated threshold $d < 25\text{px}$, firing MOUSEEVENTF_LEFTDOWN.
Right Click (Middle Pinch)
Euclidean distance between Middle Tip (Landmark 12) and Thumb Tip (Landmark 4) drops below threshold, dispatching MOUSEEVENTF_RIGHTDOWN / UP.
Two-Finger Scroll
Index and Middle fingers extended in parallel. Vertical vector displacement translates directly into native Windows mouse-wheel delta events.