feat(tiling): 自适应瓦片规划器 + VLM 分辨率离线标定

背景:逐图框审查固定「整体 + 2x2 象限」截图,大图幅(A0/A1)读不清细节。
改为按**实体分布**自适应决定截几张、截哪里。

- 新增 dwg_review/tiling.py(纯函数):
  SHEET_SIZES_MM / frame_world_size / frame_scale(sheet_scale>0 用之,否则由
  bbox/纸张反算,否则 default)/ effective_px(与 downsample 同规则)/
  world_per_px / is_text_type(标准名 + 自定义类名含 TEXT/LABEL/DIM/NOTE/TAG/
  ANNO/TITLE)/ cluster_text_flags / tiles_for_box / subdivide / plan_regions
  (文字簇优先、空白区不出图、max_tiles 截断、窗口夹到图框范围)
- 新增 verify_vlm_resolution_offline.py:PIL 合成类图纸图(精确像素字高 +
  可控线间距/线宽/虚线)→ 问 VLM 强约束 JSON → 定 R_text / R_line。
  实测:R_text=12px(黑底;白底 10px);R_line=6px。
  另:实测该模型**无法可靠计数**(6 条线在 2~10px 间距下答 4/10/4/5/4/4),
  故线条判据改用「1 条 vs 2 条」二选一。
- config 新增 TILING_ENABLED / TEXT_HEIGHT_MM / TEXT_READABLE_PX=12 /
  LINE_READABLE_PX=6 / TILE_MAX_COUNT / TILE_OVERLAP / HEATMAP_CELL_DIV /
  HEATMAP_CLUSTER_DILATE / HEATMAP_MAX_AREA_FRAC;EVIDENCE_DETAIL_TILES 与
  EVIDENCE_MAX_IMAGES 保留作回退路径参数
- llm_client 新增 image_size_b64(测量绘图区实际像素,决定单张覆盖范围)
- phases/evidence.capture_images 支持瓦片规划:先截整体图并测像素 →
  plan_regions 成功用规划窗口,否则退回 sub_windows(行为零变化);
  末尾始终复位整体视口。collect_frame 传实体/文字 bbox;任一信封
  total>returned(截断)→ 退回固定策略,避免误删有内容的瓦片
- 测试:tests/test_tiling.py(28 项)、tests/test_evidence.py 新增规划路径与
  降级用例、test_config / test_llm_client 补新常量与 image_size_b64;
  全量 283 项全绿
This commit is contained in:
gjm committed 2026-10-15 23:03:27 +08:00
1 parent 9b8a2329df
commit cba05b0c65
9 files changed
+1246 -10

No files matched your search

