Files
hair/tests/test_geometry_truth.py
T
xslandClaude Opus 4.8 8d3b145111 feat(worker): 接口1 四庭七眼测量真实实现(替换 Mock)
worker 侧从 Mock 替换为真实算法:
- face_analysis 包:detector(MediaPipe 478点) / pose(solvePnP 姿态) /
  calibration(虹膜直径法) / hair_segmenter+bisenet_model(方案B 头发分割) /
  measure(方案A兜底+B/A决策+七眼+换算) / annotation(numpy渐变线+中文标注)
- app.py:/api/v1/face/measure 接真实实现,返回 annotated_image_base64
  (不落盘不拼URL,落盘由网关做);加 X-Internal-Token 鉴权、/health 就绪态、
  可配置分辨率门槛、异常兜底
- 部署:start.sh/run_worker.sh/hair-worker.service 监听 8187;worker_config 示例
- 测试 tests/:Tier1合成真值<1e-6 + Tier2缩放不变 + Tier3叠加 + 错误码集成 +
  数值回归,pytest 24 项全绿
- 文档补实测基线表 + RTX5090/torch 说明

注:worker 为 RTX 5090(sm_120),pinned torch 2.2.2(cu121) 只到 sm_90,
BiSeNet 已自动回退 CPU(方案B 正常);要用 GPU 需换 torch cu128(≥2.7)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-14 16:07:28 +08:00

46 lines
1.9 KiB
Python

"""Tier 1 — 合成真值,精确验证测量数学(误差仅来自浮点,< 1e-6)。"""
from conftest import build_synthetic_landmarks
from face_analysis.calibration import estimate_scale_factor
from face_analysis.measure import measure_seven_eyes, estimate_vertical_landmarks
def test_scale_factor_exact():
lm, gt = build_synthetic_landmarks(px_per_cm=50.0)
assert abs(estimate_scale_factor(lm, 1000, 1000) - gt["px_per_cm"]) < 1e-6
def test_scale_factor_exact_other_scale():
lm, gt = build_synthetic_landmarks(px_per_cm=73.0)
assert abs(estimate_scale_factor(lm, 1000, 1000) - gt["px_per_cm"]) < 1e-6
def test_seven_eyes_exact():
lm, gt = build_synthetic_landmarks()
r = measure_seven_eyes(lm, 1000, 1000)
pc = gt["px_per_cm"]
assert abs(r["eye_width_px"] / pc - gt["eye_width_cm"]) < 1e-6
assert abs(r["face_width_px"] / pc - gt["face_width_cm"]) < 1e-6
assert abs(r["inter_eye_distance_px"] / pc - gt["inter_eye_cm"]) < 1e-6
def test_measured_courts_exact():
"""中庭、下庭为实测,应与真值分毫不差。"""
lm, gt = build_synthetic_landmarks()
v = estimate_vertical_landmarks(lm, 1000, 1000)
pc = gt["px_per_cm"]
assert abs(v["middle_court_px"] / pc - gt["middle_court_cm"]) < 1e-6
assert abs(v["lower_court_px"] / pc - gt["lower_court_cm"]) < 1e-6
def test_method_a_ratio_formula():
"""方案 A 推算:上/顶庭应严格按既定比例(相对中下庭均值)执行。
注意这只验证「公式按比例正确执行」,不验证贴近真实脸(方案 A 固有局限)。
"""
lm, _ = build_synthetic_landmarks()
v = estimate_vertical_landmarks(lm, 1000, 1000)
one_unit = (v["middle_court_px"] + v["lower_court_px"]) / 2
assert abs(v["upper_court_px"] - one_unit * (0.25 / 0.265)) < 1e-6
assert abs(v["top_court_px"] - one_unit * (0.22 / 0.28)) < 1e-6