face_landmarks_multi_face.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 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 ""