通过mediapipe识别摄像头手部并框选的代码
时间: 2024-02-28 17:57:23 浏览: 84
以下是使用 Mediapipe 框选摄像头手部的示例代码:
```python
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0)
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
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = hands.process(image)
image.flags.writeable = True
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)
cv2.imshow('MediaPipe Hands', image)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
```
这段代码使用了 MediaPipe 的 Hands 模型来检测手部关键点,并使用 OpenCV 在摄像头中框选出手部区域。其中,`min_detection_confidence` 和 `min_tracking_confidence` 分别控制检测和跟踪手部的置信度,可以根据需要进行调整。
阅读全文