切换 OCR 默认引擎到 PaddleOCR v4-mobile 并简化预处理

- 默认引擎改为 PaddleOCR PP-OCRv4_mobile(实测 369ms/张,比 EasyOCR 629ms 更快且精度大幅提升,coupon 等关键图错字一片 -> 全对)
- EasyOCR 仅作回退,移除"小图自动放大+锐化+对比度增强"预处理(实测对 Paddle 是负优化,会引入伪影/重复字)
- analyse_pic_word 加 LRU 内容哈希缓存(256 条),重复读图直接命中
- analyse_pic_digits 从 200 行重复实现精简成 30 行委托 + 数字过滤
- 统一两处 OCR 初始化为 _build_paddle / _build_easyocr 工厂,消除重复
- 设置 FLAGS_use_mkldnn=false 绕开 Win + Paddle 3.x 的 oneDNN+PIR 不兼容 bug(实测 FLAGS_enable_pir_in_executor=0 也修不掉,关 oneDNN 是当前唯一可行解)
- 决策依据/速度数据/已知问题归档到 docs/ocr_engine_decision.md
- 业务接口完全向后兼容
This commit is contained in:
Codex committed 2026-05-21 10:50:30 +08:00
1 parent 7fb0c3849c
commit 0131be7869
3 files changed
+332 -586

No files matched your search

