dinesh-0602 / Virtual-Piano-game

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hand Tracking Piano with OpenCV, Mediapipe, and Pygame

Overview

This Python script utilizes OpenCV, Mediapipe, and Pygame to create a simple hand-tracking piano application. The program captures video from the default camera, detects hand landmarks using the Mediapipe library, and triggers piano sounds through Pygame based on hand positions.

Dependencies

  • OpenCV: Used for capturing and processing video frames.
  • Mediapipe: Employs the hands module for hand landmark detection.
  • Pygame: Enables the playback of piano sounds.

Install the required libraries using the following commands:

pip install opencv-python
pip install mediapipe
pip install pygame

Usage

  1. Make sure you have a working camera connected to your system.
  2. Run the script by executing the following command:
python virtual_keyboard.py

A window will appear displaying the live camera feed with hand tracking and piano keys drawn on it.

Play the piano by hovering your hand over the corresponding virtual keys. The keys are mapped to the C4 to B4 notes.

Press 'q' to exit the application.

Configuration

  • You can uncomment the lines that set the frame width and height if needed:
# cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

Customize the sound files (sound_files) to use your preferred piano notes in MP3 format.

Piano Key Positions

The script defines the positions of the virtual piano keys on the video frame. You can adjust these positions in the piano_keys list based on your preferences or the dimensions of your video feed.

Acknowledgments

  • The script uses the Mediapipe library for hand tracking. More information about Mediapipe can be found here.
  • Pygame is utilized for playing piano sounds. You can find Pygame documentation here.

License

This project is licensed under the MIT License. Feel free to modify and distribute it according to the terms of the license.

Feel free to contact the author for any questions or improvements.

About


Languages

Language:Python 100.0%