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
- Before the
whileloop, determine the width and height each frame will be:
_, frame = cap.read() h, w, c = frame.shape
- For each
landLMdetected, define the initial variables for smallestxandycoordinates, and the largestxandycoordinates. 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
- Loop through the
handLMvariable, and finding thexandycoordinates of each point of the hand:
for lm in handLMs.landmark:
x, y = int(lm.x * w), int(lm.y * h)
- Update the minimum and maximum
xandyvariables 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
- 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)