Skip to content

Dannny-cell/Virtual-Hand-Controlled-Piano

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎹 Virtual Hand Controlled Piano

A real-time virtual piano you play with your bare hands β€” no keyboard, no mouse, just gestures.


🎬 Demo

A live demonstration of the project is available on LinkedIn:

πŸ”— [Watch the demo on LinkedIn] (https://www.linkedin.com/in/dannny-cell)


✨ Features

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

πŸ—‚οΈ Project Structure

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

βš™οΈ How It Works

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
  1. Capture β€” OpenCV reads frames from your webcam.
  2. Track β€” MediaPipe identifies all visible hand landmarks.
  3. Detect β€” Each fingertip's position is tested against the piano key regions.
  4. Trigger β€” A note plays when the finger bends past the press threshold.
  5. Render β€” The piano, hover highlights, and pressed states are drawn over the live feed.

πŸ–₯️ Requirements

  • OS: Windows (PowerShell used for venv activation)
  • Python: 3.10 or 3.11 recommended
  • Hardware: A working webcam
  • Audio: Piano .wav sample files (see Audio Samples below)

πŸ“¦ Installation

1. Clone the repository

git clone https://github.com/Dannny-cell/Virtual-Hand-Controlled-Piano.git
cd Virtual-Hand-Controlled-Piano

2. Create and activate a virtual environment

python -m venv .venv
.\.venv\Scripts\Activate.ps1

3. Install dependencies

pip install -r requirements.txt

⚠️ Important: This project uses mediapipe==0.10.21 because it relies on the legacy mp.solutions.hands API. Newer versions of MediaPipe removed this interface and will cause errors.


🎡 Audio Samples

The app expects piano note .wav files in:

D:\piano_wav\

Expected filenames

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

Using a different folder

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"

Where to get free piano samples


▢️ Running the App

Make sure your virtual environment is active, then:

.\.venv\Scripts\Activate.ps1
python piano.py

A window will open showing your webcam feed with the piano keyboard overlaid at the bottom.


πŸ–οΈ Controls

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

πŸ› οΈ Troubleshooting

❌ module 'mediapipe' has no attribute 'solutions'

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

πŸ”‡ No sound is playing

  • Confirm D:\piano_wav exists (or that your custom path in audio.py is correct)
  • Confirm the folder contains valid .wav files
  • Confirm filenames match the expected format exactly (e.g. c4.wav, not C4.wav or piano_c4.wav)
  • Check your system volume and audio output device

πŸ“· Webcam window does not open

  • 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__)"

βœ‹ Hand not detected

  • 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

🧰 Tech Stack

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

πŸ“„ License

This project is released under the MIT License.


Made with 🎹 and Python

About

Python application utilizing MediaPipe hand tracking and OpenCV rendering to create a gesture-controlled virtual keyboard with synchronized audio playback.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages