This project demonstrates how to control your computer's mouse cursor using hand gestures captured by a webcam. It uses OpenCV, MediaPipe, and PyAutoGUI libraries to recognize hand landmarks and translate them into mouse movements and clicks.
Before running this project, you need to have the following libraries and dependencies installed:
- Python 3.x
- OpenCV (
cv2) - MediaPipe (
mediapipe) - PyAutoGUI (
pyautogui)
You can install the required packages using pip:
pip install opencv-python mediapipe pyautogui-
Connect a webcam to your computer.
-
Clone or download this repository to your local machine.
-
Open the project directory in your terminal.
-
Run the Python script:
python main.py-
A window will appear displaying the webcam feed with hand landmarks overlaid.
-
Point your index finger (usually the second finger) to control the mouse cursor.
-
Bring your thumb close to your index finger to perform a click action.
-
To exit the program, press
Escor close the window.
-
The code captures video frames from the webcam using OpenCV.
-
It uses the MediaPipe library to detect hand landmarks in each frame.
-
The index finger is tracked to move the mouse cursor, and the thumb is used for clicking.
-
The mouse cursor's movement is relative to the screen's resolution, so it scales hand movements appropriately.
-
When the index finger and thumb are close enough (within 20 pixels), it simulates a mouse click action.
You can customize the code according to your preferences:
-
Adjust the
radiusin thecv2.circlefunction to change the size of the circles drawn on the screen. -
Modify the
abs(index_y - thumb_y)threshold to control how close the thumb needs to be to the index finger to trigger a click. -
Change the sleep duration in
pyautogui.sleepto adjust the click's duration.
If the program is not working as expected, try the following:
-
Ensure that your webcam is properly connected and functioning.
-
Check if all required libraries are installed.
-
Experiment with the thresholds and parameters to fine-tune hand gesture recognition.
-
Make sure there is enough lighting in your environment for better hand tracking.
-
This project uses OpenCV, MediaPipe, and PyAutoGUI libraries.
-
MediaPipe is developed by Google Research.
Feel free to customize and expand upon this README as needed for your project. You can include information about additional features, troubleshooting tips, or any other relevant details.