This project uses your computer's webcam with OpenCV and the MediaPipe library to enable hands-free mouse control, including cursor movement, clicking/dragging, and scrolling.
You must have Python installed. MediaPipe's official support range for Python is 3.9 - 3.12.
If you are on Windows, you MUST install the Microsoft Visual C++ Redistributable for Visual Studio 2015-2022. This is required by the underlying C++ binaries used by MediaPipe and other libraries.
-
Download: Get the latest official Redistributable installer: Microsoft Visual C++ Redistributable
-
Install: Download and run the
x64version for 64-bit Windows. -
Run: Execute the installer.
Using a virtual environment prevents conflicts with other Python projects.
python -m venv venv_hand_mouse
# Activate on Windows:
.\venv_hand_mouse\Scripts\activate
# Activate on macOS/Linux:
source venv_hand_mouse/bin/activate
Install the following packages using pip. Ensure your virtual environment is activated first.
pip install opencv-python mediapipe numpy pyautogui
| Package | Purpose |
|---|---|
opencv-python |
Handles webcam input and video display (cv2). |
mediapipe |
Provides the highly accurate hand-tracking model. |
numpy |
Essential for fast array manipulations and calculations. |
pyautogui |
Controls the operating system's mouse and keyboard. |
Save the main Python application code as a file named mp_handpos_mouse.py.
Run the script from your terminal:
python mp_handpos_mouse.py
The script will open a window displaying your webcam feed, along with real-time feedback on the detection status.
| Gesture | Action |
|---|---|
| Move Hand | Controls the cursor position on the screen. |
| Index-to-Thumb Pinch | Left-Click / Drag (while the pinch is held). |
| Middle-to-Thumb Pinch | Activates Scroll Mode. Move your pinched hand up or down to scroll the page. |
| Two-Hand Index-to-Thumb Pinch | Executes a Double-Click. |
| Press 'q' key | Stops the script and closes the video window. |
If pyautogui fails to control the mouse on your Linux distribution, you may need additional dependencies:
sudo apt-get install python3-tk python3-dev