movenet_singlepose_detection.py

# この作品はMITライセンスの下で提供されています。
# Copyright (c) 2013-2026 OpenMV LLC. 全著作権所有。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# この例は、GoogleのMoveNet Pose Estimationモデルを紹介します。
#
# 注記:この例をリアルタイムで実行するには、AE3やN6のようなNPU搭載のOpenMV Camが必要です。

import csi
import time
import ml
from ml.postprocessing.mediapipe import MoveNet

# センサーを初期化します。
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.VGA)

# 内蔵の姿勢検出モデルをロード
model = ml.Model("/rom/movenet_singlepose_192.tflite", postprocess=MoveNet(threshold=0.4))
print(model)

# 体の骨格を描画するための、体の関節間を結ぶ線。
body_lines = ((0, 1), (0, 2), (1, 3), (2, 4), (0, 5), (0, 6), (5, 6), (5, 7),
              (7, 9), (6, 8), (8, 10), (5, 11), (6, 12), (11, 12), (11, 13), (13, 15),
              (12, 14), (14, 16))


# 信頼度の低いキーポイントと、それらに接続する骨格線を除去します。
def filter_keypoints(keypoints, threshold=0.4):
    valid = {i for i, kp in enumerate(keypoints) if kp[2] > threshold}
    remap = {old: new for new, old in enumerate(sorted(valid))}
    f_keypoints = [kp for i, kp in enumerate(keypoints) if i in valid]
    f_body_lines = [(remap[a], remap[b]) for a, b in body_lines if a in valid and b in valid]
    return f_keypoints, f_body_lines


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

    # joints は ((x, y, w, h), score, keypoints) タプルのリストです
    joints = model.predict([img])

    # 検出された人物とキーポイントの周囲にバウンディングボックスを描画します。
    for r, score, keypoints in joints:
        ml.utils.draw_predictions(img, [r], ("person",), ((0, 0, 255),), format=None)

        # keypoints:体の関節の (x, y, score) を表す ndarray (17, 3)
        # インデックスはCOCOの規則に従います:
        # 0:鼻
        # 1:left_eye、2:right_eye
        # 3:left_ear、4:right_ear
        # 5:left_shoulder、6:right_shoulder
        # 7:left_elbow、8:right_elbow
        # 9:left_wrist、10:right_wrist
        # 11:left_hip、12:right_hip
        # 13:left_knee、14:right_knee
        # 15:left_ankle、16:right_ankle
        f_keypoints, f_body_lines = filter_keypoints(keypoints)
        ml.utils.draw_skeleton(img, f_keypoints, f_body_lines,
                               kp_color=(255, 0, 0), line_color=(0, 255, 0))

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

results matching ""

    No results matching ""