Finger Counter
π Overview
Count in real time how many fingers are held up in front of the camera and show the number on screen.
The approach is a classic: use the geometry between the palm and the finger joints to decide whether each finger is extended - the index, middle, ring, and little finger are judged from the relative position of the fingertip against the preceding joint, and the thumb from the horizontal distance between its tip and the base of the index finger. It is the perfect beginner exercise in hand tracking plus simple logic.
π§° What you need
- Computer + camera
- Python 3.7+ environment
- pip install: opencv-python, mediapipe, cvzone
π§ Step by step
Detect the hand
Use HandTrackingModule to get the 21 landmark coordinates
Write down the tip and joint index for each finger
Judge the four fingers
Compare the y coordinate of the fingertip against the previous joint
A finger is extended when the tip is above it (smaller y)
Judge the thumb
Compare the x coordinate of the thumb tip against the base of the index finger
Branch on the hand orientation to decide extended or curled
Count and display
Total up the extended fingers
Draw the number on the frame in a large font
Handle the edge cases
Fine-tune the logic for a palm facing away from the camera
Test a range of gestures to confirm the count is right
Extend it
You can feed it into a scoring game or gesture commands
Pass the count to another program as a control signal
π‘ Tips
- It assumes the palm faces the camera; when you turn the hand over the thumb test has to be inverted
- Curled fingers occlude the joints, so raise the detection confidence if needed
- Two hands let you build a 'higher card wins' mini game