hand_landmarks_single_hand.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2025 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 此示例展示了Google的MediaPipe单手手部关键点检测模型。
#
# 注意:此示例需要带NPU的OpenMV摄像头(如AE3或N6)才能实时运行。

import csi
import time
import ml
from ml.preprocessing import Normalization
from ml.postprocessing.mediapipe import BlazePalm
from ml.postprocessing.mediapipe import HandLandmarks

# 初始化传感器。
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.VGA)

# BlazePalm需要方形图像以获得最佳效果。
# HandLandmarks可以处理来自BlazePalm裁剪的非方形图像。
csi0.window((400, 400))

# 加载内置的手掌检测模型
palm_detection = ml.Model("/rom/palm_detection_full_192.tflite", postprocess=BlazePalm(threshold=0.4))
print(palm_detection)

# 加载内置的手部关键点模型
hand_landmarks = ml.Model("/rom/hand_landmarks_full_224.tflite", postprocess=HandLandmarks(threshold=0.4))
print(hand_landmarks)

# 手部关节之间的连线,用于绘制手部骨架。
hand_lines = ((0, 1), (1, 2), (2, 3), (3, 4), (0, 5), (5, 6), (6, 7), (7, 8),
              (5, 9), (9, 10), (10, 11), (11, 12), (9, 13), (13, 14), (14, 15), (15, 16),
              (13, 17), (17, 18), (18, 19), (19, 20), (0, 17))

# 追踪变量。
n = None

clock = time.clock()
while True:
    clock.tick()
    img = csi0.snapshot()

    if n is None:
        # palms是由((x, y, w, h), score, keypoints)元组组成的列表
        for r, score, keypoints in palm_detection.predict([img]):
            # rect为(x, y, w, h)——放大3倍供手部关键点模型使用
            wider_rect = (r[0] - r[2], r[1] - r[3], r[2] * 3, r[3] * 3)
            # 仅对检测到的手掌ROI区域进行处理
            n = Normalization(roi=wider_rect)

    else:
        # hands是由((x, y, w, h), score, keypoints)元组组成的列表
        # 索引0(如存在)为左手
        # 索引1(如存在)为右手
        hands = hand_landmarks.predict([n(img)])

        # 未检测到手,重置追踪器。
        if not hands:
            n = None
            continue

        # 在检测到的手周围绘制边界框和关键点。
        for i, detections in enumerate(hands):
            for r, score, keypoints in detections:
                ml.utils.draw_predictions(img, [r], ("right",) if i else ("left",), ((0, 0, 255),), format=None)

                # keypoints:手部关节的ndarray (21, 3),每个为(x, y, z)
                # 索引遵循MediaPipe约定:
                # 0:手腕
                # 拇指:1 cmc,2 mcp,3 ip,4 tip
                # 食指:5 mcp,6 pip,7 dip,8 tip
                # 中指:9 mcp,10 pip,11 dip,12 tip
                # 无名指:13 mcp,14 pip,15 dip,16 tip
                # 小指:17 mcp,18 pip,19 dip,20 tip
                # (cmc=掌根,mcp=掌指关节,pip=近端指间关节,dip=远端指间关节,ip=拇指指间关节,tip=指尖)
                ml.utils.draw_skeleton(img, keypoints, hand_lines, kp_color=(255, 0, 0), line_color=(0, 255, 0))

                # 将new_wider_rect以手为中心,用于追踪
                new_wider_rect = (r[0] + (r[2] // 2) - (wider_rect[2] // 2),
                                  r[1] + (r[3] // 2) - (wider_rect[3] // 2),
                                  wider_rect[2],
                                  wider_rect[3])
                # 仅对检测到的手ROI区域进行处理
                n = Normalization(roi=new_wider_rect)

    print(clock.fps(), "fps")

results matching ""

    No results matching ""