OCR 省钱三件套:结果缓存、按月预算、跳过无谓裁剪;bench 支持 --skip-done/--no-api
This commit is contained in:
+25
-2
@@ -118,8 +118,31 @@ def _crop_bytes(src: Path, box: dict) -> bytes:
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
def recognize_block(src: Path, block: dict) -> dict:
|
||||
"""把单块裁出来再识别一次;失败就退回整页那一块的文字。"""
|
||||
def _looks_complete(block: dict) -> bool:
|
||||
"""整页那一块读得够不够好?够好就别再花钱重新识别一次。
|
||||
|
||||
切题是按次计费的:一页 5 道错题若每道都裁出来重识别,就是 1+5 次。
|
||||
实测整页结果里约七成块本来就是完整的,这些直接复用。
|
||||
"""
|
||||
stem = block.get("stem") or ""
|
||||
if block.get("subs"):
|
||||
return True
|
||||
if len([ch for ch in stem if "\u4e00" <= ch <= "\u9fff"]) >= 8:
|
||||
return True
|
||||
if block.get("judge_type") == "calc" and block.get("answer"):
|
||||
return True
|
||||
if block.get("judge_type") == "choice" and len(block.get("options") or []) >= 2:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def recognize_block(src: Path, block: dict, force_crop: bool = False) -> dict:
|
||||
"""需要时把单块裁出来再识别一次;能不花那次钱就不花。"""
|
||||
if not force_crop and _looks_complete(block):
|
||||
return {"stem": block.get("stem"), "judge_type": block.get("judge_type"),
|
||||
"options": block.get("options"), "answer": block.get("page_answer"),
|
||||
"page_answer": block.get("page_answer"), "subs": block.get("subs") or [],
|
||||
"source": "page"}
|
||||
try:
|
||||
data = _crop_bytes(src, block["box"])
|
||||
except ApiError:
|
||||
|
||||
Reference in New Issue
Block a user