+151 -1
View File
@@ -1,8 +1,17 @@
"""`dwg_review.phases.evidence` 单元测试。"""
import base64
import io
import pytest
from dwg_review.phases.evidence import bbox_to_window, collect_frame
from dwg_review.phases.evidence import (
_is_truncated,
_plan_tiles,
_text_boxes,
bbox_to_window,
collect_frame,
)
from tests.fakes import FakeMcpClient, frame, make_ctx, ok_image, ok_text
@@ -141,3 +150,144 @@ def test_collect_returns_none_for_unsupported_source():
ctx = make_ctx(client=FakeMcpClient())
assert collect(ctx, 1, {"source_type": "pdf", "bbox": [0, 0, 1, 1]}) is None
# ---------------------------------------------------------------------------
# 自适应瓦片规划
# ---------------------------------------------------------------------------
@pytest.fixture
def tiling_env(monkeypatch):
"""把瓦片相关配置钉死,使用例不受外部环境变量影响。"""
from dwg_review import config
for name, value in {
"TILING_ENABLED": True,
"TEXT_HEIGHT_MM": 2.0,
"TEXT_READABLE_PX": 12,
"LINE_READABLE_PX": 6,
"TILE_MAX_COUNT": 36,
"TILE_OVERLAP": 0.0,
"HEATMAP_CELL_DIV": 32,
"HEATMAP_CLUSTER_DILATE": 0,
"HEATMAP_MAX_AREA_FRAC": 0.5,
"IMAGE_MAX_EDGE": 1280,
"EVIDENCE_MAX_IMAGES": 5,
"EVIDENCE_DETAIL_TILES": 4,
}.items():
monkeypatch.setattr(config, name, value)
return config
def _png_b64(w=2000, h=1414):
from PIL import Image
buf = io.BytesIO()
Image.new("RGB", (w, h), "black").save(buf, format="PNG")
return base64.b64encode(buf.getvalue()).decode("ascii")
def _tiling_client(b64, entity_type="LINE"):
return FakeMcpClient(
responses={
"query_entities": ok_text(
{"total": 1, "items": [{"handle": "A", "type": entity_type,
"bbox": [0, 0, 60000, 20000]}]}
),
"get_text_content": ok_text({"total": 0, "items": []}),
"get_dimensions": ok_text({"total": 0, "items": []}),
"get_block_references": ok_text({"total": 0, "items": []}),
"get_polylines_info": ok_text({"total": 0, "items": []}),
"get_viewport_screenshot": ok_image(b64),
}
)
def test_is_truncated():
assert _is_truncated({"total": 5, "items": [1]}) is True
assert _is_truncated({"total": 1, "items": [1]}) is False
assert _is_truncated({"items": [1, 2]}) is False
assert _is_truncated(None, {}, {"total": 3, "items": []}) is True
assert _is_truncated() is False
def test_text_boxes_includes_std_and_custom():
entities = {"items": [
{"type": "LINE", "bbox": [0, 0, 1, 1]},
{"type": "AECDLABEL", "bbox": [2, 2, 3, 3]},
{"type": "TEXT", "bbox": [4, 4, 5, 5]},
]}
texts = {"items": [{"bbox": [6, 6, 7, 7]}]}
dimensions = {"items": [{"bbox": [8, 8, 9, 9]}]}
boxes = _text_boxes(entities, texts, dimensions)
for expected in ((2, 2, 3, 3), (4, 4, 5, 5), (6, 6, 7, 7), (8, 8, 9, 9)):
assert tuple(float(v) for v in expected) in boxes
assert (0.0, 0.0, 1.0, 1.0) not in boxes # 普通线不算文字
def test_plan_tiles_degrades(tiling_env):
win = {"min_x": 0.0, "min_y": 0.0, "max_x": 100.0, "max_y": 50.0}
f = frame([0, 0, 100, 50], sheet_size="A0", sheet_scale=100.0)
boxes = [(0.0, 0.0, 10.0, 10.0)]
assert _plan_tiles(win, f, boxes, [], False, (100, 50)) != [] # 正常路径非空
assert _plan_tiles(win, None, boxes, [], False, (100, 50)) == [] # 缺图框
assert _plan_tiles(win, f, [], [], False, (100, 50)) == [] # 无实体
assert _plan_tiles(win, f, boxes, [], False, None) == [] # 无像素
assert _plan_tiles(win, f, boxes, [], True, (100, 50)) == [] # 数据截断
# 比例未知(sheet_scale=0 且 sheet_size 不在纸张表)→ 无法规划
unknown = frame([0, 0, 100, 50], sheet_size="ZZ", sheet_scale=0.0)
assert _plan_tiles(win, unknown, boxes, [], False, (100, 50)) == []
def test_plan_tiles_disabled_by_config(tiling_env, monkeypatch):
tiling_env.TILING_ENABLED = False
win = {"min_x": 0.0, "min_y": 0.0, "max_x": 100.0, "max_y": 50.0}
f = frame([0, 0, 100, 50], sheet_size="A0", sheet_scale=100.0)
assert _plan_tiles(win, f, [(0.0, 0.0, 10.0, 10.0)], [], False, (100, 50)) == []
def test_capture_images_tiles_line_cluster(tiling_env):
"""线条簇:单张可覆盖 → 2 张瓦片(行优先)。"""
pytest.importorskip("PIL")
ctx = make_ctx(client=_tiling_client(_png_b64()))
ev = collect_frame(
ctx, 1, frame([0, 0, 120000, 40000], sheet_size="A0", sheet_scale=100.0)
)
assert [i["label"] for i in ev["images"]] == ["overall", "tile-1", "tile-2"]
def test_capture_images_tiles_text_cluster_is_finer(tiling_env):
"""文字簇用更细尺度 → 瓦片更多,并受 EVIDENCE_MAX_IMAGES 截断。"""
pytest.importorskip("PIL")
ctx = make_ctx(client=_tiling_client(_png_b64(), entity_type="AECDLABEL"))
ev = collect_frame(
ctx, 1, frame([0, 0, 120000, 40000], sheet_size="A0", sheet_scale=100.0)
)
labels = [i["label"] for i in ev["images"]]
assert labels == ["overall", "tile-1", "tile-2", "tile-3", "tile-4"] # 上限 5 张
def test_capture_images_falls_back_without_scale(tiling_env):
"""缺 sheet_size/sheet_scale → 退回固定 2x2 象限。"""
pytest.importorskip("PIL")
ctx = make_ctx(client=_tiling_client(_png_b64()))
ev = collect_frame(ctx, 1, frame([0, 0, 120000, 40000], sheet_size="ZZ"))
assert [i["label"] for i in ev["images"]] == [
"overall", "tile-1", "tile-2", "tile-3", "tile-4",
]
def test_capture_images_falls_back_when_truncated(tiling_env):
"""实体被截断 → 禁用内容过滤,退回固定策略。"""
pytest.importorskip("PIL")
client = _tiling_client(_png_b64())
client.responses["query_entities"] = ok_text(
{"total": 500, "items": [{"handle": "A", "type": "LINE",
"bbox": [0, 0, 60000, 20000]}]}
)
ctx = make_ctx(client=client)
ev = collect_frame(
ctx, 1, frame([0, 0, 120000, 40000], sheet_size="A0", sheet_scale=100.0)
)
assert [i["label"] for i in ev["images"]] == [
"overall", "tile-1", "tile-2", "tile-3", "tile-4",
]