diff --git a/scripts/examples/03-Machine-Learning/00-TensorFlow/blazeface_face_detector.py b/scripts/examples/03-Machine-Learning/00-TensorFlow/blazeface_detector.py similarity index 73% rename from scripts/examples/03-Machine-Learning/00-TensorFlow/blazeface_face_detector.py rename to scripts/examples/03-Machine-Learning/00-TensorFlow/blazeface_detector.py index f44bb01b5..d859dc4db 100644 --- a/scripts/examples/03-Machine-Learning/00-TensorFlow/blazeface_face_detector.py +++ b/scripts/examples/03-Machine-Learning/00-TensorFlow/blazeface_detector.py @@ -2,12 +2,12 @@ # Copyright (c) 2013-2025 OpenMV LLC. All rights reserved. # https://github.com/openmv/openmv/blob/master/LICENSE # -# This example shows off Google's MediaPipe BlazeFace face detection model. +# This example shows off Google's MediaPipe Face Detection model. import csi import time import ml -from ml.postprocessing import mediapipe_face_detection_postprocess +from ml.postprocessing.mediapipe import BlazeFace # Initialize the sensor. csi0 = csi.CSI() @@ -22,7 +22,12 @@ print(model) # Create the face detection post-processor. This post-processor dynamically # generates anchors for the model input size which should only be done once. -face_detection_postprocess = mediapipe_face_detection_postprocess(threshold=0.6) +face_detection_postprocess = BlazeFace(threshold=0.6) + +# Visualization parameters. +face_labels = ["face"] +face_colors = [(0, 0, 255)] +keypoint_color = (255, 0, 0) clock = time.clock() while True: @@ -35,7 +40,8 @@ while True: # Draw bounding boxes around the detected faces and keypoints. if faces: for r, score, keypoints in faces[0]: - ml.utils.draw_predictions(img, [r], ["face"], [(0, 0, 255)], format=None) + ml.utils.draw_predictions(img, [r], face_labels, face_colors, format=None) + # keypoints is a ndarray of shape (6, 2) # 0 - right eye (x, y) # 1 - left eye (x, y) @@ -43,7 +49,6 @@ while True: # 3 - mouth (x, y) # 4 - right ear (x, y) # 5 - left ear (x, y) - for kp in keypoints.tolist(): - img.draw_circle(int(kp[0]), int(kp[1]), 4, color=(255, 0, 0)) + ml.utils.draw_keypoints(img, keypoints, color=keypoint_color) print(clock.fps(), "fps") diff --git a/scripts/libraries/ml/ml-core/ml/utils.py b/scripts/libraries/ml/ml-core/ml/utils.py index b481919b1..65e6732e0 100644 --- a/scripts/libraries/ml/ml-core/ml/utils.py +++ b/scripts/libraries/ml/ml-core/ml/utils.py @@ -195,3 +195,27 @@ def draw_predictions( color=box_color, ) image.draw_string(x, y - font_height, label.upper(), text_color) + + +def draw_keypoints( + image, + keypoints, + radius=4, + color=(255, 0, 0), + thickness=1, + fill=False, +): + for kp in keypoints: + image.draw_circle(int(kp[0]), int(kp[1]), radius, color=color, thickness=thickness, fill=fill) + + +def draw_keypoint_lines( + image, + keypoints, + lines, + color=(0, 255, 0), + thickness=1, +): + for line in lines: + image.draw_line(int(keypoints[line[0]][0]), int(keypoints[line[0]][1]), + int(keypoints[line[1]][0]), int(keypoints[line[1]][1]), color=color, thickness=thickness)