openmv/scripts/examples/03-Machine-Learning/00-TensorFlow/blazepalm_detection.py
2025-10-21 21:53:41 -07:00

55 lines
1.8 KiB
Python

# This work is licensed under the MIT license.
# Copyright (c) 2013-2025 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# This example shows off Google's MediaPipe Palm Detection model.
#
# NOTE: This exaxmple requires an OpenMV Cam with an NPU like the AE3 or N6 to run real-time.
import csi
import time
import ml
from ml.postprocessing.mediapipe import BlazePalm
# Initialize the sensor.
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.VGA)
csi0.window((400, 400))
# Load built-in palm detection model
model = ml.Model("/rom/palm_detection_full_192.tflite", postprocess=BlazePalm(threshold=0.4))
print(model)
# Line connections between hand joints for drawing the hand skeleton.
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 is a list of ((x, y, w, h), score, keypoints) tuples
palms = model.predict([img])
# Draw bounding boxes around the detected palms and keypoints.
if palms:
for r, score, keypoints in palms[0]:
ml.utils.draw_predictions(img, [r], ("palm",), ((0, 0, 255),), format=None)
# keypoints is a ndarray of shape (7, 2)
# 0 - wrist (x, y)
# 1 - index finger mcp (x, y)
# 2 - middle finger mcp (x, y)
# 3 - ring finger mcp (x, y)
# 4 - pinky mcp (x, y)
# 5 - thumb cmc (x, y)
# 6 - thumb mcp (x, y)
#
# mcp = Metacarpophalangeal Joint - the knuckle
# cmc = Carpometacarpal Joint - the base of the thumb
ml.utils.draw_skeleton(img, keypoints, palm_lines, kp_color=(255, 0, 0), line_color=(0, 255, 0))
print(clock.fps(), "fps")