feat(接口2): 新增生发后图片(ComfyUI/Flux inpaint)
在发际线预览基础上,每种发际线再出一张「植发3个月」生发图: - hairline/mask.py: headmark 5步法遮罩(额头上部区域∩SegFormer头部=ROI, 取发际线曲线以上闭合区域);用 hairline_texture_black 渲染黑线替代手绘检测; compose_comfy_rgba 合成 RGBA(alpha=255-mask, 透明=重绘区, 对齐 ComfyUI mask=1-alpha) - hairline/comfyui.py: ComfyUI 客户端(默认8182),/upload/image+/prompt(改节点26+随机seed) +轮询/history+/view 取回生发图 - hairline/render.py: 抽出 build_overlay_layer 供遮罩取曲线像素 - hairline/service.py: extract_context 一次出 landmarks/parse_map/502点; generate_grow_results 每种=预览+生发图(同步串行N张,单张ComfyUI失败则grown置空不拖垮整请求) - app.py: /hair/grow 返回 results[].grown_image_base64;重活放线程池避免卡事件循环 - add_hair.json 工作流 + hairline_texture_black/ 黑贴图入库 - 测试: test_mask.py(遮罩几何) + test_api mock ComfyUI 验 grown 字段,35 全绿 实测(5090): female 5张生发图同步约18s;预览/生发图人物五官服饰背景保持、黑线已清除。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+11
-1
@@ -81,7 +81,16 @@ def test_grow_missing_gender_1004(client):
|
||||
assert r.json()["code"] == 1004
|
||||
|
||||
|
||||
def test_grow_female_returns_5(client):
|
||||
_PNG_1x1 = base64.b64decode(
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
|
||||
)
|
||||
|
||||
|
||||
def test_grow_female_returns_5(client, monkeypatch):
|
||||
# mock ComfyUI:不依赖 8182、不跑 Flux,只验证管线接线 + grown 字段
|
||||
import hairline.comfyui as comfy
|
||||
monkeypatch.setattr(comfy, "run", lambda *a, **k: _PNG_1x1)
|
||||
|
||||
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()
|
||||
@@ -90,6 +99,7 @@ def test_grow_female_returns_5(client):
|
||||
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 base64.b64decode(results[0]["grown_image_base64"])[:8] == b"\x89PNG\r\n\x1a\n"
|
||||
assert "image_url" not in results[0]
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
"""接口2 第二步 遮罩几何单元测试(纯 numpy/cv2,无需 SegFormer/torch)。"""
|
||||
import numpy as np
|
||||
|
||||
from hairline.mask import (
|
||||
forehead_upper_region, head_silhouette, _above_curve_region,
|
||||
compose_comfy_rgba, FOREHEAD_LANDMARKS,
|
||||
)
|
||||
|
||||
|
||||
def test_forehead_upper_region():
|
||||
w = h = 200
|
||||
lm = np.zeros((468, 3), np.float32)
|
||||
for i in FOREHEAD_LANDMARKS: # 额头边界点都放在 y=0.4
|
||||
lm[i, 0] = 0.5
|
||||
lm[i, 1] = 0.4
|
||||
m = forehead_upper_region(lm, w, h)
|
||||
assert m[10, 100] == 255 # 上方在区域内
|
||||
assert m[150, 100] == 0 # 下方(0.75h)在区域外
|
||||
|
||||
|
||||
def test_head_silhouette():
|
||||
pm = np.zeros((50, 50), np.int32)
|
||||
pm[:25] = 13 # hair
|
||||
pm[25:40] = 1 # skin
|
||||
pm[40:] = 0 # bg
|
||||
m = head_silhouette(pm)
|
||||
assert m[10, 10] == 255 and m[30, 10] == 255 # hair/skin → 头部
|
||||
assert m[45, 10] == 0 # bg → 非头部
|
||||
|
||||
|
||||
def test_above_curve_region():
|
||||
h = w = 100
|
||||
curve = np.zeros((h, w), np.uint8)
|
||||
curve[60, 20:80] = 255 # 水平发际线在 row=60
|
||||
above = _above_curve_region(curve, h, w)
|
||||
assert above[30, 50] == 255 # 线以上
|
||||
assert above[80, 50] == 0 # 线以下
|
||||
|
||||
|
||||
def test_compose_comfy_rgba_alpha():
|
||||
marked = np.zeros((10, 10, 3), np.uint8)
|
||||
mask = np.zeros((10, 10), np.uint8)
|
||||
mask[2:5, 2:5] = 255 # 重绘区
|
||||
rgba = np.array(compose_comfy_rgba(marked, mask))
|
||||
assert rgba.shape == (10, 10, 4)
|
||||
assert rgba[3, 3, 3] == 0 # 重绘区 alpha=0(透明)
|
||||
assert rgba[8, 8, 3] == 255 # 非重绘区 alpha=255(不透明)
|
||||
Reference in New Issue
Block a user