face_landmarks_multi_face.py

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

import csi
import time
import ml
from ml.preprocessing import Normalization
from ml.postprocessing.mediapipe import BlazeFace
from ml.postprocessing.mediapipe import FaceLandmarks

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

# BlazeFaceで最良の結果を得るには正方形の画像が必要です。
# FaceLandmarksは、BlazeFaceでクロップされた非正方形の画像でも動作します。
csi0.window((400, 400))

# 内蔵の顔検出モデルをロード
face_detection = ml.Model("/rom/blazeface_front_128.tflite", postprocess=BlazeFace(threshold=0.4))
print(face_detection)

# 内蔵の顔ランドマーク検出モデルをロード
face_landmarks = ml.Model("/rom/face_landmarks_192.tflite", postprocess=FaceLandmarks(threshold=0.4))
print(face_landmarks)

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

    # facesは((x, y, w, h), score, keypoints)タプルのリストです
    for r, score, keypoints in face_detection.predict([img]):
        # rect は (x, y, w, h) - 顔のランドマークモデル用に2倍に拡大
        wider_rect = (r[0] - r[2] // 2, r[1] - r[3] // 2, r[2] * 2, r[3] * 2)
        # 検出された顔のROIのみを対象に処理
        n = Normalization(roi=wider_rect)

        # marks は ((x, y, w, h), score, keypoints) タプルのリストです
        for r, score, keypoints in face_landmarks.predict([n(img)]):
            ml.utils.draw_predictions(img, [r], ("face",), ((0, 0, 255),), format=None)

            # keypoints は形状 (468, 3) の ndarrayで、各キーポイントは (x, y, z) です
            ml.utils.draw_keypoints(img, keypoints, radius=0, color=(255, 0, 0))

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

results matching ""

    No results matching ""