import numpy as np import cv2 try: import mediapipe as mp _HAS_MP = True except Exception: _HAS_MP = False def segment_image(img_np): """Returns binary mask for hair/head.""" h, w = img_np.shape[:2] mask = np.zeros((h, w), dtype=np.uint8) center = (w // 2, int(h * 0.38)) axes = (int(w * 0.28), int(h * 0.33)) cv2.ellipse(mask, center, axes, 0, 0, 360, 255, -1) return mask def estimate_landmarks(img_np): if not _HAS_MP: return None mp_face = mp.solutions.face_mesh with mp_face.FaceMesh(static_image_mode=True, max_num_faces=1) as fm: results = fm.process(cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)) if not results.multi_face_landmarks: return None lm = results.multi_face_landmarks[0] xs = [p.x for p in lm.landmark[:10]] ys = [p.y for p in lm.landmark[:10]] return {"forehead_anchor": (float(np.mean(xs)), float(np.mean(ys)))}