Files
hair/hairline/face_landmarks.py
T
xsl 5b2b73f5d5 feat: 新增52号发际线接口并限制GPU worker来源IP
保留5号历史三档实现,新增中下庭高度定位、扶正裁切、矩形叠加与30案例对比页。

worker仅允许本机和配置白名单访问;网关不增加IP限制。空图片URL字段改写限定为52号,保持旧接口兼容。同步接口、裁切与部署文档。

验证:113项测试通过;1项历史数值基线失败,在未修改的a3fddbb版本复现。浏览器验证5/52及30案例对比页通过。
2026-11-04 10:20:29 +08:00

105 lines
4.0 KiB
Python

"""MediaPipe FaceMesh wrappers returning 468 3D landmarks in [0,1] x/y space.
`FaceLandmarker` uses the modern mediapipe.tasks.vision API and requires
models/face_landmarker.task.
`SolutionsFaceLandmarker` uses the older mediapipe.solutions.face_mesh API.
It is useful as a CPU fallback in WSL environments where the Tasks API may
segfault while initializing EGL/OpenGL.
"""
from __future__ import annotations
import os
import numpy as np
# 模型在 hairline/models/ 下(模块即在 hairline/ 根),故只取一层 dirname。
DEFAULT_MODEL_PATH = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"models", "face_landmarker.task",
)
class FaceLandmarker:
def __init__(self, static_image_mode: bool = True, model_path: str | None = None):
import mediapipe as mp
from mediapipe.tasks import python as mp_python
from mediapipe.tasks.python import vision
path = model_path or DEFAULT_MODEL_PATH
if not os.path.isfile(path):
raise FileNotFoundError(
f"face_landmarker.task not found at {path}. "
"Download it from "
"https://storage.googleapis.com/mediapipe-models/face_landmarker/"
"face_landmarker/float16/1/face_landmarker.task"
)
running_mode = vision.RunningMode.IMAGE if static_image_mode else vision.RunningMode.VIDEO
options = vision.FaceLandmarkerOptions(
base_options=mp_python.BaseOptions(model_asset_path=path),
running_mode=running_mode,
num_faces=1,
output_face_blendshapes=False,
output_facial_transformation_matrixes=False,
)
self._detector = vision.FaceLandmarker.create_from_options(options)
self._mp = mp
def detect(self, image_rgb: np.ndarray, include_iris: bool = False) -> np.ndarray | None:
"""Returns (468, 3), or (478, 3) with iris, normalized float32 points.
The task model produces 478 landmarks (468 face + 10 iris). By default
only the first 468 match the SDK's canonical FaceMesh topology;
include_iris=True retains all 478 for face-measurement calibration.
"""
mp_image = self._mp.Image(image_format=self._mp.ImageFormat.SRGB, data=image_rgb)
result = self._detector.detect(mp_image)
if not result.face_landmarks:
return None
lm = result.face_landmarks[0] if include_iris else result.face_landmarks[0][:468]
arr = np.array([[p.x, p.y, p.z] for p in lm], dtype=np.float32)
return arr
def close(self):
try:
self._detector.close()
except Exception:
pass
class SolutionsFaceLandmarker:
"""CPU-oriented fallback using mediapipe.solutions.face_mesh."""
def __init__(self, static_image_mode: bool = True):
import mediapipe as mp
try:
face_mesh_module = mp.solutions.face_mesh
except AttributeError as exc:
raise RuntimeError(
"当前 mediapipe 包不包含 mediapipe.solutions.face_mesh。"
"请使用 web_service.py 的默认 parsing backend,或安装包含 solutions API 的 mediapipe 版本。"
) from exc
self._face_mesh = face_mesh_module.FaceMesh(
static_image_mode=static_image_mode,
max_num_faces=1,
refine_landmarks=False,
min_detection_confidence=0.5,
)
def detect(self, image_rgb: np.ndarray) -> np.ndarray | None:
"""Returns (468, 3) float32 of normalized x, y and relative z, or None."""
image_rgb.flags.writeable = False
result = self._face_mesh.process(image_rgb)
image_rgb.flags.writeable = True
if not result.multi_face_landmarks:
return None
lm = result.multi_face_landmarks[0].landmark[:468]
return np.array([[p.x, p.y, p.z] for p in lm], dtype=np.float32)
def close(self):
try:
self._face_mesh.close()
except Exception:
pass