From ef201ffacce8a136f00f9c74b5f23542afaab0bd Mon Sep 17 00:00:00 2001 From: 418824852 <418824852@qq.com> Date: Sun, 22 Nov 2026 15:30:47 +0800 Subject: [PATCH] =?UTF-8?q?B-4=20=E9=80=90=E6=9D=A1=E5=8B=BE=E9=80=89:?= =?UTF-8?q?=E5=85=A5=E5=BA=93=E5=89=8D=E6=8A=8A=E4=B8=80=E5=9D=97=E9=87=8C?= =?UTF-8?q?=E7=9A=84=E9=A2=98=E6=91=8A=E5=BC=80,=E5=8F=AA=E6=94=B6?= =?UTF-8?q?=E5=8B=BE=E4=B8=8A=E7=9A=84(prepare=20=E6=8E=A5=E5=8F=A3=20+=20?= =?UTF-8?q?=E5=BC=B9=E7=AA=97,239=20=E6=B5=8B=E8=AF=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/README.md | 19 +++ backend/app.py | 11 ++ backend/services.py | 90 ++++++++++++-- ...b2f53091b018c12143673b069c4213e9642ce.json | 1 + ...e6af2e599e537c6723e5a5684348c560ad0ee.json | 1 + ...ea7af39ad5fefb6bfbc82a1c5023494bdade2.json | 1 + data/ocr_usage.json | 2 +- prototype/workbench/README.md | 17 +++ prototype/workbench/index.html | 117 +++++++++++++++++- report/预览-逐条勾选.png | Bin 0 -> 143086 bytes tests/smoke_login.py | 3 +- tests/smoke_manual.py | 3 +- tests/test_scan_flow.py | 55 ++++++++ 13 files changed, 305 insertions(+), 15 deletions(-) create mode 100644 data/_ocrcache_test/4efb2f53091b018c12143673b069c4213e9642ce.json create mode 100644 data/_ocrcache_test/715e6af2e599e537c6723e5a5684348c560ad0ee.json create mode 100644 data/_ocrcache_test/978ea7af39ad5fefb6bfbc82a1c5023494bdade2.json create mode 100644 report/预览-逐条勾选.png diff --git a/backend/README.md b/backend/README.md index 721d625..fa9b0b7 100644 --- a/backend/README.md +++ b/backend/README.md @@ -592,3 +592,22 @@ python tools/backfill_kp.py --apply # 真写 `tests/test_kp_match.py` 19 例:规则自检、小数/分数/单位/拼音/量词/近义词/ 标点/成语各命中一条、空题干与无关题干返回 `None`。 + +### 四点二十六、入库前逐条勾选:`/api/papers/scan/prepare` + +`POST /api/papers/scan/prepare`(`{draft_id, picks, child_id, subject}`)只读不写: +把勾中的块各 `recognize_block` 一遍,返回 `items: [{block, sub, stem, answer, …}]`。 +`sub` 是 `None` 表示这块本来就一道题,数字表示它是这块里的第几道(口算卡下标)。 + +`import_scan_picks` 的 `picks` 因此支持两种写法:`3`(整块)与 +`{"i": 3, "subs": [0, 2]}`(只要这块的第 0、2 道)。三种边界都在测试里: + +- `subs: []` —— 这块里的小题全取消,等于不入库,不能退化成「整块都要」; +- `subs` 下标越界 —— 跳过,不猜; +- 预览是空的 —— 400 `NO_PICKS`。 + +为什么要有它:切题把 `27+69` 读成 `77+69` 这种错,机器判断不了对错,但家长一眼 +就能看出来。与其让家长事后去错题本里删,不如入库前摊开给他看。 + +调用成本:预览与正式入库调的是同一个 `recognize_block`,裁剪图按 sha1 命中 +`data/ocr_cache/`,所以预览这一遍**不额外花钱**(除非把 `OCR_CACHE=0` 关了)。 diff --git a/backend/app.py b/backend/app.py index 3f33d5f..24e0ade 100644 --- a/backend/app.py +++ b/backend/app.py @@ -282,6 +282,17 @@ def post_scan_detect(ctx: Ctx, **kw): return scanflow.detect(image, str(ctx.body.get("filename") or "page.jpg")) +@route("POST", "/api/papers/scan/prepare", auth="parent") +def post_scan_prepare(ctx: Ctx, **kw): + """入库前预览:勾中的块里到底有哪几道(口算卡一块十几道,要逐条勾)。""" + picks = ctx.body.get("picks") or [] + if not isinstance(picks, list): + raise svc.ApiError(400, "BAD_PICKS", "picks 要是数组") + return svc.prepare_scan_picks( + ctx.conn, ctx.gid, str(ctx.body.get("child_id") or ""), + str(ctx.body.get("subject") or "math"), str(ctx.body.get("draft_id") or ""), picks) + + @route("POST", "/api/papers/scan/import", auth="parent") def post_scan_import(ctx: Ctx, **kw): """家长勾完 → 只把选中的块裁出来单独识别并入库。""" diff --git a/backend/services.py b/backend/services.py index af81c60..46228d2 100644 --- a/backend/services.py +++ b/backend/services.py @@ -1716,12 +1716,65 @@ def dev_scan_confirm(conn, code: str) -> dict: return out +def prepare_scan_picks(conn, gid: str, cid: str, subject: str, draft_id: str, + picks: list) -> dict: + """入库前的「逐条勾选」预览:把勾中的块认一遍,列出里面的每一道小题。 + + 为什么要单独一步:口算卡一块十几道,切题偶尔会把相邻的题读进来、甚至读歪 + (`27+69` 读成 `77+69`)。整块入库会把孩子没做错的题也塞进错题本,家长得 + 事后一条条删。所以入库前把这几道摊开给家长看,取消掉没做错的。 + + 这一步只读不写。它调的 `recognize_block` 与真正入库时同一套(裁剪图按 + sha1 命中 OCR 缓存),所以预览过一遍之后,正式入库**不会再花一次调用**。 + """ + from . import scanflow + + require_active(conn, gid) + get_child(conn, gid, cid) + src, meta = scanflow.load_draft(draft_id) + blocks = meta.get("blocks") or [] + items = [] + for p in picks: + try: + i = int(p if not isinstance(p, dict) else p.get("i")) + except (TypeError, ValueError): + continue + if not (0 <= i < len(blocks)): + continue + r = scanflow.recognize_block(src, blocks[i]) + subs = r.get("subs") or [] + if subs: + for k, x in enumerate(subs): + items.append({"block": i, "sub": k, "stem": x.get("stem") or "", + "judge_type": "calc", "answer": x.get("answer"), + "options": None, "source": r.get("source")}) + else: + items.append({"block": i, "sub": None, "stem": r.get("stem") or "", + "judge_type": r.get("judge_type"), "answer": r.get("answer"), + "options": r.get("options"), "source": r.get("source")}) + if not items: + raise ApiError(400, "NO_PICKS", "一道都没选,先点上哪几道错了") + if len(items) > 60: + items = items[:60] + return {"draft_id": draft_id, "items": items, + "hint": "这些是勾中的块里认出来的题。取消掉没做错的那几道,再入库。"} + + def import_scan_picks(conn, gid: str, cid: str, subject: str, draft_id: str, - picks: list[int]) -> dict: + picks: list) -> dict: """把「家长勾中的那几块」入库。 流程是:整页拍 → 切块给家长点 → 选中的块从原图裁出来**再单独识别一次**。 单块图字更大、没有相邻题干扰,题干比整页切时干净得多。 + + picks 每一项可以是: + + - `3`(块序号):整块都要,块里若是口算卡就展开成全部小问; + - `{"i": 3, "subs": [0, 2]}`:只要这块里的第 0、2 个小问——家长在 + 「这块里有 8 道,取消你没做错的」那一步勾出来的结果。 + + 为什么要第二种:口算卡一块十几道,切题偶尔会把相邻的题读进来(甚至读歪), + 整块入库会把没做错的题也塞进错题本。逐条勾选把最后这个决定权还给家长。 """ from . import scanflow @@ -1734,13 +1787,27 @@ def import_scan_picks(conn, gid: str, cid: str, subject: str, draft_id: str, src, meta = scanflow.load_draft(draft_id) blocks = meta.get("blocks") or [] chosen = [] - for i in picks: + for p in picks: + subs_only = None + if isinstance(p, dict): + raw_i, raw_subs = p.get("i"), p.get("subs") + if isinstance(raw_subs, list): + sub_idx = set() + for s in raw_subs: + try: + sub_idx.add(int(s)) + except (TypeError, ValueError): + continue + subs_only = sub_idx + p = raw_i try: - i = int(i) + i = int(p) except (TypeError, ValueError): continue if 0 <= i < len(blocks): - chosen.append(blocks[i]) + if subs_only is not None and not subs_only: + continue # 这块里的小题全被取消 = 这块不入库 + chosen.append((blocks[i], subs_only)) if not chosen: raise ApiError(400, "NO_PICKS", "一道都没选,先点上哪几道错了") if len(chosen) > 20: @@ -1748,15 +1815,20 @@ def import_scan_picks(conn, gid: str, cid: str, subject: str, draft_id: str, # 口算卡(一块里十几道)先展开成多道,再往库里写 questions = [] - for b in chosen: + for b, subs_only in chosen: r = scanflow.recognize_block(src, b) subs = r.get("subs") or [] if subs: - questions.extend({"stem": x.get("stem") or "", "judge_type": "calc", - "options": None, "answer": x.get("answer"), - "page_answer": None, "source": r.get("source")} for x in subs) - else: + keep = subs_only if subs_only is not None else range(len(subs)) + for k in keep: + if 0 <= k < len(subs): + x = subs[k] + questions.append({"stem": x.get("stem") or "", "judge_type": "calc", + "options": None, "answer": x.get("answer"), + "page_answer": None, "source": r.get("source")}) + elif subs_only is None: questions.append(r) + # subs_only 给了但这块其实只有一道题:说明前端的勾选过期了,跳过不猜 # 切题不给知识点;这里按题干补一次,补不上就留空(学情分析宁可少,不可错) for q in questions: diff --git a/data/_ocrcache_test/4efb2f53091b018c12143673b069c4213e9642ce.json b/data/_ocrcache_test/4efb2f53091b018c12143673b069c4213e9642ce.json new file mode 100644 index 0000000..69a3dd5 --- /dev/null +++ b/data/_ocrcache_test/4efb2f53091b018c12143673b069c4213e9642ce.json @@ -0,0 +1 @@ +{"ok": true, "data": {}} \ No newline at end of file diff --git a/data/_ocrcache_test/715e6af2e599e537c6723e5a5684348c560ad0ee.json b/data/_ocrcache_test/715e6af2e599e537c6723e5a5684348c560ad0ee.json new file mode 100644 index 0000000..8e6d223 --- /dev/null +++ b/data/_ocrcache_test/715e6af2e599e537c6723e5a5684348c560ad0ee.json @@ -0,0 +1 @@ +{"ok": true, "data": {"QuestionInfo": [{"Angle": 0, "Height": 300, "ImageBase64": "", "OrgHeight": 300, "OrgWidth": 900, "ResultList": null, "Width": 900}], "RequestId": "a359c898-8b7e-43b6-b214-495e94678f17"}, "request_id": "a359c898-8b7e-43b6-b214-495e94678f17"} \ No newline at end of file diff --git a/data/_ocrcache_test/978ea7af39ad5fefb6bfbc82a1c5023494bdade2.json b/data/_ocrcache_test/978ea7af39ad5fefb6bfbc82a1c5023494bdade2.json new file mode 100644 index 0000000..5520a8d --- /dev/null +++ b/data/_ocrcache_test/978ea7af39ad5fefb6bfbc82a1c5023494bdade2.json @@ -0,0 +1 @@ +{"ok": true, "request_id": "r1", "data": {"QuestionInfo": [{"ResultList": [{"Question": [{"Text": "2.计算:72 - 38 ="}], "Answer": [{"Text": "34"}]}]}], "RequestId": "req-1"}} \ No newline at end of file diff --git a/data/ocr_usage.json b/data/ocr_usage.json index c445232..974752e 100644 --- a/data/ocr_usage.json +++ b/data/ocr_usage.json @@ -1 +1 @@ -{"2026-11": 101} \ No newline at end of file +{"2026-11": 113} \ No newline at end of file diff --git a/prototype/workbench/README.md b/prototype/workbench/README.md index 62775a7..d3df6ab 100644 --- a/prototype/workbench/README.md +++ b/prototype/workbench/README.md @@ -231,3 +231,20 @@ node tools/verify_plan.mjs # → data/plan_verify.md 数字对、话是废话。改成按小数位数缩放后取最低位。 4. **没有闸门**:新增 `planClaims()` / `planConsistent()`,生成完回头重算, 算不通或答案没出现就 `return null`,由上层退回通用三步。 + +## 十三、这块里有几道:入库前逐条勾选 + +口算卡一块十几道。切题偶尔会把孩子没做错的题切进来,或者把 `27+69` 读成 +`77+69`。上一版的做法是「宁多不漏」——整块入库,让家长事后一条条删。这一版把 +最后这个决定权还给家长: + +1. 在图上点错题(原有流程,不变); +2. 点「入库选中的 N 道」后,只要勾中的块里有**一块多道**,就先弹一张单子, + 把这块里认出来的每一道摊开,默认全勾,家长取消掉没做错的; +3. 确认后只入库勾上的那几道:单题的块传块序号,多题的块传 `{i, subs:[…]}`。 + +单子里的序号(第 1 道、第 2 道…)与 `subs` 数组下标一一对应,取消的会划掉, +计数按钮同步变。预览这一步只读不写,`recognize_block` 的裁剪图按 sha1 命中 +OCR 缓存,所以预览过一遍之后,正式入库不会再花一次调用。 + +只有一块一道题的时候不会弹这张单子——多一次点击换不来任何信息。 diff --git a/prototype/workbench/index.html b/prototype/workbench/index.html index 911ee55..acbb347 100644 --- a/prototype/workbench/index.html +++ b/prototype/workbench/index.html @@ -438,6 +438,15 @@ td .t{font-family:"Consolas",monospace;font-size:13.5px} .scan-box i { position: absolute; top: -9px; left: -9px; width: 20px; height: 20px; border-radius: 50%; background: #fff; border: 1.5px solid rgba(228,99,74,.55); color: #B4482F; font-style: normal; font-size: 11px; line-height: 17px; text-align: center; font-weight: 700; } .scan-box.on i { background: var(--brand); border-color: var(--brand-d); color: #fff; } .scan-legend { margin-top: 12px; font-size: 12px; color: var(--muted); } +/* 口算卡逐条勾选 */ +.pklist { max-height: 46vh; overflow: auto; margin: 6px 0 14px; border: 1px solid var(--line); border-radius: 12px; } +.pkrow { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid #F6EFE4; cursor: pointer; } +.pkrow:last-child { border-bottom: 0; } +.pkrow:hover { background: #FDF8EF; } +.pkrow input { width: 17px; height: 17px; accent-color: var(--brand); flex: none; } +.pkrow .pks { font-size: 14px; } +.pkrow .pkn { font-size: 11.5px; color: var(--muted); margin-left: auto; flex: none; } +.pkrow.off .pks { color: var(--muted); text-decoration: line-through; } /* ---- 讲一遍 ---- */ .exdlg { position: relative; width: 560px; max-width: 92vw; max-height: 88vh; overflow: auto; @@ -967,6 +976,18 @@ td .t{font-family:"Consolas",monospace;font-size:13.5px}
+口算卡一块十几道,切题有时会把没做错的、读歪的一起切进来。取消掉没做错的那几道再入库。
+ +