"""接口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(不透明)