feat(接口2): C端生发发际线预览(真实实现,替换Mock)
第一步:按性别把发际线类型贴图渲染到照片,输出 N 张发际线叠加预览图。 - hairline/render.py: 解析 face_ext.obj(502 UV + 64 ribbon扩展面) + OpenCV 逐三角 仿射 warp 渲染器;关键修复——face_ext.obj 是 OBJ序,用 INDEX_MAP_468 把 MP序 502点重排成 OBJ序后再投影,否则 ribbon 会错贴到中脸 - hairline/service.py: FaceLandmarker+SegFormer 单例 + 性别贴图映射(扫描去空格) + generate_previews 管线(female5/male4) - 集成点修复: face_landmarks DEFAULT_MODEL_PATH 改 hairline/models/; constants HF_FACE_PARSER_MODEL 改本地路径(离线) - app.py: /api/v1/hair/grow 接真实实现,gender 必填(非法→1004),返回 results[].image_base64(不落盘),校验/鉴权同接口1;lifespan 预热接口2单例; 补 logging.basicConfig - 依赖: transformers==4.45.2;SegFormer 权重走 hf-mirror 下载(见 OFFLINE_ASSETS) - 测试: tests/test_hairline.py(mesh/重排/贴图映射) + test_api 接口2用例,31 全绿 注:SegFormer 受 5090/torch 限制走 CPU(~2.5s/张),换 cu128 可 SEG_DEVICE=cuda。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -72,6 +72,27 @@ def test_corrupt_1008(client):
|
||||
assert r.json()["code"] == 1008
|
||||
|
||||
|
||||
GROW = "/api/v1/hair/grow"
|
||||
|
||||
|
||||
def test_grow_missing_gender_1004(client):
|
||||
files = {"image_file": ("frontal.jpg", open(fixture("frontal.jpg"), "rb"), "application/octet-stream")}
|
||||
r = client.post(GROW, headers=H, files=files)
|
||||
assert r.json()["code"] == 1004
|
||||
|
||||
|
||||
def test_grow_female_returns_5(client):
|
||||
files = {"image_file": ("frontal.jpg", open(fixture("frontal.jpg"), "rb"), "application/octet-stream")}
|
||||
r = client.post(GROW, headers=H, files=files, data={"gender": "female"})
|
||||
body = r.json()
|
||||
assert body["code"] == 0, body
|
||||
results = body["data"]["results"]
|
||||
assert [x["hairline_type"] for x in results] == ["ellipse", "flower", "heart", "straight", "wave"]
|
||||
assert [x["order"] for x in results] == [1, 2, 3, 4, 5]
|
||||
assert base64.b64decode(results[0]["image_base64"])[:8] == b"\x89PNG\r\n\x1a\n"
|
||||
assert "image_url" not in results[0]
|
||||
|
||||
|
||||
def test_success_structure(client):
|
||||
r = _post(client, "frontal.jpg")
|
||||
body = r.json()
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
"""接口2 单元测试:mesh 解析 / MP→OBJ 重排 / 性别贴图映射(不需 SegFormer)。"""
|
||||
import numpy as np
|
||||
|
||||
from hairline.render import load_ext_mesh, mp_order_to_obj_order
|
||||
from hairline.service import get_texture_map
|
||||
from hairline import constants as C
|
||||
|
||||
|
||||
def test_ext_mesh():
|
||||
uv, ext_faces = load_ext_mesh()
|
||||
assert uv.shape == (502, 2)
|
||||
assert len(ext_faces) == 64 # ribbon 扩展三角形
|
||||
# 扩展面至少含一个 ≥468 的顶点
|
||||
assert all(max(f) >= 468 for f in ext_faces)
|
||||
|
||||
|
||||
def test_mp_to_obj_anchor_mapping():
|
||||
"""OBJ 序重排后,ribbon 引用的 obj 锚点应映射回 MP_TOP_ANCHORS。"""
|
||||
pts = np.zeros((502, 3), np.float32)
|
||||
pts[:468, 0] = np.arange(468) # 用 x 编码 MP 索引
|
||||
obj = mp_order_to_obj_order(pts)
|
||||
_uv, ext_faces = load_ext_mesh()
|
||||
obj_anchor_ids = sorted({pi for f in ext_faces for pi in f if pi < 468})
|
||||
mapped = sorted(int(obj[i, 0]) for i in obj_anchor_ids) # 还原成 MP 索引
|
||||
assert mapped == sorted(C.MP_TOP_ANCHORS)
|
||||
|
||||
|
||||
def test_texture_map():
|
||||
m = get_texture_map()
|
||||
assert [k for k, _ in m["female"]] == ["ellipse", "flower", "heart", "straight", "wave"]
|
||||
assert [k for k, _ in m["male"]] == ["ellipse", "inverse_arc", "m", "straight"]
|
||||
Reference in New Issue
Block a user