Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hand Finger Counter

A real-time hand tracking application that detects your hand using your webcam and counts the number of fingers you're showing (0-5).

Hand Finger Counter Demo OpenCV MediaPipe

Features

  • 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

Requirements

  • Python 3.7 or higher
  • Webcam

Installation

1. Clone or download this repository

cd camera_watcher

2. Create a virtual environment (recommended)

Windows:

python -m venv venv
venv\Scripts\activate

macOS/Linux:

python3 -m venv venv
source venv/bin/activate

3. Install dependencies

pip install -r requirements.txt

This will install:

  • opencv-python - For webcam access and image processing
  • mediapipe - For hand tracking and landmark detection

Usage

Running the application

Simply run the main script:

python main.py

Controls

  • Show your hand to the webcam to see the finger count
  • Press 'q' to quit the application

Tips for best results

  1. Lighting: Make sure you have good lighting for better hand detection
  2. Background: A plain or contrasting background works best
  3. Distance: Keep your hand at a comfortable distance from the camera (30-50 cm)
  4. Orientation: Face your palm towards the camera with fingers clearly visible

How it works

Finger Detection Logic

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).

Code Structure

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

Module Documentation

hand_detector.py

Contains the HandDetector class with the following methods:

  • find_hands(img, draw=True) - Detects hands and draws landmarks
  • find_position(img, hand_no=0) - Returns landmark positions and bounding box
  • count_fingers(lm_list) - Counts extended fingers (0-5)
  • draw_bounding_box(img, bbox, finger_count) - Draws bounding box around hand
  • close() - Releases MediaPipe resources

main.py

Main application loop that:

  1. Initializes webcam and hand detector
  2. Processes video frames in real-time
  3. Displays results with FPS counter
  4. Handles user input for quitting

Troubleshooting

Webcam not opening

  • Make sure no other application is using your webcam
  • Check if your webcam is properly connected
  • Try changing the camera index in main.py from 0 to 1 or 2

Low FPS or lag

  • Close other resource-intensive applications
  • Reduce the webcam resolution in main.py if needed
  • Lower the detection_confidence and tracking_confidence values

Incorrect finger counting

  • 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.py if needed

Customization

Adjust webcam resolution

In main.py, modify these lines:

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

Adjust detection confidence

In main.py, modify the detector initialization:

detector = HandDetector(max_hands=1, detection_confidence=0.7, tracking_confidence=0.5)

Detect multiple hands

Change max_hands parameter:

detector = HandDetector(max_hands=2)

License

This project is open source and available for educational and personal use.

Acknowledgments

Contributing

Feel free to fork this project and submit pull requests for any improvements!


Made with ❤️ using Python, OpenCV, and MediaPipe

About

"Real-time hand tracking application that counts fingers (0-5) using MediaPipe and OpenCV

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages