DEV Community

Cover image for Creating a Real-Time Hand Tracking Application with MediaPipe and OpenCV
Prince
Prince

Posted on

2 1 1 1 1

Creating a Real-Time Hand Tracking Application with MediaPipe and OpenCV

Image Sample
Welcome to an exciting journey into the world of computer vision! In this blog, we'll guide you through building a real-time hand tracking application using two powerful Python libraries: MediaPipe and OpenCV. By the end of this page, you'll be able to create an application that can detect and track hand landmarks in a video stream, paving the way for interactive experiences, gesture recognition, and more.

Prerequisites
Before we begin, ensure that you have both the mediapipe and opencv-python libraries installed. You can install them using the following command:

pip install mediapipe opencv-python
Enter fullscreen mode Exit fullscreen mode

Step 1: Initializing the Environment
Let's start by importing the necessary libraries and initializing some variables:

import mediapipe as mp
import cv2
import time

# Initialize the video capture
cap = cv2.VideoCapture(0)

# Initialize the hand tracking model
mpHands = mp.solutions.hands
hands = mpHands.Hands()

# Initialize the drawing utility
mpDraw = mp.solutions.drawing_utils

# Initialize time variables
pTime = 0
cTime = 0
Enter fullscreen mode Exit fullscreen mode

Step 2: The Tracking Loop
Next, we'll create an infinite loop to continuously process frames from the video capture and perform hand tracking:

while True:
    success, img = cap.read()

    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    result = hands.process(imgRGB)

    if result.multi_hand_landmarks:
        for handLms in result.multi_hand_landmarks:
            mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)

    cTime = time.time()
    fps = 1 / (cTime - pTime)
    pTime = cTime

    cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
    cv2.imshow("Hand Tracking", img)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
Enter fullscreen mode Exit fullscreen mode

Step 3: Wrapping Up
Lastly, we need to release the video capture resources and close the OpenCV windows when we're done:

cap.release()
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

Conclusion
Congratulations! You've successfully built a real-time hand tracking application using MediaPipe and OpenCV. This application can detect and visualize hand landmarks while displaying the frames per second (FPS) on the video stream. Now you have a solid foundation to explore further and integrate hand tracking into various projects, from virtual reality interactions to creative gesture recognition applications.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay