检测侧:补扫没救回漏检,改成勾中口算卡时裁剪重读(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:
2026-11-19 21:35:10 +08:00
parent 55e44ec1f5
commit 9f8ef886f0
10 changed files with 462 additions and 15 deletions
+101
View File
@@ -0,0 +1,101 @@
"""补扫(gap scan)的单元测试。
要验的是:切题接口整块漏掉的那一条(竖式区、被折痕挡住的半栏)能被找回来,
而且不能重复、不能越补越多、关掉就真的不花那次钱。
"""
import io
import os
import unittest
from unittest import mock
from PIL import Image, ImageDraw
from backend import scanflow
from ._tmpdir import workdir
def _page(w=400, h=800, band=(500, 700)):
"""白纸上画一条"像文字"的带(短笔画,墨迹约占三成),带外留白。"""
im = Image.new("RGB", (w, h), (255, 255, 255))
dr = ImageDraw.Draw(im)
if band:
y0, y1 = band
for y in range(y0, y1, 20):
for x in range(20, w - 40, 40):
dr.rectangle((x, y, x + 18, y + 7), fill=(30, 30, 30))
buf = io.BytesIO()
im.save(buf, "JPEG", quality=90)
return buf.getvalue()
def _q(stem, box, judge_type="manual", answer=None):
return {"stem": stem, "judge_type": judge_type, "detected_as": judge_type,
"answer": answer, "page_answer": None, "options": None,
"box": box, "subs": []}
class GapScanTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.dir = workdir("gapscan")
def _calls(self, second):
"""第一次调用返回整页结果(只有一块),之后的调用返回补扫结果。"""
first = {"ok": True, "questions": [
_q("1. 计算:36+47=()", {"x": 20, "y": 40, "w": 300, "h": 60}, "calc", "83"),
]}
return mock.Mock(side_effect=[first, second, second])
def test_recovers_missed_band(self):
second = {"ok": True, "questions": [
_q("1.8+0.6=\n6.5-2.9=", {"x": 20, "y": 5, "w": 200, "h": 120}, "manual"),
]}
m = self._calls(second)
with mock.patch.object(scanflow.tencent_ocr, "split_questions", m):
d = scanflow.detect(_page(), "p.jpg")
self.assertEqual(len(d["blocks"]), 2, "漏掉的条带应当被补回来")
added = d["blocks"][1]
self.assertTrue(added.get("from_gap"))
# 坐标要平移回整页:补扫裁的是 y≈476 以下
self.assertGreater(added["box"]["y"], 400)
def test_no_duplicate_when_same_stem(self):
second = {"ok": True, "questions": [
_q("1. 计算:36+47=()", {"x": 20, "y": 5, "w": 200, "h": 60}, "calc", "83"),
]}
m = self._calls(second)
with mock.patch.object(scanflow.tencent_ocr, "split_questions", m):
d = scanflow.detect(_page(), "p.jpg")
self.assertEqual(len(d["blocks"]), 1, "同一道题不能进两次")
self.assertEqual(m.call_count, 2, "仍然会补扫一次,只是结果被去重")
def test_blank_page_no_extra_call(self):
m = self._calls({"ok": True, "questions": []})
with mock.patch.object(scanflow.tencent_ocr, "split_questions", m):
d = scanflow.detect(_page(band=None), "p.jpg")
self.assertEqual(len(d["blocks"]), 1)
self.assertEqual(m.call_count, 1, "页面没有未覆盖的墨迹,不该再调用")
def test_env_switch_off(self):
m = self._calls({"ok": True, "questions": []})
with mock.patch.dict(os.environ, {"OCR_GAP_SCAN": "0"}), \
mock.patch.object(scanflow.tencent_ocr, "split_questions", m):
d = scanflow.detect(_page(), "p.jpg")
self.assertEqual(len(d["blocks"]), 1)
self.assertEqual(m.call_count, 1, "关掉开关就一次都不补")
def test_gap_capped(self):
many = {"ok": True, "questions": [
_q(f"第{i}行算式", {"x": 20, "y": 5 + i * 40, "w": 200, "h": 30}, "calc", "1")
for i in range(10)
]}
m = self._calls(many)
with mock.patch.object(scanflow.tencent_ocr, "split_questions", m):
d = scanflow.detect(_page(band=(200, 760)), "p.jpg")
# GAP_MAX=2 条带 → 最多 2 次补扫 + 1 次整页
self.assertLessEqual(m.call_count, 1 + scanflow.GAP_MAX)
if __name__ == "__main__":
unittest.main()
+29 -6
View File
@@ -186,13 +186,36 @@ class NoWastefulCropTest(unittest.TestCase):
sp.assert_called_once()
self.assertEqual(r["source"], "crop")
def test_card_block_skips_crop(self):
def test_card_block_rechecks_and_takes_more(self):
"""口算卡会被整块读歪(27+69 读成 77+69):勾到它时裁出来重读,题更多就信裁剪版。"""
from backend import scanflow
block = {"i": 0, "box": {"x": 1, "y": 1, "w": 300, "h": 300}, "stem": "10-6= 7+8=",
block = {"i": 0, "box": {"x": 1, "y": 1, "w": 300, "h": 300}, "stem": "34+25= 78-25= 77+69=",
"judge_type": "card", "options": None, "page_answer": "",
"subs": [{"stem": "10-6 =", "answer": "4"}]}
with mock.patch("backend.tencent_ocr.split_questions") as sp:
"subs": [{"stem": "34+25 =", "answer": "59"},
{"stem": "77+69 =", "answer": "146"}]}
with mock.patch("backend.scanflow._crop_bytes", return_value=b"jpg"), \
mock.patch("backend.tencent_ocr.split_questions", return_value={"ok": True, "questions": [
{"stem": "34+25=\n78-25=\n27+69=", "judge_type": "card",
"subs": [{"stem": "34+25 =", "answer": "59"},
{"stem": "78-25 =", "answer": "53"},
{"stem": "27+69 =", "answer": "96"}]}]}):
r = scanflow.recognize_block(Path("data/bench/raw/x.jpg"), block)
sp.assert_not_called()
self.assertEqual(len(r["subs"]), 1)
stems = [s["stem"] for s in r["subs"]]
self.assertEqual(r["source"], "crop_card")
self.assertIn("27+69 =", stems, "裁剪版读对的那道要进来")
self.assertIn("78-25 =", stems, "整页版吞掉的行也要补回来")
self.assertEqual(len(stems), 4, "宁多不漏:读歪的那条也留着,等逐条勾选再收拾")
def test_card_falls_back_when_crop_is_worse(self):
from backend import scanflow
block = {"i": 0, "box": {"x": 1, "y": 1, "w": 300, "h": 300}, "stem": "a",
"judge_type": "card", "options": None, "page_answer": "",
"subs": [{"stem": "1+1 =", "answer": "2"}, {"stem": "2+2 =", "answer": "4"}]}
with mock.patch("backend.scanflow._crop_bytes", return_value=b"jpg"), \
mock.patch("backend.tencent_ocr.split_questions",
return_value={"ok": False, "code": "X"}):
r = scanflow.recognize_block(Path("data/bench/raw/x.jpg"), block)
self.assertEqual(r["source"], "page")
self.assertEqual(len(r["subs"]), 2)