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.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

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

Okay