使用Python,mediapipe构建手部姿势探测器
- 1. 效果图
- 2. 手部标志
- 3. 源码
- 参考
这篇博客将介绍手部标志是什么,以及如何使用Python,mediapipe构建手部姿势探测器,
Mediapipe是一个跨平台的库,由谷歌开发,为计算机视觉任务提供惊人的现成的ML解决方案。
pip install mediapipe
1. 效果图
2. 手部标志
WRIST = 0
THUMB_CMC = 1
THUMB_MCP = 2
THUMB_IP = 3
THUMB_TIP = 4
INDEX_FINGER_MCP = 5
INDEX_FINGER_PIP = 6
INDEX_FINGER_DIP = 7
INDEX_FINGER_TIP = 8
MIDDLE_FINGER_MCP = 9
MIDDLE_FINGER_PIP = 10
MIDDLE_FINGER_DIP = 11
MIDDLE_FINGER_TIP = 12
RING_FINGER_MCP = 13
RING_FINGER_PIP = 14
RING_FINGER_DIP = 15
RING_FINGER_TIP = 16
PINKY_MCP = 17
PINKY_PIP = 18
PINKY_DIP = 19
PINKY_TIP = 20
3. 源码
# 手部姿势检测
# python hand_gesture_detect.py
import cv2
# 导入OpenCV和Mediapipe 库
import mediapipe as mp
# 初始化Mediapipe
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
# 初始化视频捕获
cap = cv2.VideoCapture(0)
# 检查相机是否成功打开
if not cap.isOpened():
print("Unable to open camera")
exit()
# 初始化Mediapipe Hands object,static_image_mode设置为false启动实时视频处理,max_num_hands设置要检测的最大手数,并设置min_detection_confidence检测的最小置信度阈值。
with mp_hands.Hands(static_image_mode=False, max_num_hands=2, min_detection_confidence=0.5) as hands:
while True:
# 启动无限循环以连续读取视频捕获中的帧
ret, frame = cap.read()
if not ret:
print("Error reading frame from camera")
break
# 转换BGR为RGB图像
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 使用Mediapipe处理帧
results = hands.process(frame_rgb)
# 帧上绘制手部姿势
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS,
mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=4),
mp_drawing.DrawingSpec(color=(255, 0, 0), thickness=2, circle_radius=2))
# 获得索引 12 和 9 处的红色圆圈的坐标 (x, y)。比较每个索引处的 Y 坐标,这样,如果索引 12 处的 Y 坐标低于索引处的 Y 坐标指数为 9,那么可以认为这手牌是闭合的,否则,它是打开的。
h, w, _ = frame.shape # h = frame height , w = frame width
x, y = int(hand_landmarks.landmark[12].x * w), int(hand_landmarks.landmark[12].y * h)
x1, y1 = int(hand_landmarks.landmark[9].x * w), int(hand_landmarks.landmark[9].y * h)
if y < y1:
hand_status = "Open"
cv2.rectangle(frame, (0, 0), (200, 60), (255, 0, 0), -1)
cv2.putText(frame, "Open Hand", (0, 35), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 3)
else:
hand_status = "Closed"
cv2.rectangle(frame, (0, 0), (200, 60), (255, 0, 0), -1)
cv2.putText(frame, "Closed Hand", (0, 35), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 3)
# 显示帧
cv2.imshow("Hand Detection", frame)
# 按下‘q’键退出循环
if cv2.waitKey(1) == ord("q"):
break
# 释放视频流指针,关闭打开的窗口
cap.release()
cv2.destroyAllWindows()
参考
- https://mp.weixin.qq.com/s/jXMnlx9DygZE-jzkYID9FA