File size: 951 Bytes
ffdd09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)))}