How to Install Mediapipe with Python?

Estimated read time 2 min read

To install the Mediapipe library with Python, you can follow these steps:

  1. First, make sure that you have Python installed on your system. You can download and install Python from the official website: https://www.python.org/downloads/
  2. Open a command prompt or terminal window on your system.
  3. Use pip (Python package manager) to install the Mediapipe library by running the following command:
pip install mediapipe
  1. Wait for the installation to complete. Once it is finished, you should be able to import and use the Mediapipe library in your Python scripts.

Here is an example of how to import and use the Mediapipe library to detect hand landmarks:

import mediapipe as mp
import cv2

# Initialize the hand tracking module
mp_hands = mp.solutions.hands

# Initialize the drawing utilities
mp_drawing = mp.solutions.drawing_utils

# Initialize the webcam
cap = cv2.VideoCapture(0)

# Process each frame from the webcam
with mp_hands.Hands(min_detection_confidence=0.5, min_tracking_confidence=0.5) as hands:
    while cap.isOpened():
        success, image = cap.read()
        if not success:
            print("Ignoring empty camera frame.")
            continue

        # Flip the image horizontally for a later selfie-view display
        image = cv2.flip(image, 1)

        # Convert the image to RGB
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        # Process the image to detect hands
        results = hands.process(image)

        # Draw hand landmarks on the image
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        if results.multi_hand_landmarks:
            for hand_landmarks in results.multi_hand_landmarks:
                mp_drawing.draw_landmarks(
                    image, hand_landmarks, mp_hands.HAND_CONNECTIONS)

        # Display the image
        cv2.imshow('MediaPipe Hands', image)
        if cv2.waitKey(5) & 0xFF == 27:
            break

# Release resources
cap.release()
cv2.destroyAllWindows()

This example uses the Mediapipe Hands module to detect and draw hand landmarks on each frame from the webcam. Note that you will also need to have OpenCV installed to run this code example.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply