blazepalm_detection.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.postprocessing.mediapipe import BlazePalm

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

# BlazePalm需要方形图像以获得最佳效果。
csi0.window((400, 400))

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

# 手部关节之间的连线,用于绘制手部骨架。
palm_lines = ((0, 1), (1, 2), (2, 3), (3, 4), (4, 0), (0, 5), (5, 6))

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

    # palms是由((x, y, w, h), score, keypoints)元组组成的列表
    for r, score, keypoints in model.predict([img]):
        ml.utils.draw_predictions(img, [r], ("palm",), ((0, 0, 255),), format=None)

        # keypoints是形状为(7, 2)的ndarray
        # 0 - 手腕(x, y)
        # 1 - 食指掌指关节(x, y)
        # 2 - 中指掌指关节(x, y)
        # 3 - 无名指掌指关节(x, y)
        # 4 - 小指掌指关节(x, y)
        # 5 - 拇指腕掌关节(x, y)
        # 6 - 拇指掌指关节(x, y)
        #
        # mcp = 掌指关节——指根关节
        # cmc = 腕掌关节——拇指根部
        ml.utils.draw_skeleton(img, keypoints, palm_lines, kp_color=(255, 0, 0), line_color=(0, 255, 0))

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

results matching ""

    No results matching ""