113 lines
4.6 KiB
Python
113 lines
4.6 KiB
Python
"""补扫(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", 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": [
|
|
_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", 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", gap_scan=True)
|
|
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_scan=True)
|
|
# GAP_MAX=2 条带 → 最多 2 次补扫 + 1 次整页
|
|
self.assertLessEqual(m.call_count, 1 + scanflow.GAP_MAX)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|