45 lines
2.3 KiB
Python
45 lines
2.3 KiB
Python
"""整页拍端点自检:detect → prepare → import 各跑一次。
|
|
|
|
python tools/scan_e2e.py http://127.0.0.1:8000
|
|
先起后端;图片走 OCR 缓存,重复跑不花钱。
|
|
"""
|
|
import base64, json, pathlib, sys, urllib.request as u
|
|
|
|
BASE = (sys.argv[1] if len(sys.argv) > 1 else "http://127.0.0.1:8000")
|
|
def call(path, body=None, tok=None, method=None):
|
|
req = u.Request(BASE + path, data=None if body is None else json.dumps(body).encode(),
|
|
method=method or ("POST" if body is not None else "GET"),
|
|
headers={"Content-Type": "application/json",
|
|
**({"Authorization": "Bearer " + tok} if tok else {})})
|
|
try:
|
|
with u.urlopen(req, timeout=60) as r:
|
|
return json.loads(r.read().decode())
|
|
except u.HTTPError as e:
|
|
return json.loads(e.read().decode())
|
|
|
|
h = call("/api/health")["data"]
|
|
print("routes", len(h["routes"]) if isinstance(h.get("routes"), list) else h.get("routes"), "wx", h.get("wx_mode"))
|
|
|
|
d = call("/api/auth/sms-code", {"phone": "13800000001"})["data"]
|
|
tok = call("/api/auth/sms-login", {"phone": "13800000001", "code": d["dev_code"]})["data"]["token"]
|
|
call("/api/consent", {"version": "v1"}, tok)
|
|
cid = call("/api/children", {"nickname": "小满", "grade": 2}, tok)["data"]["id"]
|
|
print("child", cid)
|
|
|
|
img = pathlib.Path("data/bench/raw/4111381f63283328.jpg").read_bytes()
|
|
r = call("/api/papers/scan/detect", {"image_b64": base64.b64encode(img).decode(), "filename": "page.jpg"}, tok)["data"]
|
|
blocks = r["blocks"]
|
|
print("draft", r["draft_id"], "size", r["width"], r["height"], "blocks", len(blocks))
|
|
for b in blocks[:6]:
|
|
print(" ", b["i"], b["box"], "subs", len(b.get("subs") or []), "|", (b["stem"] or "")[:28].replace("\n", " "))
|
|
|
|
picked = [b["i"] for b in blocks[:2]]
|
|
prep = call("/api/papers/scan/prepare", {"child_id": cid, "subject": "math", "draft_id": r["draft_id"], "picks": picked}, tok)["data"]
|
|
print("prepare items", len(prep["items"]))
|
|
for it in prep["items"][:6]:
|
|
print(" block", it["block"], "sub", it["sub"], "|", (it["stem"] or "")[:30].replace("\n", " "))
|
|
|
|
imp = call("/api/papers/scan/import", {"child_id": cid, "subject": "math", "draft_id": r["draft_id"], "picks": picked}, tok)["data"]
|
|
print("import ->", {k: imp["ocr"].get(k) for k in ("picked", "created", "blocks")})
|
|
|