A real-time hand tracking application that detects your hand using your webcam and counts the number of fingers you're showing (0-5).
- Real-time hand detection using MediaPipe
- Finger counting from 0 to 5
- Visual feedback with hand landmarks and bounding box
- FPS counter to monitor performance
- Mirror mode for intuitive interaction
- Clean and modular code structure
- Python 3.7 or higher
- Webcam
cd camera_watcherWindows:
python -m venv venv
venv\Scripts\activatemacOS/Linux:
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txtThis will install:
opencv-python- For webcam access and image processingmediapipe- For hand tracking and landmark detection
Simply run the main script:
python main.py- Show your hand to the webcam to see the finger count
- Press 'q' to quit the application
- Lighting: Make sure you have good lighting for better hand detection
- Background: A plain or contrasting background works best
- Distance: Keep your hand at a comfortable distance from the camera (30-50 cm)
- Orientation: Face your palm towards the camera with fingers clearly visible
The application uses MediaPipe's hand tracking to detect 21 landmarks on your hand. It then analyzes these landmarks to determine which fingers are extended:
- Thumb: Checks if the thumb tip is farther from the wrist than the thumb IP joint
- Other fingers: Checks if the fingertip is above (lower y-coordinate) the PIP joint
The total count of extended fingers gives you the number (0-5).
camera_watcher/
│
├── main.py # Main application with webcam loop
├── hand_detector.py # Hand detection and finger counting logic
├── requirements.txt # Python dependencies
└── README.md # This file
Contains the HandDetector class with the following methods:
find_hands(img, draw=True)- Detects hands and draws landmarksfind_position(img, hand_no=0)- Returns landmark positions and bounding boxcount_fingers(lm_list)- Counts extended fingers (0-5)draw_bounding_box(img, bbox, finger_count)- Draws bounding box around handclose()- Releases MediaPipe resources
Main application loop that:
- Initializes webcam and hand detector
- Processes video frames in real-time
- Displays results with FPS counter
- Handles user input for quitting
- Make sure no other application is using your webcam
- Check if your webcam is properly connected
- Try changing the camera index in
main.pyfrom0to1or2
- Close other resource-intensive applications
- Reduce the webcam resolution in
main.pyif needed - Lower the
detection_confidenceandtracking_confidencevalues
- Make sure your entire hand is visible in the frame
- Keep your hand flat with fingers clearly extended or closed
- Improve lighting conditions
- Adjust the detection logic in
hand_detector.pyif needed
In main.py, modify these lines:
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)In main.py, modify the detector initialization:
detector = HandDetector(max_hands=1, detection_confidence=0.7, tracking_confidence=0.5)Change max_hands parameter:
detector = HandDetector(max_hands=2)This project is open source and available for educational and personal use.
Feel free to fork this project and submit pull requests for any improvements!
Made with ❤️ using Python, OpenCV, and MediaPipe