Gesture Volume Control
π Overview
Pinch in front of the camera: the distance between your thumb and index finger controls the computer's volume in real time.
The pipeline is simple and direct - hand landmark distance, mapped to a volume range, fed into the system volume API. It is the bridge project from gesture detection to gesture control, and the standard example of an interactive application.
π§° What you need
- Computer + camera
- Python 3.7+ environment
- pip install: opencv-python, mediapipe, cvzone, pycaw (Windows volume control)
- Optional: external speakers for testing
π§ Step by step
Install the dependencies
On Windows use pycaw for volume, on macOS osascript is a workable substitute
Test that the volume API works before going further
Detect the hand landmarks
Use HandTrackingModule to get the thumb and index fingertips
Draw the connecting line and the distance text between them
Compute the distance
Use the Euclidean distance formula on the two fingertips (in pixels)
Moving nearer or further from the camera changes the absolute value, so normalise first
Map it to a volume range
Map the pixel distance linearly onto a volume of 0-100
Use numpy clip/interp to handle the ends of the range
Set the system volume
Write the mapped value to the system volume API
Draw the volume bar on the frame for immediate feedback
Smooth and finish
Apply a smoothing filter to the volume value so it does not jump
Refactor it into a reusable volume-control function
π‘ Tips
- The further your hand is from the camera, the smaller the change in distance for the same gesture; keep your hand 30-60cm from the lens
- Set the minimum volume step to 1-2 so small jitters do not make the volume jump around
- Abstract the volume interface across platforms - one implementation each for Windows, macOS, and Linux