A fun and interactive Python/OpenCV project that transforms your webcam into a virtual drawing canvas using advanced hand gesture recognition.
VirtualPainter uses computer vision and machine learning to detect hand gestures in real-time, allowing users to draw, paint, and create digital art using nothing but hand movements. Built with MediaPipe and OpenCV, it offers an intuitive and engaging way to create digital artwork.
- 🖐️ Real-time Hand Tracking: Advanced gesture recognition using MediaPipe
- 🎨 Multiple Colors: Red, Green, Blue, Magenta, and Eraser modes
- ✏️ Drawing Modes: Selection mode (two fingers) and Drawing mode (index finger)
- 🖼️ Persistent Canvas: Your drawings stay on screen until cleared
- 🎯 Gesture Controls: Intuitive finger-based interface
- 📱 Cross-platform: Works on Windows, macOS, and Linux
-
Clone the repository
git clone <repository-url> cd VirtualPainter
-
Install dependencies
pip install -r requirements.txt
-
Run the application
python app.py
- Position yourself 2-3 feet from your webcam
- Select colors by raising index and middle fingers together, then hover over the color palette
- Draw by raising only your index finger and moving it around
- Erase by selecting the black color (rightmost in palette)
- Press 'q' to quit the application
- API Documentation - Complete API reference for all classes and functions
- User Guide - Step-by-step instructions and tips for users
- Technical Reference - Architecture details and implementation guide
- Selection Mode: Index + Middle fingers up → Navigate color palette
- Drawing Mode: Index finger only → Draw on canvas
- Rest Position: All fingers down → No action
| Color | Position | Use Case |
|---|---|---|
| Red | Slot 1 | Default drawing |
| Green | Slot 2 | Nature elements |
| Blue | Slot 3 | Sky and water |
| Magenta | Slot 4 | Highlights |
| Black | Slot 5 | Eraser mode |
VirtualPainter/
├── app.py # Main application
├── detectlib.py # Detection library with HandDetector, FaceDetector, PoseDetector
├── requirements.txt # Python dependencies
├── graphics/ # UI assets and color palette images
├── docs/ # Comprehensive documentation
│ ├── API_DOCUMENTATION.md # Complete API reference
│ ├── USER_GUIDE.md # User instructions and tips
│ └── TECHNICAL_REFERENCE.md # Technical implementation details
└── README.md # This file
import detectlib as dlib
# Initialize detector
detector = dlib.HandDetector(min_detection_confidence=0.7)
# Detect hands in frame
frame = detector.detect_hands(frame, draw_rect=True)
# Get hand landmarks
landmarks = detector.find_position(frame, draw=False)
# Check finger states
fingers = detector.fingers_up() # [thumb, index, middle, ring, pinky]detect_hands()- Detect and draw hand bounding boxesfind_position()- Get landmark coordinatesfingers_up()- Determine which fingers are extendeddraw_circle()- Draw markers at specific landmarkshands_count()- Count detected hands
import cv2
import detectlib as dlib
detector = dlib.HandDetector()
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if ret:
frame = detector.detect_hands(frame)
cv2.imshow('Hand Tracking', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()fingers = detector.fingers_up()
if fingers[1] and fingers[2]:
print("Selection mode - Two fingers up")
elif fingers[1] and not fingers[2]:
print("Drawing mode - Index finger only")- Python 3.7+
- OpenCV 4.5+
- MediaPipe 0.8.9+
- NumPy 1.19+
- Webcam (built-in or external)
- Hand not detected: Improve lighting, move closer to camera
- Choppy drawing: Move finger more slowly, ensure good lighting
- Colors not changing: Make sure both fingers are clearly raised in selection mode
See the User Guide for detailed troubleshooting steps.
The application can be easily customized:
- Brush settings: Modify
brush_thicknessanderaser_thickness - Colors: Add new colors to the palette
- Camera settings: Adjust resolution and FPS
- Detection sensitivity: Tune confidence thresholds
See the Technical Reference for implementation details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests and documentation
- Submit a pull request
This project is open source and available under the MIT License.
- MediaPipe - Google's framework for building multimodal applied ML pipelines
- OpenCV - Open source computer vision library
- Community - Thanks to all contributors and users
Happy Drawing! 🎨✨