BASIC HAND TRACKING USING PYTHON
Basically, the hand tracking is based on the 21 landmarks in 3D with multi-hand support, based on high-performance palm detection and hand landmark model.
The 21 hand landmarks will be like,
And, The output will be like this,
Packages to install :
- pip install opencv-python.
- pip install mediapipe.
Program :
# Hand Tracking with python
import cv2
import mediapipe as mp
import time
# Capturing video from the web cam
video = cv2.VideoCapture(0)
# Creating objects for hands
mpHands = mp.solutions.hands
hands = mpHands.Hands()
previousTime = 0
currentTime = 0
while True:
success, img = video.read()
# Converting BGR image to RGB image
RGBimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Getting landmark points and storing in multiHandLandmarks variable
result = hands.process(RGBimg)
multiHandLandmarks = result.multi_hand_landmarks
# This 'if' block will run when it's having the handlandmark points
if multiHandLandmarks:
for handLandmarks in multiHandLandmarks:
# Accessing each landmark in the hand
"""
for index, handLandmark in enumerate(handLandmarks.landmark):
h, w, c = img.shape # returns height, width, center
cx, cy = int(handLandmark.x * w), int(handLandmark.y * h)
if index:
cv2.circle(img, (cx, cy), 10, (255, 0, 255), cv2.FILLED)
"""
# Drawing and connecting the points
mp.solutions.drawing_utils.draw_landmarks(img, handLandmarks, mpHands.HAND_CONNECTIONS)
# calculating frames per second
currentTime = time.time()
fps = 1 / (currentTime - previousTime)
previousTime = currentTime
# This will show the fps value on screen
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
cv2.imshow("Image", img)
cv2.waitKey(1)
Comments