Skip to main content

A powerful real-time hand tracking module using MediaPipe and OpenCV

Project description

Hand Tracking Module

PyPI version Python License: MIT

A powerful and easy-to-use Python package for real-time hand detection and tracking using MediaPipe and OpenCV. This module provides a simple interface to detect hands, track hand landmarks, and perform gesture recognition through your webcam.

Features

  • Real-time Hand Detection: Detect up to 2 hands simultaneously
  • Hand Landmark Tracking: Get precise coordinates of 21 hand landmarks
  • Finger Counting: Detect which fingers are up/down
  • Distance Measurement: Calculate distances between landmarks
  • Bounding Box Detection: Get hand bounding boxes
  • Customizable Parameters: Adjust detection confidence and tracking sensitivity
  • Easy Integration: Simple API for quick integration into your projects

Installation

Install the package using pip:

pip install tanay

Requirements

  • Python 3.8+
  • OpenCV
  • MediaPipe
  • NumPy

These dependencies will be automatically installed when you install the package.

Quick Start

Here's a simple example to get you started:

import cv2
from tanay.HandTrackingModule import handDetector

# Initialize the hand detector
detector = handDetector()

# Start video capture
cap = cv2.VideoCapture(0)

while True:
    success, img = cap.read()
    
    # Find hands in the image
    img = detector.findHands(img)
    
    # Get hand landmarks
    lmList, bbox = detector.findPosition(img)
    
    # Check if hands are detected
    if len(lmList) != 0:
        # Get finger status (which fingers are up)
        fingers = detector.fingersUp()
        print(f"Fingers up: {fingers}")
        
        # Count total fingers up
        totalFingers = fingers.count(1)
        print(f"Total fingers: {totalFingers}")
    
    cv2.imshow("Hand Tracking", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

API Reference

Class: handDetector

Constructor

handDetector(mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5)

Parameters:

  • mode (bool): Static image mode. If False, treats input as video stream
  • maxHands (int): Maximum number of hands to detect (1-2)
  • detectionCon (float): Minimum detection confidence (0.0-1.0)
  • trackCon (float): Minimum tracking confidence (0.0-1.0)

Methods

findHands(img, draw=True)

Detects hands in the image and optionally draws landmarks.

Parameters:

  • img: Input image (BGR format)
  • draw (bool): Whether to draw hand landmarks

Returns:

  • Modified image with hand landmarks drawn (if draw=True)
findPosition(img, handNo=0, draw=True)

Gets the positions of hand landmarks.

Parameters:

  • img: Input image
  • handNo (int): Hand index (0 for first hand, 1 for second)
  • draw (bool): Whether to draw landmark points

Returns:

  • lmList: List of landmark positions [[id, x, y], ...]
  • bbox: Bounding box coordinates [xmin, ymin, xmax, ymax]
fingersUp()

Determines which fingers are extended upward.

Returns:

  • List of 5 integers (0 or 1) representing finger status [thumb, index, middle, ring, pinky]
findDistance(p1, p2, img, draw=True, r=15, t=3)

Calculates distance between two landmarks.

Parameters:

  • p1, p2 (int): Landmark IDs
  • img: Input image
  • draw (bool): Whether to draw the distance line
  • r (int): Circle radius for endpoints
  • t (int): Line thickness

Returns:

  • length: Distance between points
  • img: Image with distance visualization
  • [x1, y1, x2, y2, cx, cy]: Coordinates of points and center

Hand Landmark IDs

The hand landmarks follow MediaPipe's convention:

Wrist: 0
Thumb: 1, 2, 3, 4
Index: 5, 6, 7, 8
Middle: 9, 10, 11, 12
Ring: 13, 14, 15, 16
Pinky: 17, 18, 19, 20

Examples

Example 1: Finger Counting

import cv2
from tanay.HandTrackingModule import handDetector

detector = handDetector()
cap = cv2.VideoCapture(0)

while True:
    success, img = cap.read()
    img = detector.findHands(img)
    lmList, bbox = detector.findPosition(img, draw=False)
    
    if len(lmList) != 0:
        fingers = detector.fingersUp()
        totalFingers = fingers.count(1)
        
        # Display finger count
        cv2.putText(img, f'Fingers: {totalFingers}', (10, 70), 
                   cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
    
    cv2.imshow("Finger Counter", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Example 2: Distance Measurement

import cv2
from tanay.HandTrackingModule import handDetector

detector = handDetector()
cap = cv2.VideoCapture(0)

while True:
    success, img = cap.read()
    img = detector.findHands(img)
    lmList, bbox = detector.findPosition(img, draw=False)
    
    if len(lmList) != 0:
        # Distance between thumb tip and index tip
        length, img, lineInfo = detector.findDistance(4, 8, img)
        print(f"Distance: {length}")
    
    cv2.imshow("Distance Measurement", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Applications

This hand tracking module can be used for:

  • Gesture Recognition: Build gesture-controlled applications
  • Virtual Mouse: Control mouse cursor with hand movements
  • Sign Language Recognition: Detect and interpret sign language
  • Augmented Reality: Add virtual objects that interact with hands
  • Accessibility Tools: Create hands-free interfaces
  • Games: Hand-controlled gaming experiences
  • Art and Creative Tools: Digital painting with hand gestures

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Tanay Baviskar

Acknowledgments

  • MediaPipe for the hand tracking solution
  • OpenCV for computer vision functionality

Changelog

Version 0.1.1

  • Initial release
  • Basic hand detection and tracking functionality
  • Finger counting capability
  • Distance measurement between landmarks
  • Bounding box detection

⭐ If you found this package helpful, please give it a star on PyPI!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tanay-0.1.3.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tanay-0.1.3-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file tanay-0.1.3.tar.gz.

File metadata

  • Download URL: tanay-0.1.3.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for tanay-0.1.3.tar.gz
Algorithm Hash digest
SHA256 f454edd40e95f00a5a2995ef4ecafa4d7e404eaecd657fa9f48599ae53a9e38b
MD5 cf4d7e6872f73973c27f32d1ad93c8d4
BLAKE2b-256 c04a7ebb6b11bbb5adc298aa18e3b7a6cdcf092ac8817dcfc4391132339827b9

See more details on using hashes here.

File details

Details for the file tanay-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: tanay-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for tanay-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a56a5dde84bf07d130b1d228a7949ee473972a0bced1b0a62df2895bde624f23
MD5 d31386bceffdc5416b193f53f67ceaa1
BLAKE2b-256 2aa4175a61e6be25f70c1245feb1f54debe4e7da7613d3f808e21246ce9f2b1c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page