补扫:默认关(要开用 gap_scan=True),修坐标没除 1.4 倍导致的越界框(240 测试)

This commit is contained in:
dsh
2026-11-22 21:23:53 +08:00
parent bde4170423
commit 8a93058855
4 changed files with 45 additions and 11 deletions
+8 -1
View File
@@ -484,7 +484,7 @@ python tests/smoke_http.py # 39 项 HTTP 冒烟(真起服务
金标 330 道时还剩 12 道没认出来,一开始以为是「整块没被切出来」,做了补扫才发现**不是**。
**补扫**(`scanflow._gap_bands / _gap_blocks`,`OCR_GAP_SCAN=0` 可关)
**补扫**(`scanflow._gap_bands / _gap_blocks`,默认关:`detect(gap_scan=True)` 才做,`OCR_GAP_SCAN=0` 强制关)
- 找出「没有框盖住、但确实有字」的横向条带:灰度二值化后按行求墨迹比例
(`bw.resize((1,sh), BOX)` 一次拿到每行均值),先做行距桥接再找连续段,
高度 ≥90px、墨迹在 0.4%–55% 之间才算,一页最多补 2 条
@@ -492,6 +492,13 @@ python tests/smoke_http.py # 39 项 HTTP 冒烟(真起服务
- **实测在 3 张漏检图上救回 0 道**:那 3 道根本不在空条带里,而是
「框在、框里的内容读歪了」——`27+69` 被读成 `77+69`,`4/5-2/5` 被整行吞掉。
补扫留着(真有空条带时它有用),但它不是这次的解药。
- **默认关**:每页多 1–2 次调用,而金标口径上救回 0 道;线上要开就
`scanflow.detect(image, name, gap_scan=True)`。它在「看图写数、比大小」这种
整块没有文字框的区域确实有用(一张卷上能多出 3 块可点区域)。
- **踩过一个坐标 bug**:补扫条带是**放大 1.4 倍**再送去识别的,早先 `_gap_blocks`
只做 `y + y0` 平移、没把框除以 1.4,于是小程序里出现 `y=1448 / 页高 1260` 的框,
点在页面外面。现在用 `GAP_SCALE = 1.4`,裁切和还原用同一个常量,并把完全落在
页外的框丢掉。凡是「裁一块出去识别、再把坐标搬回来」的代码都要成对检查缩放。
**卡片重读**(`scanflow.recognize_block` 里 `crop_card` 分支)
- 口算卡最容易整块读歪。家长**勾到这一块**时,把它裁出来重读一次(1 次调用),
+21 -5
View File
@@ -35,6 +35,8 @@ GAP_MAX = 2 # 一页最多补扫两条,再多就是在赌
GAP_INK = 0.004 # 条带里墨迹像素占比下限(低于它基本是空白)
GAP_MAX_INK = 0.55 # 高于它更像照片/阴影,不是文字
GAP_PAD = 24
# 补扫的条带会放大再送 OCR,返回的框是放大后的坐标,要按这个数除回去
GAP_SCALE = 1.4
def _pil():
@@ -70,8 +72,15 @@ def save_draft(image_bytes: bytes, filename: str = "page.jpg") -> dict:
def _gap_enabled() -> bool:
return os.environ.get("OCR_GAP_SCAN", "1").strip().lower() not in (
"0", "false", "no", "off")
"""补扫要不要做:默认由 detect(gap_scan=) 决定,环境变量只用来「强制关」。
补扫在真实卷子上救回过整块(看图写数、比大小),但每页多 1–2 次调用,
所以默认关闭;要开就 detect(..., gap_scan=True),线上想临时全关就设 OCR_GAP_SCAN=0。
"""
v = os.environ.get("OCR_GAP_SCAN")
if v is None:
return True
return v.strip().lower() not in ("0", "false", "no", "off")
def _ink_rows(src: Path) -> list[float]:
@@ -147,7 +156,7 @@ def _crop_band(src: Path, y0: int, y1: int) -> bytes | None:
with Image.open(src) as im:
im = im.convert("RGB")
crop = im.crop((0, y0, im.width, y1))
crop = crop.resize((int(crop.width * 1.4), int(crop.height * 1.4)),
crop = crop.resize((int(crop.width * GAP_SCALE), int(crop.height * GAP_SCALE)),
Image.LANCZOS)
crop = ImageOps.autocontrast(crop, cutoff=1)
buf = io.BytesIO()
@@ -193,7 +202,14 @@ def _gap_blocks(src: Path, blocks: list[dict], w: int, h: int) -> list[dict]:
continue
if box["w"] < MIN_BOX_SIDE or box["h"] < MIN_BOX_SIDE:
continue
moved = {"x": box["x"], "y": box["y"] + y0, "w": box["w"], "h": box["h"]}
# 补扫图是放大 1.4 倍再送的,坐标要除回去;再整体下移到条带在原图里的位置
bx = int(round(box["x"] / GAP_SCALE))
by = int(round(box["y"] / GAP_SCALE)) + y0
bw = int(round(box["w"] / GAP_SCALE))
bh = int(round(box["h"] / GAP_SCALE))
if by < 0 or by >= h or bx >= w:
continue
moved = {"x": bx, "y": by, "w": bw, "h": bh}
if _center_in(moved, blocks) or _center_in(moved, added):
continue
seen.add(key)
@@ -210,7 +226,7 @@ def _gap_blocks(src: Path, blocks: list[dict], w: int, h: int) -> list[dict]:
def detect(image_bytes: bytes, filename: str = "page.jpg",
gap_scan: bool = True) -> dict:
gap_scan: bool = False) -> dict:
"""整页切块。返回题块 + 草稿 id;切不出题就抛 422 让前端提示重拍。"""
d = save_draft(image_bytes, filename)
res = tencent_ocr.split_questions(image_bytes)
+1 -1
View File
@@ -1 +1 @@
{"2026-11": 137}
{"2026-11": 197}
+15 -4
View File
@@ -53,12 +53,16 @@ class GapScanTest(unittest.TestCase):
]}
m = self._calls(second)
with mock.patch.object(scanflow.tencent_ocr, "split_questions", m):
d = scanflow.detect(_page(), "p.jpg")
d = scanflow.detect(_page(), "p.jpg", gap_scan=True)
self.assertEqual(len(d["blocks"]), 2, "漏掉的条带应当被补回来")
added = d["blocks"][1]
self.assertTrue(added.get("from_gap"))
# 坐标要平移回整页:补扫裁的是 y≈476 以下
self.assertGreater(added["box"]["y"], 400)
# 补扫图放大过 1.4 倍,框要除回去,并且不能跑出页面
box = added["box"]
self.assertLessEqual(box["y"] + box["h"], 800)
self.assertLessEqual(box["x"] + box["w"], 400)
def test_no_duplicate_when_same_stem(self):
second = {"ok": True, "questions": [
@@ -66,14 +70,21 @@ class GapScanTest(unittest.TestCase):
]}
m = self._calls(second)
with mock.patch.object(scanflow.tencent_ocr, "split_questions", m):
d = scanflow.detect(_page(), "p.jpg")
d = scanflow.detect(_page(), "p.jpg", gap_scan=True)
self.assertEqual(len(d["blocks"]), 1, "同一道题不能进两次")
self.assertEqual(m.call_count, 2, "仍然会补扫一次,只是结果被去重")
def test_default_is_off(self):
"""默认不补扫:这是钱,上一次证明过救不回来。"""
m = self._calls({"ok": True, "questions": []})
with mock.patch.object(scanflow.tencent_ocr, "split_questions", m):
scanflow.detect(_page(), "p.jpg")
self.assertEqual(m.call_count, 1, "默认不该补扫;要用就显式开")
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")
d = scanflow.detect(_page(band=None), "p.jpg", gap_scan=True)
self.assertEqual(len(d["blocks"]), 1)
self.assertEqual(m.call_count, 1, "页面没有未覆盖的墨迹,不该再调用")
@@ -92,7 +103,7 @@ class GapScanTest(unittest.TestCase):
]}
m = self._calls(many)
with mock.patch.object(scanflow.tencent_ocr, "split_questions", m):
d = scanflow.detect(_page(band=(200, 760)), "p.jpg")
d = scanflow.detect(_page(band=(200, 760)), "p.jpg", gap_scan=True)
# GAP_MAX=2 条带 → 最多 2 次补扫 + 1 次整页
self.assertLessEqual(m.call_count, 1 + scanflow.GAP_MAX)