This Python script implements a hand-tracking module using the Mediapipe library and OpenCV. The module is encapsulated in a class named handDetectors
. It provides functionalities for detecting hands in a video feed, drawing landmarks on the detected hands, and extracting the 2D coordinates of specific landmarks.
- Python 3.x
- OpenCV
- Mediapipe
Install the required libraries using:
pip install opencv-python
pip install mediapipe
- Clone the repository or download the script.
git clone https://github.com/devDurgeshK/handTrackingModule.git
cd handTrackingModule
- Run the script:
python main.py
- Press 'q' to exit the program.
The handDetectors
class can be configured by adjusting the initialization parameters in the script:
mode
: Hand tracking mode (default: False)maxHands
: Maximum number of hands to detect (default: 2)modelComplexity
: Model complexity (default: 1)detectionCon
: Detection confidence threshold (default: 0.5)trackCon
: Tracking confidence threshold (default: 0.5)
-
Hand Detection:
- The
findHands
method processes the input image to detect hands and, if specified, draws landmarks and connections on the hands.
- The
-
Landmark Position Extraction:
- The
findPosition
method extracts the 2D coordinates of specific landmarks on the detected hand.
- The
-
Display and Interaction:
- The script displays the processed image with detected hands and landmark information.
- It also calculates and displays the frames per second.
Feel free to contribute to the project by submitting issues or pull requests. Your feedback and enhancements are welcomed!
Below are the documentation sections for the findHands
and findPosition
methods in the handDetectors
class.
The findHands
method processes an input image to detect hands using the configured parameters. If specified, it also draws landmarks and connections on the detected hands.
def findHands(self, img, draw=True):
img
: Input image (numpy array) in BGR format.draw
: Boolean flag to indicate whether to draw landmarks and connections on the detected hands (default: True).
The method returns the input image with detected hands and landmarks if drawing is enabled.
# Create an instance of handDetectors
detector = handDetectors()
# Process an image to detect hands and draw landmarks
img_with_hands = detector.findHands(input_image)
The findPosition
method extracts the 2D coordinates of specific landmarks on a detected hand.
def findPosition(self, img, handNum=0, draw=False):
img
: Input image (numpy array) in BGR format.handNum
: Index of the hand to extract landmarks from (default: 0).draw
: Boolean flag to indicate whether to draw circles around the landmarks (default: False).
The method returns a list of landmark information, where each element is a list [id, cx, cy]
representing the landmark ID and its 2D coordinates.
# Create an instance of handDetectors
detector = handDetectors()
# Process an image to detect hands
img_with_hands = detector.findHands(input_image)
# Extract landmark information for the first hand
landmark_info = detector.findPosition(img_with_hands, handNum=0)