- 默认引擎改为 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 - 业务接口完全向后兼容
259 lines
8.8 KiB
Python
259 lines
8.8 KiB
Python
"""OCR操作模块
|
||
|
||
包含OCR识别相关的操作。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import contextlib
|
||
import hashlib
|
||
import threading
|
||
from collections import OrderedDict
|
||
from typing import Any
|
||
|
||
# PaddleOCR 3.x 会在初始化/导入时进行模型源连通性检查;在国内网络环境下容易卡住。
|
||
# 这里提前设置环境变量,避免启动耗时/卡死。
|
||
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:允许底层库使用多线程,但不要设置太高避免线程竞争
|
||
import os as os_module
|
||
cpu_count = os_module.cpu_count() or 4
|
||
optimal_omp_threads = min(4, max(2, cpu_count // 3))
|
||
os.environ.setdefault("OMP_NUM_THREADS", str(optimal_omp_threads))
|
||
|
||
import logging as _logging
|
||
|
||
# 模块级 logger:原代码用 print 输出 OCR 引擎初始化日志,统一走 logging。
|
||
# 保留消息中已有的 "[OCR]" 前缀,避免改变历史搜索习惯。
|
||
_LOGGER = _logging.getLogger("operations.ocr_operations")
|
||
|
||
|
||
def _ocr_log(msg: str, level: str = "INFO") -> None:
|
||
"""统一 OCR 操作日志出口;失败兜底 stdout。"""
|
||
try:
|
||
lv = str(level or "INFO").upper()
|
||
if lv == "ERROR":
|
||
_LOGGER.error(str(msg))
|
||
elif lv == "WARNING":
|
||
_LOGGER.warning(str(msg))
|
||
elif lv == "DEBUG":
|
||
_LOGGER.debug(str(msg))
|
||
else:
|
||
_LOGGER.info(str(msg))
|
||
except Exception:
|
||
try:
|
||
_ocr_log(str(msg))
|
||
except Exception:
|
||
pass
|
||
|
||
|
||
|
||
|
||
# ===== 结果缓存(基于图片内容哈希,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处理器类"""
|
||
|
||
def __init__(self):
|
||
# 关闭文档方向/矫正等重型前处理:我们的截图是手机UI,不是扫描文档。
|
||
# 支持延迟初始化(测试流程/只做点击时不需要 OCR)。
|
||
self.ocr: Any = None
|
||
self.ocr_type = None # 'easyocr' 或 'paddleocr'
|
||
|
||
# 默认使用CPU模式(删除GPU检测)
|
||
self._gpu_available = False
|
||
|
||
if not str(os.environ.get("SKIP_OCR_INIT", "")).strip():
|
||
self._init_ocr()
|
||
|
||
def _init_ocr(self):
|
||
"""初始化OCR引擎
|
||
|
||
基于 31 张项目截图的 benchmark:
|
||
- PaddleOCR v4-mobile:~370ms/张,coupon 关键图全对 ← 首选
|
||
- EasyOCR:~630ms/张,coupon 错字一片,仅作回退
|
||
"""
|
||
# 优先 PaddleOCR v4-mobile
|
||
try:
|
||
self.ocr = _build_paddle()
|
||
self.ocr_type = "paddleocr"
|
||
_ocr_log("[OCR] 使用 PaddleOCR v4-mobile(CPU模式)")
|
||
return
|
||
except Exception as e:
|
||
_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引擎已初始化(延迟初始化路径)"""
|
||
if self.ocr is not None:
|
||
return
|
||
self._init_ocr()
|
||
|
||
|
||
# 全局OCR处理器实例
|
||
_ocr_processor = OCRProcessor()
|
||
|
||
|
||
# 全局OCR底层操作单例(延迟初始化)
|
||
_UNDERLYING_OPS_SINGLETON = None
|
||
|
||
|
||
def _ensure_ocr():
|
||
"""确保OCR引擎已初始化(向后兼容接口)"""
|
||
_ocr_processor._ensure_ocr()
|
||
|
||
|
||
def analyse_pic_word(picname, change_color=0, pic_path=None):
|
||
"""文字OCR识别(向后兼容接口)"""
|
||
# 使用全局复用的 underlying_operations 实例,避免每次调用都初始化 OCR(耗时)
|
||
from douyin.core.underlying_operations import underlying_operations
|
||
global _UNDERLYING_OPS_SINGLETON # type: ignore[name-defined]
|
||
try:
|
||
_UNDERLYING_OPS_SINGLETON # type: ignore[name-defined]
|
||
except NameError:
|
||
_UNDERLYING_OPS_SINGLETON = underlying_operations()
|
||
return _UNDERLYING_OPS_SINGLETON.analyse_pic_word(picname, change_color, pic_path)
|
||
|
||
|
||
def analyse_pic_digits(picname, change_color=1, pic_path=None):
|
||
"""数字OCR识别(向后兼容接口)"""
|
||
# 使用全局复用的 underlying_operations 实例,避免每次调用都初始化 OCR(耗时)
|
||
from douyin.core.underlying_operations import underlying_operations
|
||
global _UNDERLYING_OPS_SINGLETON # type: ignore[name-defined]
|
||
try:
|
||
_UNDERLYING_OPS_SINGLETON # type: ignore[name-defined]
|
||
except NameError:
|
||
_UNDERLYING_OPS_SINGLETON = underlying_operations()
|
||
return _UNDERLYING_OPS_SINGLETON.analyse_pic_digits(picname, change_color, pic_path)
|
||
|
||
|
||
def extract_ocr_content(content=[], return_confidence=False):
|
||
"""提取OCR内容(向后兼容接口)"""
|
||
# 使用全局复用的 underlying_operations 实例
|
||
from douyin.core.underlying_operations import underlying_operations
|
||
global _UNDERLYING_OPS_SINGLETON # type: ignore[name-defined]
|
||
try:
|
||
_UNDERLYING_OPS_SINGLETON # type: ignore[name-defined]
|
||
except NameError:
|
||
_UNDERLYING_OPS_SINGLETON = underlying_operations()
|
||
return _UNDERLYING_OPS_SINGLETON.extract_ocr_content(content, return_confidence)
|
||
|
||
|
||
__all__ = [
|
||
"OCRProcessor",
|
||
"analyse_pic_word",
|
||
"analyse_pic_digits",
|
||
"extract_ocr_content",
|
||
"_ensure_ocr",
|
||
"cache_get",
|
||
"cache_put",
|
||
"cache_clear",
|
||
"cache_stats",
|
||
"_img_fingerprint",
|
||
"PADDLE_KWARGS",
|
||
]
|