+126 -122
View File
@@ -7,6 +7,9 @@ from __future__ import annotations
import os
import contextlib
import hashlib
import threading
from collections import OrderedDict
from typing import Any
# PaddleOCR 3.x 会在初始化/导入时进行模型源连通性检查;在国内网络环境下容易卡住。
@@ -14,13 +17,16 @@ from typing import Any
os.environ.setdefault("DISABLE_MODEL_SOURCE_CHECK", "True")
# 额外抑制 paddle/oneDNN/glog 的冗余信息(INFO/WARNING)
os.environ.setdefault("GLOG_minloglevel", "2")
# Win + Paddle 3.x:oneDNN 后端跟新 PIR 执行器有 ConvertPirAttribute2RuntimeAttribute 不兼容 bug。
# paddle 3.3.1 + paddleocr 3.5 + Win 实测:FLAGS_enable_pir_in_executor=0 也修不掉(PIR 已强制默认)。
# 唯一可行解:关 oneDNN。代价是 CPU 推理慢 20-30%,但 v4-mobile 仍有 ~370ms/张实测,业务足够。
# 等 paddle 上游补全 onednn_instruction.cc:118 的属性转换分支后可以删掉这行。
os.environ.setdefault("FLAGS_use_mkldnn", "false")
os.environ.setdefault("FLAGS_call_stack_level", "0")
# 优化OMP_NUM_THREADS:允许底层库使用多线程,但不要设置太高避免线程竞争
# 设置为CPU核心数的一半,平衡性能和资源使用
import os as os_module
cpu_count = os_module.cpu_count() or 4
# 对于OCR任务,每个线程内部会使用多个OMP线程,所以设置为2-4比较合适
# 如果CPU核心数>=8,设置为4;否则设置为2
optimal_omp_threads = min(4, max(2, cpu_count // 3))
os.environ.setdefault("OMP_NUM_THREADS", str(optimal_omp_threads))
@@ -52,6 +58,94 @@ def _ocr_log(msg: str, level: str = "INFO") -> None:
# ===== 结果缓存(基于图片内容哈希,LRU)=====
_OCR_CACHE_MAX = int(os.environ.get("OCR_CACHE_MAX", "256"))
_ocr_cache: "OrderedDict[str, Any]" = OrderedDict()
_ocr_cache_lock = threading.Lock()
_ocr_cache_stats = {"hit": 0, "miss": 0}
def _img_fingerprint(img_bytes: bytes, mode: int = 0) -> str:
"""对图片字节做 blake2b 哈希作为缓存 key(比 sha256 快约 30%)。"""
h = hashlib.blake2b(img_bytes, digest_size=12)
h.update(bytes([mode & 0xFF]))
return h.hexdigest()
def cache_get(key: str):
with _ocr_cache_lock:
if key in _ocr_cache:
_ocr_cache.move_to_end(key)
_ocr_cache_stats["hit"] += 1
return _ocr_cache[key]
_ocr_cache_stats["miss"] += 1
return None
def cache_put(key: str, value: Any) -> None:
with _ocr_cache_lock:
_ocr_cache[key] = value
_ocr_cache.move_to_end(key)
while len(_ocr_cache) > _OCR_CACHE_MAX:
_ocr_cache.popitem(last=False)
def cache_clear() -> None:
with _ocr_cache_lock:
_ocr_cache.clear()
_ocr_cache_stats["hit"] = 0
_ocr_cache_stats["miss"] = 0
def cache_stats() -> dict:
with _ocr_cache_lock:
total = _ocr_cache_stats["hit"] + _ocr_cache_stats["miss"]
hit_rate = (_ocr_cache_stats["hit"] / total) if total else 0.0
return {
"size": len(_ocr_cache),
"max": _OCR_CACHE_MAX,
"hit": _ocr_cache_stats["hit"],
"miss": _ocr_cache_stats["miss"],
"hit_rate": hit_rate,
}
# Paddle OCR 推理参数(统一在这里,避免初始化两处重复)
PADDLE_KWARGS = dict(
ocr_version="PP-OCRv4",
text_detection_model_name="PP-OCRv4_mobile_det",
text_recognition_model_name="PP-OCRv4_mobile_rec",
device="cpu",
cpu_threads=optimal_omp_threads,
enable_mkldnn=False, # 关键:绕开 Win Paddle 3.x oneDNN+PIR bug
precision="fp32",
lang="ch",
use_doc_orientation_classify=False,
use_doc_unwarping=False,
use_textline_orientation=False,
)
def _build_paddle():
"""构造 PaddleOCR 实例。stdout/stderr 静默处理。"""
devnull = open(os.devnull, "w")
try:
with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(devnull):
from paddleocr import PaddleOCR # type: ignore # pylint: disable=import-error
return PaddleOCR(**PADDLE_KWARGS)
finally:
devnull.close()
def _build_easyocr():
"""构造 EasyOCR 实例(回退方案)。"""
import io
from contextlib import redirect_stdout, redirect_stderr
import easyocr # type: ignore[import-untyped] # pylint: disable=import-error
with redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()):
return easyocr.Reader(["ch_sim", "en"], gpu=False, verbose=False)
class OCRProcessor:
"""OCR处理器类"""
@@ -68,130 +162,36 @@ class OCRProcessor:
self._init_ocr()
def _init_ocr(self):
"""初始化OCR引擎"""
# 优先使用EasyOCR(更快),如果失败则回退到PaddleOCR
# 首先尝试EasyOCR
"""初始化OCR引擎
基于 31 张项目截图的 benchmark:
- PaddleOCR v4-mobile:~370ms/张,coupon 关键图全对 ← 首选
- EasyOCR:~630ms/张,coupon 错字一片,仅作回退
"""
# 优先 PaddleOCR v4-mobile
try:
# 在Windows上,避免使用stderr重定向,可能会导致"lost sys.stderr"问题
import easyocr # type: ignore[import-untyped, import] # pyright: ignore[reportMissingImports, reportMissingModuleSource] # pylint: disable=import-error
# 只加载中文和英文,加快初始化速度
# 自动检测GPU可用性,如果可用则启用GPU加速
# 临时重定向输出以减少初始化时的噪音
import io
from contextlib import redirect_stdout, redirect_stderr
fake_stdout = io.StringIO()
fake_stderr = io.StringIO()
with redirect_stdout(fake_stdout), redirect_stderr(fake_stderr):
self.ocr = easyocr.Reader(
["ch_sim", "en"], gpu=False, verbose=False
)
self.ocr_type = "easyocr"
_ocr_log(f"[OCR] 使用 EasyOCR(CPU模式)")
self.ocr = _build_paddle()
self.ocr_type = "paddleocr"
_ocr_log("[OCR] 使用 PaddleOCR v4-mobile(CPU模式)")
return
except Exception as e:
# EasyOCR初始化失败,回退到PaddleOCR
_ocr_log(f"[OCR] EasyOCR初始化失败: {e},回退到PaddleOCR")
devnull = None
try:
devnull = open(os.devnull, "w")
with contextlib.redirect_stdout(
devnull
), contextlib.redirect_stderr(devnull):
from paddleocr import PaddleOCR # type: ignore # pylint: disable=import-error
# 使用 PaddleOCR(强制 CPU 模式,不进行 GPU 检测)
use_gpu = False
self.ocr = PaddleOCR(
lang="ch",
ocr_version="PP-OCRv4",
use_angle_cls=False,
use_gpu=use_gpu,
use_doc_orientation_classify=False,
use_doc_unwarping=False,
use_textline_orientation=False,
)
self.ocr_type = "paddleocr"
_ocr_log(f"[OCR] EasyOCR不可用,使用 PaddleOCR(CPU模式)")
_ocr_log(
f"[OCR] 提示:安装EasyOCR可提升速度,运行: pip install easyocr"
)
except Exception:
# 若静默初始化失败,保留 None 并允许后续按需初始化,避免阻塞启动。
self.ocr = None
self.ocr_type = None
finally:
try:
if devnull is not None:
devnull.close()
except Exception:
pass
_ocr_log(f"[OCR] PaddleOCR 初始化失败: {e},回退到 EasyOCR", "WARNING")
# 回退:EasyOCR
try:
self.ocr = _build_easyocr()
self.ocr_type = "easyocr"
_ocr_log("[OCR] 使用 EasyOCR(CPU模式,回退引擎)")
except Exception as e:
_ocr_log(f"[OCR] 所有 OCR 引擎初始化失败: {e}", "ERROR")
self.ocr = None
self.ocr_type = None
def _ensure_ocr(self):
"""确保OCR引擎已初始化"""
"""确保OCR引擎已初始化(延迟初始化路径)"""
if self.ocr is not None:
return
# 默认使用CPU模式(延迟初始化时)
# 优先尝试EasyOCR
devnull = None
try:
devnull = open(os.devnull, "w")
with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(
devnull
):
import easyocr # type: ignore[import-untyped] # pylint: disable=import-error
self.ocr = easyocr.Reader(
["ch_sim", "en"], gpu=False, verbose=False
)
self.ocr_type = "easyocr"
gpu_status = "CPU模式"
_ocr_log(f"[OCR] 延迟初始化:使用 EasyOCR({gpu_status})")
except Exception:
# EasyOCR失败,回退到PaddleOCR
try:
if devnull is not None:
try:
devnull.close()
except Exception:
pass
except Exception:
pass
try:
devnull = open(os.devnull, "w")
with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(
devnull
):
from paddleocr import PaddleOCR # type: ignore # pylint: disable=import-error
# 使用 PaddleOCR(强制 CPU 模式)
use_gpu = False
self.ocr = PaddleOCR(
lang="ch",
ocr_version="PP-OCRv4",
use_angle_cls=False,
use_gpu=use_gpu,
use_doc_orientation_classify=False,
use_doc_unwarping=False,
use_textline_orientation=False,
)
self.ocr_type = "paddleocr"
_ocr_log(f"[OCR] 延迟初始化:EasyOCR不可用,使用 PaddleOCR(CPU模式)")
except Exception as e:
_ocr_log(f"[OCR] OCR引擎初始化失败: {e}")
self.ocr = None
self.ocr_type = None
finally:
try:
if devnull is not None:
devnull.close()
except Exception:
pass
self._init_ocr()
# 全局OCR处理器实例
@@ -202,8 +202,6 @@ _ocr_processor = OCRProcessor()
_UNDERLYING_OPS_SINGLETON = None
def _ensure_ocr():
"""确保OCR引擎已初始化(向后兼容接口)"""
_ocr_processor._ensure_ocr()
@@ -251,4 +249,10 @@ __all__ = [
"analyse_pic_digits",
"extract_ocr_content",
"_ensure_ocr",
"cache_get",
"cache_put",
"cache_clear",
"cache_stats",
"_img_fingerprint",
"PADDLE_KWARGS",
]