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

Merged
zzz merged 2 commits from claude/compassionate-sammet-469997 into main 2026-05-21 11:37:36 +08:00
3 changed files with 332 additions and 586 deletions

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
# OCR 引擎选型决策记录
> 2026-05 决策依据。基于 ~30 张项目截图实测对比,决定换掉 EasyOCR,统一用 **PaddleOCR PP-OCRv4_mobile**。
---
## 结论
| 引擎 | 平均/张 | 关键图精度 | 选用 |
|---|---|---|---|
| **PaddleOCR v4-mobile** | **369ms** | ✅ 12 段全对 | **首选** |
| PaddleOCR v5-mobile | 407ms | ⚠️ 偶尔合并段落 | 略差 |
| RapidOCR-ONNX | 1494ms | ✅ 12 段全对 | 兜底 |
| PaddleOCR v5-server | 2395ms | ⚠️ 多噪声字符 | 不用(重型) |
| EasyOCR | 629ms | ❌ "官方保障"→"膏方保腰" | **回退** |
| PaddleOCR v4-server | 2721ms | ⚠️ 切分错误 | 不用(重型) |
业务图都是裁切后的小区域(300×120 倒计时框、600×260 内容框),mobile 模型完胜 server 模型。
## 关键修复:Win + Paddle 3.x oneDNN+PIR bug
`ConvertPirAttribute2RuntimeAttribute not support pir::ArrayAttribute<pir::DoubleAttribute>` 在 paddle 3.3 + paddleocr 3.5 + Win 必现。
实测结论:
- ❌ `FLAGS_enable_pir_in_executor=0` 修不掉(PIR 已强制默认)
- ❌ paddleocr 3.x 强制要 paddle 3.x,不能降级
- ✅ **`enable_mkldnn=False`** 是当前唯一可行解,代价是 CPU 推理慢 20-30%
代码里在 `operations/ocr_operations.py` 模块顶层设置 `FLAGS_use_mkldnn=false` + `enable_mkldnn=False`。等 paddle 上游补全 `onednn_instruction.cc:118` 的属性转换分支后可以删掉这行。
## 已知次优配置
- PaddleOCR `predict()` 是 3.x 推荐 API,`ocr()` 仍兼容(代码做了运行时检测)
- 自定义"放大+锐化+对比度增强"预处理对 PaddleOCR 是负优化(实测 `coupon_full_title` 输出 `全全场通用` 重复字、`coupon_limit` 多出 `工品` 噪声),已删除。PaddleOCR 内部的 resize/normalize 已经够用
## 速度数据(19 张 + 11 张混合裁切框,CPU 模式)
### 速度横向对比(按场景分组的代表数据)
| 图片类型 | v4-mobile | v5-mobile | EasyOCR |
|---|---|---|---|
| 复杂多文字(coupon.png 整图)| 2293ms | 2148ms | 2168ms |
| 长文字行(banner_long.png)| 898ms | 910ms | 451ms |
| 短文字(coupon_title.png)| 163ms | 162ms | 279ms |
| 数字小框(coupon_30.png)| 18ms | 106ms | 147ms |
| 福袋裁切区域(fudai_*)| ~150-1700ms | ~150-2200ms | ~470-5500ms |
| 空白图标 | ~10-100ms | ~10-150ms | ~30-100ms |
EasyOCR 在彩色背景大图上特别慢(fudai 1280×1280 跑 5 秒+),且关键内容图错字率极高。
### v4-mobile vs v5-mobile 精度差
- coupon.png:v4-mobile `一单回本 | 全场通用` 正确切分,v5-mobile `一单回本全场通用` 合并
- banner_long.png:v4-mobile `上一个 | 下一个` 正确,v5-mobile `E— | 下-` 错误
v4-mobile 综合最优。
## 生效的优化
`operations/ocr_operations.py`:
1. 切到 PaddleOCR v4-mobile 优先(之前 EasyOCR 优先)
2. 加 LRU 结果缓存(256 条),重复图直接命中(业务里 cut_pic 短时间常重读相同区域)
3. 抽出 `PADDLE_KWARGS` / `_build_paddle()` / `_build_easyocr()` 工厂消除重复
`Underlying_Operations.py`:
1. `__init__` / `_ensure_ocr` 都对齐到 Paddle 优先
2. `analyse_pic_word` 加缓存层(基于 blake2b 内容哈希 + change_color)
3. 删掉小图自动放大+锐化+对比度增强(实测负优化)
4. PaddleOCR 切到 `predict()` 新 API
5. `analyse_pic_digits` 从 200 行精简到 30 行委托 + 数字过滤
业务接口完全向后兼容,调用方零改动。
## 测试用图
`assets/`:项目自带 9 张图
裁切扩展:从 coupon.png/fudai.png 裁出多种难度的小区域(~22 张)测试模型对小裁切框的表现
## 维护提示
- 缓存大小:环境变量 `OCR_CACHE_MAX`(默认 256,设 0 禁用)
- 缓存统计:`from operations.ocr_operations import cache_stats; print(cache_stats())`
- 调试图保存:环境变量 `SAVE_OCR_DEBUG_IMAGES=1`
- 跳过初始化(仅做点击不需要 OCR 的流程):环境变量 `SKIP_OCR_INIT=1`
+122 -464
View File
@@ -69,93 +69,32 @@ class underlying_operations:
self._gpu_available = False
if not str(os.environ.get("SKIP_OCR_INIT", "")).strip():
# 优先使用EasyOCR(更快),如果失败则回退到PaddleOCR
# 首先尝试EasyOCR
devnull = None
# 统一通过 operations.ocr_operations 的工厂函数初始化(避免与 OCRProcessor 重复定义)
# 优先 PaddleOCR v4-mobile(实测精度+速度均胜 EasyOCR),失败回退 EasyOCR
try:
# 在Windows上,避免使用stderr重定向,可能会导致"lost sys.stderr"问题
# 改为直接静默导入和初始化
# 尝试按需导入 easyocr,避免静态检查器在缺少依赖时报错
import importlib
from operations.ocr_operations import _build_paddle, _build_easyocr # noqa: WPS433
except Exception:
_build_paddle = None
_build_easyocr = None
try:
easyocr = importlib.import_module("easyocr")
except Exception:
easyocr = None
# 只加载中文和英文,加快初始化速度
# 自动检测GPU可用性,如果可用则启用GPU加速
# 临时重定向输出以减少初始化时的噪音
import io
from contextlib import redirect_stdout, redirect_stderr
fake_stdout = io.StringIO()
fake_stderr = io.StringIO()
if easyocr is not None:
with redirect_stdout(fake_stdout), redirect_stderr(fake_stderr):
self.ocr = easyocr.Reader(
["ch_sim", "en"], gpu=False, verbose=False
)
self.ocr_type = "easyocr"
gpu_status = "CPU模式"
print(f"[OCR] 使用 EasyOCR({gpu_status})")
else:
# 强制抛出异常以触发回退逻辑
raise ImportError("easyocr not available")
try:
if _build_paddle is None:
raise ImportError("ocr_operations._build_paddle 不可用")
self.ocr = _build_paddle()
self.ocr_type = "paddleocr"
print("[OCR] 使用 PaddleOCR v4-mobile(CPU模式)")
except Exception as e:
# EasyOCR初始化失败,回退到PaddleOCR
print(f"[OCR] EasyOCR初始化失败: {e},回退到PaddleOCR")
print(f"[OCR] PaddleOCR 初始化失败: {e},回退到 EasyOCR")
try:
if devnull is not None:
try:
devnull.close()
except Exception:
pass
except Exception:
pass
try:
devnull = open(os.devnull, "w", encoding="utf-8")
with contextlib.redirect_stdout(
devnull
), contextlib.redirect_stderr(devnull):
from paddleocr import PaddleOCR
# 使用 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"
print("[OCR] EasyOCR不可用,使用 PaddleOCR(CPU模式)")
print(
"[OCR] 提示:安装EasyOCR可提升速度,运行: pip install easyocr"
)
except Exception:
# 若静默初始化失败,保留 None 并允许后续按需初始化,避免阻塞启动。
try:
if devnull is not None:
try:
devnull.close()
except Exception:
pass
except Exception:
pass
if _build_easyocr is None:
raise ImportError("ocr_operations._build_easyocr 不可用")
self.ocr = _build_easyocr()
self.ocr_type = "easyocr"
print("[OCR] 使用 EasyOCR(CPU模式,回退引擎)")
except Exception as e2:
print(f"[OCR] 所有 OCR 引擎初始化失败: {e2}")
self.ocr = None
self.ocr_type = None
finally:
try:
if devnull is not None:
devnull.close()
except Exception:
pass
# 注释掉全局日志禁用,以便查看详细的计时日志
# logging.disable(logging.DEBUG)
# logging.disable(logging.WARNING)
@@ -194,76 +133,44 @@ class underlying_operations:
return shlex.split(str(shell_cmd or ""), posix=(os.name != "nt"))
def _ensure_ocr(self):
"""OCR 引擎延迟初始化(与 __init__ 同优先级:Paddle 优先,EasyOCR 兜底)
加锁防止多线程并发触发重复初始化(main 在 ff8de2c 修复 #7 并发安全问题时引入 _ocr_lock)。
"""
if self.ocr is not None:
return
with self._ocr_lock:
# 双检:拿到锁后再确认一次
if self.ocr is not None:
return
# 默认使用CPU模式(延迟初始化时)
# 优先尝试EasyOCR
try:
# 在Windows上,避免使用stderr重定向,可能会导致"lost sys.stderr"问题
import importlib
from operations.ocr_operations import _build_paddle, _build_easyocr # noqa: WPS433
except Exception:
_build_paddle = None
_build_easyocr = None
try:
easyocr = importlib.import_module("easyocr")
except Exception:
easyocr = None
# 临时重定向输出以减少初始化时的噪音
import io
from contextlib import redirect_stdout, redirect_stderr
fake_stdout = io.StringIO()
fake_stderr = io.StringIO()
if easyocr is not None:
with redirect_stdout(fake_stdout), redirect_stderr(fake_stderr):
self.ocr = easyocr.Reader(
["ch_sim", "en"], gpu=False, verbose=False
)
self.ocr_type = "easyocr"
gpu_status = "CPU模式"
print(f"[OCR] 延迟初始化:使用 EasyOCR({gpu_status})")
else:
raise ImportError("easyocr not available")
try:
if _build_paddle is None:
raise ImportError("ocr_operations._build_paddle 不可用")
self.ocr = _build_paddle()
self.ocr_type = "paddleocr"
print("[OCR] 延迟初始化:使用 PaddleOCR v4-mobile(CPU模式)")
return
except Exception as e:
# EasyOCR失败,回退到PaddleOCR
print(f"[OCR] 延迟初始化EasyOCR失败: {e},回退到PaddleOCR")
devnull = None
try:
devnull = open(os.devnull, "w", encoding="utf-8")
with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(
devnull
):
from paddleocr import PaddleOCR
print(f"[OCR] 延迟初始化 PaddleOCR 失败: {e},回退 EasyOCR")
# 使用 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"
print("[OCR] 延迟初始化:使用 PaddleOCR(CPU模式)")
except Exception:
self.ocr = None
self.ocr_type = None
finally:
try:
if devnull is not None:
devnull.close()
except Exception:
pass
try:
if _build_easyocr is None:
raise ImportError("ocr_operations._build_easyocr 不可用")
self.ocr = _build_easyocr()
self.ocr_type = "easyocr"
print("[OCR] 延迟初始化:使用 EasyOCR(CPU模式,回退)")
except Exception as e:
print(f"[OCR] 延迟初始化失败: {e}")
self.ocr = None
self.ocr_type = None
def select_device(self):
"""选择需要连接的设备"""
@@ -1430,12 +1337,12 @@ class underlying_operations:
参数:
picname: 用于分类和调试的图片名(如"福袋内容")
change_color: 颜色处理模式,0=保留彩色,1/2=二值化等
change_color: 颜色处理模式,0=保留彩色(推荐),1/2=二值化等
pic_path: 可选的图片路径,未提供时通过 `_find_pic_path` 查找
return_confidence: 是否返回置信度信息(默认False,保持向后兼容)
返回:
如果return_confidence=False: 识别到的文本(字符串),识别失败返回空字符串
如果return_confidence=True: (text, confidence_info) 元组,其中confidence_info是置信度信息字典
如果return_confidence=True: (text, confidence_info) 元组
"""
if not pic_path:
try:
@@ -1444,67 +1351,53 @@ class underlying_operations:
pic_path = None
if not pic_path or not os.path.exists(pic_path):
return ""
return ("", None) if return_confidence else ""
# ===== 缓存查询:基于图片字节哈希 =====
# 同一张图重复 OCR 直接命中(业务里 cut_pic 可能短时间多次重读相同区域)
cache_key = None
try:
from operations.ocr_operations import (
_img_fingerprint, cache_get, cache_put,
)
with open(pic_path, "rb") as _f:
_bytes = _f.read()
cache_key = _img_fingerprint(_bytes, mode=int(change_color or 0))
cached = cache_get(cache_key)
if cached is not None:
return cached if return_confidence else cached[0]
except Exception:
cache_key = None
# 优化:减少图像验证步骤,直接加载(节省50-100ms)
img = None
try:
# 直接打开图像,不进行verify(节省时间)
# verify会重新读取文件,对于已经存在的文件是多余的
img = Image.open(pic_path)
img.load() # 立即加载图像数据,避免延迟加载
img.load()
except (IOError, OSError, UnidentifiedImageError):
return ""
return ("", None) if return_confidence else ""
if img is None:
return ""
return ("", None) if return_confidence else ""
# 预处理:实测 PaddleOCR v4-mobile 内部已自带 resize/normalize,
# 外层"放大+锐化+对比度增强"反而会引入伪影,让识别变差(tools/test_preprocess_impact.py 实测)。
# 因此默认走原图。仅在 change_color=1/2 业务显式要求二值化时才做。
if change_color in (1, 2):
img = img.convert("L")
if change_color == 1:
img = img.point(lambda x: 0 if int(x) < 128 else 255)
else:
img = img.point(lambda x: 0 if int(x) < 180 else 255)
thresh = 128 if change_color == 1 else 180
img = img.point(lambda x, t=thresh: 0 if int(x) < t else 255)
img = img.convert("RGB")
else:
img = img.convert("RGB")
w, h = img.size
if max(w, h) < 200:
try:
scale = max(2, 300 // max(w, h, 1))
img = img.resize((w * scale, h * scale), _RESAMPLE.BICUBIC)
w, h = img.size
except Exception:
pass
img1 = img # 不再做放大/锐化/对比度,PaddleOCR 自己处理
from PIL import ImageEnhance
# 优化:简化图像预处理,减少不必要的操作
# 对于小图像(<300px),进行锐化和对比度增强;对于大图像,直接使用原图
img1 = img
if max(w, h) < 300:
# 小图像需要增强以提高识别率
try:
img_sharpened = img.filter(
ImageFilter.UnsharpMask(radius=1, percent=150, threshold=3)
)
enhancer = ImageEnhance.Contrast(img_sharpened)
img1 = enhancer.enhance(1.25)
except Exception:
try:
img1 = img.filter(ImageFilter.SHARPEN)
except Exception:
img1 = img # Fallback to original image
# 大图像直接使用,不进行额外处理(节省时间)
# 优化:只在明确启用调试模式时保存调试图像(减少I/O操作)
# 默认不保存,避免每次OCR都进行文件I/O操作(可节省50-200ms)
# 调试图像保存(默认关闭,业务调试时通过 SAVE_OCR_DEBUG_IMAGES=1 启用)
try:
# 检查是否启用调试图像保存
save_debug = os.environ.get("SAVE_OCR_DEBUG_IMAGES", "0") == "1"
if not save_debug:
# 检查analyser配置
try:
save_debug = bool(getattr(self, "save_ocr_debug_images", False))
except Exception:
@@ -1514,7 +1407,6 @@ class underlying_operations:
from douyin.handlers import douyin_screenshot_manager
from douyin.core import path_config
# 尝试获取OCR区域坐标
ocr_coords = None
try:
with self._ocr_coords_lock:
@@ -1522,7 +1414,6 @@ class underlying_operations:
except Exception:
pass
# 加载完整截图
full_screenshot = None
try:
screenshot_path = path_config.get_screenshot_path()
@@ -1533,7 +1424,6 @@ class underlying_operations:
except Exception:
pass
# 如果成功加载完整截图,使用完整截图;否则使用预处理图像
debug_image = full_screenshot if full_screenshot is not None else img1
debug_path = douyin_screenshot_manager.save_debug_image(
debug_image,
@@ -1562,7 +1452,7 @@ class underlying_operations:
# 用于收集置信度信息
ocr_confidence_info = None
# 如果 OCR 未初始化(例如 EasyOCR/PaddleOCR 均不可用),安全降级为不识别
# 如果 OCR 未初始化,安全降级
try:
ocr_obj = getattr(self, "ocr", None)
ocr_type = getattr(self, "ocr_type", None)
@@ -1575,7 +1465,7 @@ class underlying_operations:
logging.warning("OCR 未初始化,跳过识别:picname=%s", picname)
except Exception:
pass
return ""
return ("", None) if return_confidence else ""
try:
if ocr_type == "easyocr":
@@ -1588,9 +1478,16 @@ class underlying_operations:
else:
text = extract_result
else:
result = ocr_obj.ocr(img_np)
# PaddleOCR 3.x:predict() 是新推荐 API;ocr() 仍兼容
_predict = getattr(ocr_obj, "predict", None) or ocr_obj.ocr
result = _predict(img_np)
if result == [None]:
return ""
if cache_key:
try:
cache_put(cache_key, ("", None))
except Exception:
pass
return ("", None) if return_confidence else ""
extract_result = self.extract_ocr_content(
result, return_confidence=True
)
@@ -1603,7 +1500,14 @@ class underlying_operations:
logging.warning("OCR 调用失败,返回空结果:%s", e)
except Exception:
pass
return ""
return ("", None) if return_confidence else ""
# 写入缓存
if cache_key:
try:
cache_put(cache_key, (text, ocr_confidence_info))
except Exception:
pass
# Initialize variables to avoid unbound variable issues
img1 = img # img1 is used later, initialize from img
@@ -1810,284 +1714,38 @@ class underlying_operations:
def analyse_pic_digits(self, picname, change_color=1, pic_path=None):
"""轻量数字OCR识别函数,专门用于识别数字(0-9)。
相比analyse_pic_word,这个函数:
1. 使用二值化预处理(change_color=1),更适合数字识别
2. 只提取数字字符,忽略其他字符
3. 使用更轻量的OCR配置(如果支持)
参数:
picname: 用于分类和调试的图片名(如"福袋价值")
change_color: 颜色处理模式,默认1=二值化(更适合数字)
pic_path: 可选的图片路径,未提供时通过 `_find_pic_path` 查找
返回:
识别到的数字文本(字符串),识别失败返回空字符串
策略:
1. 复用 analyse_pic_word 的核心路径(缓存 / 预处理 / OCR 调用 / 调试图保存)
2. change_color=1/2 触发二值化(数字框背景简单时有效)
3. 在拿到原始文本后做数字过滤与智能段提取(保留 . 用于小数)
"""
if not pic_path:
try:
pic_path = self._find_pic_path(picname)
except Exception:
pic_path = None
if not pic_path or not os.path.exists(pic_path):
text = self.analyse_pic_word(
picname, change_color=change_color, pic_path=pic_path
)
if not text:
return ""
# 优化:减少图像验证步骤,直接加载(节省50-100ms)
img = None
try:
# 直接打开图像,不进行verify(节省时间)
img = Image.open(pic_path)
img.load() # 立即加载图像数据,避免延迟加载
except (IOError, OSError, UnidentifiedImageError):
return ""
# 规范化常见全角/中文标点 -> 英文点
text_norm = (
str(text).replace("。", ".").replace(",", ".").replace(".", ".")
)
digits_only = "".join(ch for ch in text_norm if ch.isdigit())
digits = "".join(ch for ch in text_norm if ch.isdigit() or ch == ".")
if img is None:
return ""
# 智能过滤:识别出的数字过多通常意味着混入了非目标内容
# (例如把文件名/坐标也识别进来),尝试提取合理段
if len(digits_only) > 10:
digit_segments = re.findall(r"\d+(?:\.\d+)?", text_norm)
if digit_segments:
# 价值类:1-6 位最合理;倒计时:1-2 位最合理
reasonable_segments = [
s for s in digit_segments if 1 <= len(s.replace(".", "")) <= 6
]
if reasonable_segments:
return max(reasonable_segments, key=len)
return min(digit_segments, key=len) if digit_segments else digits[:6]
# 数字识别更适合使用二值化处理
img = img.convert("L")
if change_color == 1:
img = img.point(lambda x: 0 if int(x) < 128 else 255)
elif change_color == 2:
img = img.point(lambda x: 0 if int(x) < 180 else 255)
img = img.convert("RGB")
w, h = img.size
# 数字识别需要更高的分辨率
if max(w, h) < 200:
try:
scale = max(2, 400 // max(w, h, 1))
img = img.resize((w * scale, h * scale), _RESAMPLE.BICUBIC)
w, h = img.size
except Exception:
pass
# 优化:简化数字OCR预处理,只对小图像进行增强
from PIL import ImageEnhance
img1 = img
# 对于已经放大过的图像(>=200px),直接使用,不进行额外处理
if max(w, h) < 200:
# 小图像需要增强以提高识别率
try:
img_sharpened = img.filter(
ImageFilter.UnsharpMask(radius=1, percent=200, threshold=3)
)
enhancer = ImageEnhance.Contrast(img_sharpened)
img1 = enhancer.enhance(1.3) # 更高的对比度
except Exception:
try:
img1 = img.filter(ImageFilter.SHARPEN)
except Exception:
img1 = img # Fallback to original image
# 大图像直接使用,不进行额外处理(节省时间)
# 保存预处理后的调试图像(OCR前)- 使用完整截图
try:
from douyin.handlers import douyin_screenshot_manager
from douyin.core import path_config
# 尝试获取OCR区域坐标
ocr_coords = None
try:
ocr_coords = self._ocr_coords_cache.get(picname)
except Exception:
pass
# 加载完整截图
full_screenshot = None
try:
screenshot_path = path_config.get_screenshot_path()
if os.path.exists(screenshot_path):
full_screenshot = Image.open(screenshot_path)
full_screenshot.load()
full_screenshot = full_screenshot.convert("RGB")
except Exception:
pass
# 如果成功加载完整截图,使用完整截图;否则使用预处理图像
debug_image = full_screenshot if full_screenshot is not None else img1
debug_path = douyin_screenshot_manager.save_debug_image(
debug_image,
operation_type="ocr",
picname=picname,
step="preprocessed",
coords=ocr_coords,
extra_info={
"mode": change_color,
"size": f"{w}x{h}",
"source": pic_path if pic_path else picname,
"has_full_screenshot": full_screenshot is not None,
"has_coords": ocr_coords is not None,
"type": "digits",
},
)
if debug_path:
try:
logging.info("[调试图像] OCR数字预处理保存: %s", debug_path)
except Exception:
pass
except Exception:
pass
img_np = np.array(img1)
# 使用OCR识别(与analyse_pic_word相同的OCR引擎)
ocr_confidence_info = None
if getattr(self, "ocr_type", None) == "easyocr":
result = self.ocr.readtext(img_np)
extract_result = self.extract_ocr_content(result, return_confidence=True)
if isinstance(extract_result, tuple):
text, ocr_confidence_info = extract_result
else:
text = extract_result
else:
result = self.ocr.ocr(img_np)
if result == [None]:
return ""
extract_result = self.extract_ocr_content(result, return_confidence=True)
if isinstance(extract_result, tuple):
text, ocr_confidence_info = extract_result
else:
text = extract_result
# 保存OCR结果后的调试图像(带OCR文本结果和置信度)- 使用完整截图
try:
from douyin.handlers import douyin_screenshot_manager
from douyin.core import path_config
# 尝试获取OCR区域坐标
ocr_coords = None
try:
ocr_coords = self._ocr_coords_cache.get(picname)
except Exception:
pass
# 加载完整截图
full_screenshot = None
try:
screenshot_path = path_config.get_screenshot_path()
if os.path.exists(screenshot_path):
full_screenshot = Image.open(screenshot_path)
full_screenshot.load()
full_screenshot = full_screenshot.convert("RGB")
except Exception:
pass
# 如果成功加载完整截图,使用完整截图;否则使用预处理图像
debug_image = full_screenshot if full_screenshot is not None else img1
# 准备额外信息
# 保留小数点(包括全角/中文标点归一化为英文点)以便调试时看到如 24.9 这样的值
digits_result = ""
if text:
t_norm = (
str(text).replace("。", ".").replace(",", ".").replace(".", ".")
)
digits_result = "".join(
ch for ch in t_norm if ch.isdigit() or ch == "."
)
extra_info_dict = {
"mode": change_color,
"text_len": len(text or ""),
"digits": digits_result,
"type": "digits",
}
# 添加置信度信息
if ocr_confidence_info:
extra_info_dict["avg_confidence"] = round(
ocr_confidence_info.get("avg_confidence", 0.0), 4
)
extra_info_dict["min_confidence"] = round(
ocr_confidence_info.get("min_confidence", 0.0), 4
)
extra_info_dict["max_confidence"] = round(
ocr_confidence_info.get("max_confidence", 0.0), 4
)
extra_info_dict["confidence_items"] = ocr_confidence_info.get(
"item_count", 0
)
extra_info_dict["has_full_screenshot"] = full_screenshot is not None
extra_info_dict["has_coords"] = ocr_coords is not None
debug_path = douyin_screenshot_manager.save_debug_image(
debug_image,
operation_type="ocr",
picname=picname,
step="after_ocr",
coords=ocr_coords,
extra_info=extra_info_dict,
)
# 记录OCR置信度到日志
if ocr_confidence_info and ocr_confidence_info.get("item_count", 0) > 0:
try:
logging.info(
"[OCR置信度] %s: 平均=%.4f, 最小=%.4f, 最大=%.4f, 项目数=%d",
picname,
ocr_confidence_info.get("avg_confidence", 0.0),
ocr_confidence_info.get("min_confidence", 0.0),
ocr_confidence_info.get("max_confidence", 0.0),
int(ocr_confidence_info.get("item_count", 0) or 0),
)
except Exception:
pass
if debug_path:
try:
logging.info(
"[调试图像] OCR数字结果保存: %s, 数字: %s",
debug_path,
digits_result,
)
except Exception:
pass
except Exception:
pass
# 只提取数字字符,并进行智能过滤
if text:
# 同时准备两种数字抽取结果:
# 1) digits_only: 仅数字(用于倒计时/位置等只需整数的场景)
# 2) digits: 允许小数点(用于金额/带小数的数值),并规范化常见全角标点
text_norm = (
str(text).replace("。", ".").replace(",", ".").replace(".", ".")
)
digits_only = "".join(ch for ch in text_norm if ch.isdigit())
digits = "".join(ch for ch in text_norm if ch.isdigit() or ch == ".")
# 智能过滤:如果识别到的数字太多(可能是识别到了文件名或坐标),
# 尝试提取合理的数字段
if len(digits_only) > 10:
# 对于价值:通常不超过6位数字(999999元以内)
# 对于倒计时:通常是1-2位数字
# 尝试提取最后几个数字(通常是实际值)
# 或者提取最短的连续数字段
# 提取所有连续的数字段(允许小数点的段落匹配)
digit_segments = re.findall(r"\d+(?:\.\d+)?", text_norm)
if digit_segments:
# 对于价值,选择最大的合理数字段(不超过6位数字,不计小数点)
# 对于倒计时,选择1-2位的数字段
# 这里先返回所有数字,让调用方处理
# 但优先选择较短的段(1-6位),忽略小数点长度影响
reasonable_segments = [
s for s in digit_segments if 1 <= len(s.replace(".", "")) <= 6
]
if reasonable_segments:
# 如果有多段,优先选择较短的(倒计时)或较长的(价值)
# 这里返回最长的合理段(通常是价值)
return max(reasonable_segments, key=len)
else:
# 如果没有合理段,返回最短的段
return (
min(digit_segments, key=len)
if digit_segments
else digits[:6]
)
return digits
return ""
return digits
def extract_ocr_content(self, content=None, return_confidence=False):
"""对OCR识别到的内容进行取值和拼接,变成完整的一段内容
+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",
]