feat: 整页拍 + 点选出错题(scanflow + 工作台界面)
This commit is contained in:
@@ -1684,3 +1684,69 @@ def dev_scan_confirm(conn, code: str) -> dict:
|
||||
out = scan_confirm(conn, row["id"], code)
|
||||
out["demo"] = True
|
||||
return out
|
||||
|
||||
|
||||
def import_scan_picks(conn, gid: str, cid: str, subject: str, draft_id: str,
|
||||
picks: list[int]) -> dict:
|
||||
"""把「家长勾中的那几块」入库。
|
||||
|
||||
流程是:整页拍 → 切块给家长点 → 选中的块从原图裁出来**再单独识别一次**。
|
||||
单块图字更大、没有相邻题干扰,题干比整页切时干净得多。
|
||||
"""
|
||||
from . import scanflow
|
||||
|
||||
require_active(conn, gid)
|
||||
child = get_child(conn, gid, cid)
|
||||
if subject not in ("math", "chinese"):
|
||||
raise ApiError(400, "BAD_SUBJECT", "科目只有 math / chinese")
|
||||
if subject not in child["subjects"]:
|
||||
raise ApiError(400, "SUBJECT_OFF", "这个科目还没开")
|
||||
src, meta = scanflow.load_draft(draft_id)
|
||||
blocks = meta.get("blocks") or []
|
||||
chosen = []
|
||||
for i in picks:
|
||||
try:
|
||||
i = int(i)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
if 0 <= i < len(blocks):
|
||||
chosen.append(blocks[i])
|
||||
if not chosen:
|
||||
raise ApiError(400, "NO_PICKS", "一道都没选,先点上哪几道错了")
|
||||
if len(chosen) > 20:
|
||||
raise ApiError(400, "TOO_MANY", "一次最多入库 20 道")
|
||||
|
||||
questions = [scanflow.recognize_block(src, b) for b in chosen]
|
||||
|
||||
pid = new_id("p")
|
||||
folder = UPLOAD_DIR / cid
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
img_path = folder / f"{pid}.jpg"
|
||||
img_path.write_bytes(src.read_bytes())
|
||||
|
||||
with tx(conn):
|
||||
conn.execute("INSERT INTO papers (id, child_id, subject, image_count, clean_path, image_path,"
|
||||
" created_at) VALUES (?,?,?,?,?,?,?)",
|
||||
(pid, cid, subject, 1, None, f"data/uploads/{cid}/{img_path.name}", now()))
|
||||
qids = []
|
||||
for i, q in enumerate(questions, start=1):
|
||||
qid = new_id("q")
|
||||
qids.append(qid)
|
||||
conn.execute(
|
||||
"INSERT INTO questions (id, paper_id, child_id, subject, seq, stem, answer, options,"
|
||||
" kp, judge_type, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
|
||||
(qid, pid, cid, q.get("subject") or subject, i, q["stem"], q.get("answer"),
|
||||
json.dumps(q.get("options"), ensure_ascii=False) if q.get("options") else None,
|
||||
q.get("kp"), q.get("judge_type") or "manual", now()))
|
||||
|
||||
# 家长点过的就是错的——直接入库(复用 import_mistakes 的排期与错因逻辑)
|
||||
imported = import_mistakes(conn, gid, cid, qids)
|
||||
|
||||
paper = get_paper(conn, gid, cid, pid)
|
||||
paper["source"] = "scan"
|
||||
paper["ocr"] = {"ok": True, "engine": "tencent", "questions": len(questions),
|
||||
"picked": len(chosen), "blocks": len(blocks),
|
||||
"created": len(imported.get("created") or []),
|
||||
"skipped": len(imported.get("skipped") or []),
|
||||
"image": f"data/uploads/{cid}/{img_path.name}"}
|
||||
return paper
|
||||
|
||||
Reference in New Issue
Block a user