From 7a29633921ccd59e2ee0a862f6b7158fdd3803eb Mon Sep 17 00:00:00 2001 From: xiaoxue_admin <9283713@qq.com> Date: Mon, 22 Jun 2026 07:50:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=AD=97=E5=85=B8=EF=BC=88=E5=86=85=E7=BD=AE=E5=B7=B2?= =?UTF-8?q?=E8=A6=86=E7=9B=96=EF=BC=89=EF=BC=8C=E5=8A=A0=20PaddleOCR=20Non?= =?UTF-8?q?e=20=E9=98=B2=E5=BE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 包装好 --- config.yaml | 4 ++-- core/recognizer.py | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/config.yaml b/config.yaml index 3472fed..cb58533 100644 --- a/config.yaml +++ b/config.yaml @@ -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 diff --git a/core/recognizer.py b/core/recognizer.py index 0bf74c7..287e23c 100644 --- a/core/recognizer.py +++ b/core/recognizer.py @@ -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: