fix: EasyOCR启动异常+超时保护+消息搅和隔离

1. 修复负冷却时间bug:启动失败时若未设置冷却,现在设置30s冷却
2. EasyOCR worker添加超时保护:readtext()使用concurrent.futures包装,30s超时
3. Windows OCR消息搅和隔离:EasyOCR超时+Windows OCR多时间戳→返回空结果
4. 添加easyocr_worker_state.json到.gitignore
This commit is contained in:
youpaishidifu
2026-08-08 22:14:02 +08:00
parent c300a1fd18
commit 09ea05fc79
3 changed files with 34 additions and 3 deletions
+21 -3
View File
@@ -286,8 +286,10 @@ def _init_reader():
return reader
def _process_image(img_b64: str, reader) -> str:
"""解码 base64 图片 → EasyOCR 识别 → 返回空格拼接文本。"""
def _process_image(img_b64: str, reader, timeout: float = 30.0) -> str:
"""解码 base64 图片 → EasyOCR 识别 → 返回空格拼接文本。
timeout: 单张图片OCR超时秒数,防止GPU/CPU推理卡死。
"""
if reader is None:
return ""
raw = base64.b64decode(img_b64)
@@ -296,7 +298,23 @@ def _process_image(img_b64: str, reader) -> str:
if img.mode != "RGBA":
img = img.convert("RGBA")
img_np = np.array(img)
results = reader.readtext(img_np)
# ⭐ 超时保护:用 concurrent.futures 包装 readtext,防止GPU推理卡死
from concurrent.futures import ThreadPoolExecutor, TimeoutError as FuturesTimeoutError
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(reader.readtext, img_np)
try:
results = future.result(timeout=timeout)
except FuturesTimeoutError:
sys.stderr.write(f"[easyocr_worker] readtext 超时({timeout}s),放弃本次OCR\n")
sys.stderr.flush()
_write_crash_log(f"{datetime.now()} [easyocr_worker] readtext 超时({timeout}s)")
return ""
except Exception as e:
sys.stderr.write(f"[easyocr_worker] readtext 异常: {e}\n")
sys.stderr.flush()
raise
return "\n".join([r[1] for r in results]).strip()