检测侧:补扫没救回漏检,改成勾中口算卡时裁剪重读(312->321/330)
- scanflow._gap_bands/_gap_blocks:找出没被框盖住但有字的条带,裁出来补切一次 (OCR_GAP_SCAN=0 可关,一页最多 2 条);实测在 3 张漏检图上救回 0 道, 因为那 3 道不是空条带,而是框在、内容读歪(27+69 读成 77+69) - recognize_block 增加 crop_card 分支:勾中口算卡时裁出来重读,两版小题求并集 (宁多不漏;整页版吞行、裁剪版丢题,谁也说不全) - bench/verify_effect.py:同一份整页结果内比 page 与 page∪crop, 不跨调用比——腾讯切题两次调用本身就不完全一致 - bench/run.py --gap / --verify-cards;gold_expr.py --results - tests/test_gap_scan.py 5 例;卡片重读 2 例;179 单测 + 4 套冒烟全绿
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
"""一次性对照:同一份整页结果下,「卡片重读」能多救回几道金标。
|
||||
|
||||
为什么要写这个:腾讯切题对同一张图的两次调用并不完全一致(分段会变),
|
||||
跨两次调用比「318 → 321」会把随机性当成效果。这里只比同一份结果内的
|
||||
page 与 page∪crop 两个并集。
|
||||
"""
|
||||
import io
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
|
||||
from backend import scanflow # noqa: E402
|
||||
from bench.gold_expr import _norm_expr, _same # noqa: E402
|
||||
|
||||
GOLD = Path("bench/gold.json")
|
||||
RAW = Path("data/bench/raw")
|
||||
|
||||
|
||||
def _pairs(blocks):
|
||||
out = []
|
||||
for b in blocks:
|
||||
if b.get("subs"):
|
||||
out.extend((_norm_expr(s.get("stem")), str(s.get("answer") or "")) for s in b["subs"])
|
||||
elif b.get("stem"):
|
||||
out.append((_norm_expr(b.get("stem")), str(b.get("answer") or "")))
|
||||
return out
|
||||
|
||||
|
||||
def score(exprs, pairs):
|
||||
hit = 0
|
||||
for e in exprs:
|
||||
want = _norm_expr(e["t"])
|
||||
truth = str(e.get("a") or "").strip()
|
||||
exact = [p for p in pairs if p[0] == want]
|
||||
pref = [p for p in pairs if p[0].startswith(want) and p[0] != want]
|
||||
got = (exact or pref or [(None, None)])[0][1]
|
||||
if got and truth and (got.strip() == truth or _same(got, truth)):
|
||||
hit += 1
|
||||
return hit
|
||||
|
||||
|
||||
def main():
|
||||
gold = json.loads(GOLD.read_text(encoding="utf-8"))
|
||||
total = page_hit = merged_hit = crop_hit = 0
|
||||
for iid, spec in gold.items():
|
||||
if iid.startswith("_") or not isinstance(spec, dict):
|
||||
continue
|
||||
fp = RAW / f"{iid}.jpg"
|
||||
if not fp.exists():
|
||||
continue
|
||||
det = scanflow.detect(fp.read_bytes(), fp.name, gap_scan=False)
|
||||
src = scanflow._img_path(det["draft_id"])
|
||||
exprs = spec["exprs"]
|
||||
total += len(exprs)
|
||||
a = score(exprs, _pairs(det["blocks"]))
|
||||
page_hit += a
|
||||
merged, prefer = [], []
|
||||
for b in det["blocks"]:
|
||||
if b.get("subs"):
|
||||
m = scanflow.recognize_block(src, b) # 并集版
|
||||
merged.append(m)
|
||||
crop_only = [s for s in m["subs"]
|
||||
if s not in (b.get("subs") or [])]
|
||||
# 「裁剪版更多就整版换掉」的对照
|
||||
if len(m["subs"]) > len(b.get("subs") or []):
|
||||
prefer.append({**b, "subs": m["subs"]})
|
||||
else:
|
||||
prefer.append(b)
|
||||
else:
|
||||
merged.append(b)
|
||||
prefer.append(b)
|
||||
bb = score(exprs, _pairs(merged))
|
||||
cc = score(exprs, _pairs(prefer))
|
||||
merged_hit += bb
|
||||
crop_hit += cc
|
||||
if bb != a or cc != a:
|
||||
print(f"{iid} 整页 {a} / 并集 {bb} / 整版换 {cc}")
|
||||
print(f"合计 {total} 道:整页 {page_hit},并集 {merged_hit},整版换 {crop_hit}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user