- Before the
while
loop, determine the width and height each frame will be:
_, frame = cap.read()
h, w, c = frame.shape
- 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
- 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)
- 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
- 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)