paddleocr图片文字识别
介绍:PaddleOCR是由百度开发的一个OCR库,基于深度学习框架PaddlePaddle。PaddleOCR支持多语言文本识别,特别适合中文场景,同时它还提供了丰富的预训练模型。
# PaddleOCR 文档
https://www.paddleocr.ai/main/index.html
python3.7.9 自动匹配paddlepaddle版本2.5.2, paddleocr版本2.7.0.2
python3.12.13 自动匹配paddlepaddle版本3.3.1, paddleocr版本3.5.0
1、安装
pip3 install paddlepaddle pip3 install paddleocr
2、使用
from paddleocr import PaddleOCR
def paddle_image():
ocr = PaddleOCR(use_angle_cls=True, lang='ch')
# 读取图像
results = ocr.ocr(image_path, cls=True)
# for (bbox, text) in results[0]: print(f"{text}")
# 打印识别结果
for line in results:
for word_info in line:
print(f"Detected text: {word_info[1][0]}, Confidence: {word_info[1][1]:.2f}")
paddle_image()
3、注意事项
3.1、windows使用时报错pyclipper模块init文件中ImpoerError:DLL load failed:找不到指定的模块。
动态链接库 (DLL), 下载并安装 Microsoft Visual C++ Redistributable。https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170
• 选择与你的 Python 版本匹配的版本(x86 或 x64)。

3.2、未联网环境复制下C:\Users\zhouman02\.paddleocr\whl
联网自动安装:
download https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_det_infer.tar to C:\Users\zhouman02/.paddleocr/whl\det\ch\ch_PP-OCRv4_det_infer\ch_PP-OCRv4_det_infer.tar
100%|██████████| 4.89M/4.89M [00:06<00:00, 758kiB/s]
download https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_rec_infer.tar to C:\Users\zhouman02/.paddleocr/whl\rec\ch\ch_PP-OCRv4_rec_infer\ch_PP-OCRv4_rec_infer.tar
100%|██████████| 11.0M/11.0M [00:05<00:00, 2.19MiB/s]
download https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar to C:\Users\zhouman02/.paddleocr/whl\cls\ch_ppocr_mobile_v2.0_cls_infer\ch_ppocr_mobile_v2.0_cls_infer.tar
100%|██████████| 2.19M/2.19M [00:03<00:00, 658kiB/s]
3.3、报错ModuleNotFoundError: No module named ‘paddle’
paddlepaddle未安装时会报该错误,安装后import paddle则不会报错4.python3.12使用
# ocr_version默认"PP-OCRv5",使用"PP-OCRv4"可以减少代码执行时间
def paddle_image(image_path): ocr = PaddleOCR(lang='ch', ocr_version="PP-OCRv4", use_doc_orientation_classify=False, # 通过 use_doc_orientation_classify 参数指定不使用文档方向分类模型 use_doc_unwarping=False, # 通过 use_doc_unwarping 参数指定不使用文本图像矫正模型 use_textline_orientation=False, # 通过 use_textline_orientation 参数指定不使用文本行方向分类模型 ) # 读取图像 png_text = "" result = ocr.predict(image_path) for page_result in result: rec_texts = page_result.get('rec_texts') if rec_texts: text = "\n".join(rec_texts) print(f"文本: {text}") png_text += f"{text}\n" return png_text
4.1 报错NotImplementedError
报错现象:paddleocr3.5.0使用results = ocr.predict(image_path)报错NotImplementedError: (Unimplemented) ConvertPirAttribute2RuntimeAttribute not support [pir::ArrayAttribute<pir::DoubleAttribute>] (at ..\paddle\fluid\framework\new_executor\instruction\onednn\onednn_instruction.cc:118)
报错含义:PaddleOCR 调用 PaddlePaddle 做推理时,进入了 CPU 的 oneDNN/MKLDNN 加速路径,Paddle 新 IR,也就是 PIR,中有一种属性 `pir::ArrayAttribute<pir::DoubleAttribute>`,当前运行时的 oneDNN 指令转换函数不支持它,所以在 `predictor.run()` 阶段抛出 `NotImplementedError`。同类问题已在 PaddlePaddle issue 中被描述为 PaddlePaddle 3.3.0 的 CPU oneDNN 推理回归问题,现象就是 `ConvertPirAttribute2RuntimeAttribute not support [pir::ArrayAttribute<pir::DoubleAttribute>]`,且 3.2.x 之前可正常运行。([GitHub][1])
# 官方给出的规避方案:在`import paddleocr` 之前,关闭 MKLDNN/oneDNN 验证。
import os os.environ["PADDLE_PDX_ENABLE_MKLDNN_BYDEFAULT"] = "0" from paddleocr import PaddleOCR
4.2 未联网环境复制C:\Users\zhouman02\.paddlex
模型在C:\Users\zhouman02\.paddlex\official_models\

浙公网安备 33010602011771号