Skip to content
Advertisement

create a rectangle around all the points returned from mediapipe hand landmark detection just like cv2.boundingrect does

I am using following code to detect hand landmarks using mediapipe

import cv2
import mediapipe as mp

mphands = mp.solutions.hands
hands = mphands.Hands()
mp_drawing = mp.solutions.drawing_utils
cap = cv2.VideoCapture(0)

while True:
    _, frame = cap.read()
    framergb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    result = hands.process(framergb)
    hand_landmarks = result.multi_hand_landmarks
    if hand_landmarks:
        for handLMs in hand_landmarks:
            mp_drawing.draw_landmarks(frame, handLMs, mphands.HAND_CONNECTIONS)
            print("nnn")
    cv2.imshow("Frame", frame)

    cv2.waitKey(1)

I just want a rectangle around all the points returned by the detector Please tell if there is any way to do so built in mediapipe or using opencv

Advertisement

Answer

  1. Before the while loop, determine the width and height each frame will be:
_, frame = cap.read()

h, w, c = frame.shape
  1. For each landLM detected, define the initial variables for smallest x and y coordinates, and the largest x and y coordinates. The first two variables will later act as the starting point of the rectangle, and the last two variables will later act as the last point of the rectangle:
            x_max = 0
            y_max = 0
            x_min = w
            y_min = h
  1. Loop through the handLM variable, and finding the x and y coordinates of each point of the hand:
            for lm in handLMs.landmark:
                x, y = int(lm.x * w), int(lm.y * h)
  1. Update the minimum and maximum x and y variables as new coordinates are being detected:
                if x > x_max:
                    x_max = x
                if x < x_min:
                    x_min = x
                if y > y_max:
                    y_max = y
                if y < y_min:
                    y_min = y
  1. Finally, draw the rectangle with the points:
            cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)

Altogether:

import cv2
import mediapipe as mp

mphands = mp.solutions.hands
hands = mphands.Hands()
mp_drawing = mp.solutions.drawing_utils
cap = cv2.VideoCapture(0)

_, frame = cap.read()

h, w, c = frame.shape

while True:
    _, frame = cap.read()
    framergb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    result = hands.process(framergb)
    hand_landmarks = result.multi_hand_landmarks
    if hand_landmarks:
        for handLMs in hand_landmarks:
            x_max = 0
            y_max = 0
            x_min = w
            y_min = h
            for lm in handLMs.landmark:
                x, y = int(lm.x * w), int(lm.y * h)
                if x > x_max:
                    x_max = x
                if x < x_min:
                    x_min = x
                if y > y_max:
                    y_max = y
                if y < y_min:
                    y_min = y
            cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
            mp_drawing.draw_landmarks(frame, handLMs, mphands.HAND_CONNECTIONS)
    cv2.imshow("Frame", frame)

    cv2.waitKey(1)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement