Skip to content

Hmcg23/virtual-mouse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Virtual Mouse - Hand Tracking Controller

Control your mouse cursor using your index finger and click by pinching!

Features

  • Cursor Control: Move your index finger to control the mouse cursor
  • Click Detection: Pinch your thumb and index finger together to click
  • Smooth Movement: Built-in smoothing to reduce jittery cursor movement
  • Visual Feedback: See your hand skeleton and finger tracking in real-time

Installation

  1. Install Python dependencies:

    pip install -r requirements.txt
  2. For Linux users, you may need additional permissions for mouse control:

    # Install xdotool
    sudo apt-get install xdotool

Usage

Run the program:

python virtual_mouse.py

Controls:

  • Move cursor: Move your index finger around
  • Click: Pinch your thumb and index finger together
  • Quit: Press 'q' on the keyboard

How It Works

  1. Camera Capture: Uses OpenCV to capture video from your webcam
  2. Hand Detection: MediaPipe detects your hand and tracks 21 landmarks
  3. Finger Tracking: Monitors the index finger tip position (landmark #8)
  4. Coordinate Mapping: Converts camera coordinates to screen coordinates
  5. Smoothing: Applies exponential smoothing to reduce jitter
  6. Gesture Recognition: Detects pinch gesture (thumb-index distance < threshold)
  7. Mouse Control: PyAutoGUI moves the cursor and triggers clicks

Customization

You can adjust these parameters in the code:

  • smoothing (line 18): Higher = smoother but more lag (default: 7)
  • frame_reduction (line 21): Increases control area (default: 100)
  • min_detection_confidence (line 43): Hand detection sensitivity (default: 0.7)
  • Pinch threshold in is_pinching() (line 30): Distance for click (default: 0.05)

Tips for Best Results

  1. Lighting: Good lighting helps with hand detection
  2. Background: Plain backgrounds work better than cluttered ones
  3. Distance: Keep your hand 1-2 feet from the camera
  4. Calibration: Adjust frame_reduction if the cursor doesn't reach screen edges
  5. Smoothing: Increase smoothing value if cursor is too jittery

Troubleshooting

Cursor is jittery:

  • Increase the smoothing value (try 10-15)

Cursor doesn't reach screen edges:

  • Decrease frame_reduction value or adjust the interp ranges

Hand not detected:

  • Improve lighting
  • Lower min_detection_confidence to 0.5
  • Make sure your hand is clearly visible

Clicks not registering:

  • Increase the pinch threshold (try 0.07 instead of 0.05)
  • Make a more deliberate pinching motion

Requirements

  • Python 3.7+
  • Webcam
  • The libraries in requirements.txt

Future Enhancements

Ideas to extend this project:

  • Right-click with different gestures
  • Scroll by swiping
  • Drag-and-drop functionality
  • Multi-finger gestures
  • Calibration mode for different screen sizes
  • Virtual keyboard integration

virtual-mouse

About

virtual mouse that tracks and responds to hand movements

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages