this is a webcam version of Fruit Ninja. move landmark 8 (your index finger) to slice the fruits. to pause / play, bring landmarks 4 and 8 together (in essence, pinch your thumb and index together).
- Python 3.8+
- Webcam
- macOS, Windows, or Linux
-
clone the repo:
git clone <your-repo-url> cd hand-tracking
-
create a venv (recommended):
python3 -m venv venv source venv/bin/activateand if you're on Windows:
venv\Scripts\activate
-
install dependencies:
pip install -r requirements.txt
-
download the hand-tracking model:
curl -o hand_landmarker.task https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task
run the app:
python main.py- move your index finger to control the cursor
- pinch your thumb and index finger together to click
- Press 'q' to quit the application
- MediaPipe: Google's ML framework for hand landmark detection
- OpenCV: real-time video processing & display
- PyAutoGUI: system-level mouse control (i've only tested till IDE)
- captures the video frames from your webcam
- converts frames to an rgb format so mediapipe can use it
- detects hands & extracts the pre-labeled 21 landmark points per hand
- maps index finger position to screen coordinates
- calculates the distance b/w thumb & index finger tips
- triggers click when pinch distance falls below a certain threshold
You can adjust these in main.py:
# cursor smoothing (0.0-0.9, least smoothing to highest)
smoothing = 0.5
# pinch detection threshold (lower = more sensitive)
pinch_threshold = 0.05
# hand detection confidence
min_hand_detection_confidence = 0.5
min_tracking_confidence = 0.5mediapipe==0.10.31 # hand tracking ML model
opencv-python # vid capture + display
pyautogui # mouse control
- make sure your webcam is connected and not being used by another application
- on macOS, you may need to grant camera permissions in system preferences
- increase the
smoothingvalue (try 0.7 or 0.8) - make sure you have good lighting
- keep your hand at a steady distance from the camera
- adjust the
pinch_thresholdvalue: increase (ex: 0.07) for less sensitivity OR decrease (ex: 0.03) for more sensitivity
- this means you're using an incompatible MediaPipe version
- make sure you have MediaPipe 0.10.30 or higher installed
- run
pip install --upgrade mediapipe
- make sure
hand_landmarker.taskis in the same directory asmain.py - re-download the model using the curl command from step 4 in installation
MIT License - feel free to use this project for personal or commercial purposes.
- MediaPipe by Google for the hand tracking model
- OpenCV community for video processing tools
- PyAutoGUI devs for mouse control capabilities