This project implements a real-time hand gesture volume controller using a webcam. It detects the pinch gesture between your thumb and index finger with "MediaPipe", calculates the pinch distance, and maps it to system volume control using "Pycaw" on Windows. Visual feedback is provided via "OpenCV".
- Real-time hand landmark detection with MediaPipe.
- Pinch distance computation for smooth volume adjustment.
- System volume control integration using Pycaw (Windows only).
- Visual overlays for landmarks and volume bar.
- Easy-to-use Python script with minimal setup.
-->Prerequisites
- Python 3.x installed on Windows.
- Webcam connected and accessible.
- Recommended to use a virtual environment.
--> Install Dependencies
- Uses MediaPipe Hands model to detect 21 keypoints per hand.
- Captures frames from the webcam via OpenCV.
- Converts frames to RGB for MediaPipe processing.
- Detects thumb tip (landmark 4) and index finger tip (landmark 8).
- Computes Euclidean distance between thumb and index fingertips.
- Distance formula:
[ \text{distance} = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} ] - Clamps distance between calibrated minimum and maximum pinch distances.
- Maps clamped pinch distance linearly to system volume range supported by Pycaw.
- Volume level set via Pycaw's Core Audio API bindings.
- Provides volume percentage for user interface feedback.
- OpenCV displays webcam feed with:
- Hand landmarks and connections.
- Circles on thumb and index finger tips.
- Line connecting thumb and index for pinch visualization.
- Volume bar indicating current volume percentage.
- Color changes when pinch distance is near minimum (mute signal).
-
Save the Python script (e.g.,
hand_gesture.py). -
Connect your webcam.
-
Run the script:
-
Use the pinch gesture in view of the webcam to control volume.
-
Press
ESCto exit the program.
- Adjust minimum and maximum pinch distances if volume reacts too fast or slow.
- Ensure proper lighting for better hand detection.
- Use a plain background for improved landmark accuracy.
- Pycaw works only on Windows; Linux/macOS support requires other audio libraries.
- Python 3.x
- MediaPipe (hand keypoint detection)
- OpenCV (video capture and UI)
- Pycaw (Windows audio control)
- NumPy (math and array operations)