Control MIDI piano chords in real-time using hand gestures captured from your webcam. Each finger is mapped to a chord in the D major scale, and chords play and sustain naturally as you move your fingers.
- 🖐️ Real-time hand detection using cvzone
- 🎼 Chord mapping to fingers (D major scale)
- 🎹 MIDI output with sustain effect using
pygame.midi - 👏 Supports both left and right hands
- 🎶 Dynamic gesture-based music interaction
git clone https://github.com/yourusername/hand-midi-chords.git
cd hand-midi-chordsMake sure Python 3.7+ is installed.
pip install opencv-python pygame cvzoneIf
cvzoneis not found, install directly from GitHub:
pip install git+https://github.com/cvzone/cvzone.git- The webcam detects your hands in real-time using
cvzone’sHandDetector. - Each finger corresponds to a specific chord (left and right hands are mapped identically).
- When a finger is raised (
fingersUp), a chord is played. - When the finger is lowered, the chord sustains for a short delay before stopping.
| Finger | Chord (Notes) | Description |
|---|---|---|
| Thumb | D Major (62, 66, 69) | D - F# - A |
| Index | E Minor (64, 67, 71) | E - G - B |
| Middle | F# Minor (66, 69, 73) | F# - A - C# |
| Ring | G Major (67, 71, 74) | G - B - D |
| Pinky | A Major (69, 73, 76) | A - C# - E |
Both hands use the same chord mapping.
pygame.midi.init()initializes MIDI output.cvzone.HandTrackingModule.HandDetectorhandles hand and finger tracking.fingersUp()determines which fingers are raised.- MIDI notes are triggered with
note_onandnote_offfunctions. - Chords are sustained for a configurable delay (
SUSTAIN_TIME = 2.0).
-
🔁 Change Instrument:
Modify the instrument using:player.set_instrument(<instrument_number>)
Refer to the General MIDI Instrument List for codes.
-
🎼 Change Chord Scale:
Replace thechordsdictionary with your preferred scales or custom mappings. -
⏱️ Adjust Sustain Time:
ModifySUSTAIN_TIME(in seconds) to lengthen or shorten the delay after chord release.
- Press
qon your keyboard to quit the application safely. - This releases the webcam and MIDI resources.
- cvzone by Murtaza Hassan
pygame.midifor MIDI interfacing- OpenCV for real-time camera input
This project is open-source and available under the MIT License.