A real-time virtual piano you play with your bare hands β no keyboard, no mouse, just gestures.
A live demonstration of the project is available on LinkedIn:
π [Watch the demo on LinkedIn] (https://www.linkedin.com/in/dannny-cell)
| Feature | Description |
|---|---|
| π Real-time hand tracking | Uses MediaPipe to detect up to 21 hand landmarks per hand |
| πΉ On-screen piano keyboard | Rendered directly on the live webcam feed via OpenCV |
| π Hover detection | Highlights keys when a fingertip is positioned over them |
| π΅ Press detection | Triggers a note when the finger bends downward past a threshold |
| π Audio playback | Plays real .wav piano samples with pygame |
| π‘ Visual feedback | Shows hover, pressed, and sustained note states in real time |
| π§© Modular codebase | Clean separation of concerns across dedicated Python modules |
Virtual-Hand-Controlled-Piano/
βββ piano.py # Main entry point β runs the app loop
βββ audio.py # Loads and plays .wav note samples
βββ collision.py # Detects fingertip-to-key collisions
βββ hand_tracking.py # MediaPipe hand landmark detection
βββ piano_layout.py # Defines key positions and note mappings
βββ rendering.py # Draws the piano and overlays onto the frame
βββ utils.py # Shared helper functions
βββ requirements.txt # Pinned Python dependencies
βββ README.md
Webcam Frame
β
βΌ
MediaPipe Hand Tracking βββΊ 21 Landmarks per hand
β
βΌ
Fingertip Positions βββΊ Collision Detection against Key Regions
β
βββ Fingertip over key? βββΊ Highlight key (hover state)
β
βββ Finger bent enough? βββΊ Play .wav sample + Press visual
- Capture β OpenCV reads frames from your webcam.
- Track β MediaPipe identifies all visible hand landmarks.
- Detect β Each fingertip's position is tested against the piano key regions.
- Trigger β A note plays when the finger bends past the press threshold.
- Render β The piano, hover highlights, and pressed states are drawn over the live feed.
- OS: Windows (PowerShell used for venv activation)
- Python: 3.10 or 3.11 recommended
- Hardware: A working webcam
- Audio: Piano
.wavsample files (see Audio Samples below)
git clone https://github.com/Dannny-cell/Virtual-Hand-Controlled-Piano.git
cd Virtual-Hand-Controlled-Pianopython -m venv .venv
.\.venv\Scripts\Activate.ps1pip install -r requirements.txt
β οΈ Important: This project usesmediapipe==0.10.21because it relies on the legacymp.solutions.handsAPI. Newer versions of MediaPipe removed this interface and will cause errors.
The app expects piano note .wav files in:
D:\piano_wav\
c3.wav d3.wav e3.wav f3.wav g3.wav a3.wav b3.wav
c#3.wav d#3.wav f#3.wav g#3.wav a#3.wav
c4.wav d4.wav e4.wav f4.wav g4.wav a4.wav b4.wav
c#4.wav d#4.wav f#4.wav g#4.wav a#4.wav
c5.wav
If your samples are stored elsewhere, open audio.py and update the DEFAULT_SAMPLES_DIR constant:
DEFAULT_SAMPLES_DIR = r"C:\path\to\your\piano_wav"- freesound.org β search "piano note C4" etc.
- University of Iowa MIS β free high-quality samples
- SampleSwap β royalty-free audio library
Make sure your virtual environment is active, then:
.\.venv\Scripts\Activate.ps1
python piano.pyA window will open showing your webcam feed with the piano keyboard overlaid at the bottom.
| Action | Result |
|---|---|
| Move fingertip over a key | Key highlights (hover) |
| Bend finger downward | Key press + note plays |
| Multiple fingers | Play chords |
Press Q |
Quit the application |
You have a newer, incompatible version of MediaPipe installed. Fix it with:
pip uninstall -y mediapipe
pip install --no-cache-dir mediapipe==0.10.21- Confirm
D:\piano_wavexists (or that your custom path inaudio.pyis correct) - Confirm the folder contains valid
.wavfiles - Confirm filenames match the expected format exactly (e.g.
c4.wav, notC4.wavorpiano_c4.wav) - Check your system volume and audio output device
- Make sure your webcam is plugged in and not in use by another app (Teams, Zoom, etc.)
- Try changing the camera index in
piano.py:cv2.VideoCapture(0)βcv2.VideoCapture(1) - Confirm OpenCV installed correctly:
python -c "import cv2; print(cv2.__version__)"
- Ensure good, even lighting on your hand
- Keep your hand within 40β70 cm of the webcam
- Avoid backgrounds that are similar in color to your skin tone
| Library | Version | Purpose |
|---|---|---|
| MediaPipe | 0.10.21 | Hand landmark detection |
| OpenCV | 4.x | Webcam capture & rendering |
| pygame | 2.x | .wav audio playback |
| NumPy | 1.x | Array math for landmark coordinates |
This project is released under the MIT License.
Made with πΉ and Python