face_landmarks_single_face.py
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)
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)
n = None
clock = time.clock()
while True:
clock.tick()
img = csi0.snapshot()
if n is None:
for r, score, keypoints in face_detection.predict([img]):
wider_rect = (r[0] - r[2] // 2, r[1] - r[3] // 2, r[2] * 2, r[3] * 2)
n = Normalization(roi=wider_rect)
else:
marks = face_landmarks.predict([n(img)])
if not marks:
n = None
continue
for r, score, keypoints in marks:
ml.utils.draw_predictions(img, [r], ("face",), ((0, 0, 255),), format=None)
ml.utils.draw_keypoints(img, keypoints, radius=0, color=(255, 0, 0))
new_wider_rect = (r[0] + (r[2] // 2) - (wider_rect[2] // 2),
r[1] + (r[3] // 2) - (wider_rect[3] // 2),
wider_rect[2],
wider_rect[3])
n = Normalization(roi=new_wider_rect)
print(clock.fps(), "fps")