"""MediaPipe Face Landmarker: 478 landmarks (x, y normalised to the image, z relative) -> landmarks.npy
usage: python face_landmarks.py <image> <out.npy>"""
import sys, numpy as np, mediapipe as mp
from mediapipe.tasks import python as mpt
from mediapipe.tasks.python import vision
opts = vision.FaceLandmarkerOptions(base_options=mpt.BaseOptions(model_asset_path="/models/face_landmarker.task"),
                                    num_faces=1, output_face_blendshapes=False)
det = vision.FaceLandmarker.create_from_options(opts)
img = mp.Image.create_from_file(sys.argv[1])
res = det.detect(img)
assert res.face_landmarks, "no face found"
L = np.array([(p.x, p.y, p.z) for p in res.face_landmarks[0]], np.float32)
np.save(sys.argv[2], L); print("landmarks", L.shape, "x range", L[:, 0].min(), L[:, 0].max())
