Example: 03-Machine-Learning/00-TensorFlow/movenet_singlepose_detection.py
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 = model.predict([img])
for r, score, keypoints in joints:
ml.utils.draw_predictions(img, [r], ("person",), ((0, 0, 255),), format=None)
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")