- 从head3d复制发际线检测管线到 hairline/ 包:MediaPipe Tasks + SegFormer分割 + 17锚点射线检测 + 502点mesh(face_ext.obj)+UV - 复制模型:face_landmarker.task(3.7MB)、SegFormer config/preprocessor (model.safetensors 340MB 单独下载中) - 新增 docs/接口2-C端生发-技术实现方案.md:第一步=发际线曲线叠加预览图, 新增gender必填参数,按性别贴图数量输出(female5/male4),hairline_type英文key, 服务端cv2逐三角形warp渲染器(head3d只有浏览器端Three.js渲染) - 接口文档.md 接口2章节同步:gender参数、输出语义、错误码说明 - hairline_texture/ 9张发际线贴图入库
68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
"""Standalone MediaPipe FaceLandmarker runner used by web_service.py.
|
|
|
|
Running MediaPipe Tasks in the same process as the Flask dev server can
|
|
segfault under WSL (D3D12 EGL backend). Spawning a fresh subprocess per
|
|
request keeps the web server alive and lets us inject WSL-friendly env
|
|
vars before any mediapipe import.
|
|
|
|
Usage:
|
|
python -m python._mediapipe_subprocess <image_path> <output_npy>
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
|
|
os.environ.setdefault("LIBGL_ALWAYS_SOFTWARE", "1")
|
|
os.environ.setdefault("MESA_LOADER_DRIVER_OVERRIDE", "llvmpipe")
|
|
os.environ.setdefault("GALLIUM_DRIVER", "llvmpipe")
|
|
os.environ.setdefault("MEDIAPIPE_DISABLE_GPU", "1")
|
|
os.environ.setdefault("EGL_PLATFORM", "surfaceless")
|
|
|
|
import numpy as np # noqa: E402
|
|
|
|
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
PROJECT_DIR = os.path.dirname(THIS_DIR)
|
|
if PROJECT_DIR not in sys.path:
|
|
sys.path.insert(0, PROJECT_DIR)
|
|
|
|
from python.face_landmarks import FaceLandmarker # noqa: E402
|
|
|
|
|
|
def main() -> int:
|
|
if len(sys.argv) != 3:
|
|
print(
|
|
"usage: python -m python._mediapipe_subprocess <image_path> <output_npy>",
|
|
file=sys.stderr,
|
|
)
|
|
return 2
|
|
|
|
image_path, output_path = sys.argv[1], sys.argv[2]
|
|
|
|
import cv2
|
|
|
|
bgr = cv2.imread(image_path)
|
|
if bgr is None:
|
|
print(f"could not read image: {image_path}", file=sys.stderr)
|
|
return 3
|
|
|
|
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
|
|
rgb = np.ascontiguousarray(rgb, dtype=np.uint8)
|
|
|
|
landmarker = FaceLandmarker(static_image_mode=True)
|
|
try:
|
|
landmarks = landmarker.detect(rgb)
|
|
finally:
|
|
landmarker.close()
|
|
|
|
if landmarks is None:
|
|
print("no face detected", file=sys.stderr)
|
|
return 4
|
|
|
|
np.save(output_path, landmarks.astype(np.float32))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|