像 cv2.boundingrect 一样围绕从 mediapipe 手界标检测返回的所有点创建一个矩形

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

我正在使用以下代码使用 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("\n\n\n")
    cv2.imshow("Frame", frame)

    cv2.waitKey(1)

我只想在检测器返回的所有点周围画一个矩形 请告诉是否有任何方法可以内置 mediapipe 或使用 opencv

  1. while循环之前,确定每帧的宽度和高度为:
_, frame = cap.read()

h, w, c = frame.shape
  1. 对于检测到的每个 landLM,定义最小 xy 坐标以及最大 xy 坐标的初始变量。前两个变量稍后将作为矩形的起点,最后两个变量稍后将作为矩形的最后一个点:
            x_max = 0
            y_max = 0
            x_min = w
            y_min = h
  1. 遍历handLM变量,找到手的每个点的xy坐标:
            for lm in handLMs.landmark:
                x, y = int(lm.x * w), int(lm.y * h)
  1. 在检测到新坐标时更新最小和最大 xy 变量:
                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. 最后,用点画出矩形:
            cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)

一共:

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)