Hands-free mouse control using Python, OpenCV, and MediaPipe hand-tracking.
This project turns your webcam into a gesture-driven mouse. The system tracks the
thumb tip to move the cursor and uses pinch gestures to perform mouse actions:
left-drag (thumb+index), right-click (thumb+middle), and scrolling (thumb+ring).
Motion is smoothed using an Exponential Moving Average, while pyautogui
injects native OS mouse events. A safe preview mode and live overlay aid debugging and calibration.
cv2.VideoCapture
(DirectShow on Windows) for frames.mediapipe.solutions.hands.Hands
for 21 landmarks (1 hand).MOVE_REGION
and display size.Ema2D
) to stabilize jitter.pyautogui
for move, click, drag, and scroll.4
with EMA smoothing.thumb+index
pinch to drag; release to drop.thumb+middle
pinch (debounced single fire).thumb+ring
to enter; move up/down to scroll.MOVE_REGION
constrains motion to a comfortable box.ema = ฮฑยทx + (1-ฮฑ)ยทema
per axis; SMOOTHING
sets ฮฑ.dist(thumb, index) < PINCH_THRESH
.DEBOUNCE_FRAMES
consecutive frames to trigger/hold actions.MOVE_REGION
then scale to screen_w ร screen_h
.dy
from anchor maps to pyautogui.scroll()
scaled by SCROLL_SENS
.q
SMOOTHING
(0..1): higher = smoother (but laggier).PINCH_THRESH
: lower = stricter gesture recognition.DEBOUNCE_FRAMES
: raise to reduce false triggers.CURSOR_SPEED
: scale cursor movement.MOVE_REGION
: normalized box to limit motion area.TARGET_FPS
and USE_MJPG
: try for camera stability.mediapipe==0.10.14
protobuf==3.20.3
opencv-contrib-python
numpy
pyautogui
protobuf==3.20.3
and reinstall mediapipe
.CAM_INDEX
.
1) Open camera (DirectShow on Windows) โ 2) Detect hand landmarks โ 3) Thumb tip normalized โ 4) Smooth with EMA โ 5) Map to screen โ
6) Detect pinches (index/middle/ring) with debouncing โ 7) Emit OS input via pyautogui
โ 8) Draw overlay & FPS.