fix: 移除自定义字典(内置已覆盖),加 PaddleOCR None 防御

- finance_dict.txt 格式错误导致 rec_postprocess IndexError
- 内置 ppocr_keys_v1.txt (6623 chars) 已含全部财务大写字符
- 加 OCR 结果 None/空值防御性处理
- GPU 检测函数增加 paddle.is_compiled_with_cuda() 检查
- config.yaml use_gpu=false 直到 GPU 包装好
This commit is contained in:
2026-06-22 07:50:49 +08:00
parent 11a9c598fd
commit 7a29633921
2 changed files with 15 additions and 4 deletions
+2 -2
View File
@@ -47,7 +47,7 @@ preprocessing:
# OCR 参数(PaddleOCR 原生参数)
ocr:
# === 通用参数 ===
use_gpu: true # 自动检测 GPU,不可用则回退 CPU
use_gpu: false # GPU 包未就绪前强制 CPU
lang: ch
show_log: false
use_angle_cls: true
@@ -75,7 +75,7 @@ ocr:
# === SVTR 文本识别 ===
rec_drop_score: 0.5 # 低于此置信度丢弃
rec_char_dict_path: dict/finance_dict.txt # 自定义财务字典
# rec_char_dict_path: dict/finance_dict.txt # 内置字典已覆盖财务大写
# === TableMaster 表格识别 ===
table_algorithm: TableMaster
+13 -2
View File
@@ -43,8 +43,9 @@ def _build_ocr_params(config: dict, photo_type: str = 'normal'):
def _check_gpu_available() -> bool:
"""检查 GPU 是否可用(不依赖 PaddlePaddle)"""
"""检查 GPU 是否对 PaddleOCR 可用(不仅看 nvidia-smi)"""
try:
# 先看 nvidia-smi
import subprocess
result = subprocess.run(
['nvidia-smi'],
@@ -52,7 +53,11 @@ def _check_gpu_available() -> bool:
text=True,
timeout=5
)
return result.returncode == 0
if result.returncode != 0:
return False
# 再看 PaddlePaddle 能否真正加载 GPU
import paddle
return paddle.is_compiled_with_cuda()
except Exception:
return False
@@ -130,6 +135,12 @@ def run_ocr_det_rec(img, config: dict, photo_type: str = 'normal', verbose=False
ocr = PaddleOCR(**params)
result = ocr.ocr(img, cls=True)
# 防御:PaddleOCR 可能返回 None 或 [None]
if not result or result[0] is None:
if verbose:
print(f" ⚠️ PaddleOCR 未检测到文字", file=sys.stderr)
return []
lines = []
for line_group in result:
for line in line_group: