cnocr安装和识别文字
cnocr 介绍
参考:
cnocr: 极简的中文OCR Python包
https://cnocr.readthedocs.io/zh/latest/
cnocr 主要针对的是排版简单的印刷体文字图片,如截图图片,扫描件等。目前内置的文字检测和分行模块无法处理复杂的文字排版定位。如果要用于场景文字图片的识别,需要结合其他的场景文字检测引擎使用,例如文字检测引擎 cnstd。
目前使用的识别模型是crnn,识别准确度约为 98.7%
安装
pip install cnocr
import cnocr
# 创建 cnocr 对象
ocr = cnocr.CnOcr()
# 读取图像文件
image_path = './pic/img_2.png'
result = ocr.ocr(image_path)
print(result)
text = ' '.join([x['text'] for x in result])
注意cnocr.CnOcr()可能报错网络失败,
huggingface_hub.utils._errors.LocalEntryNotFoundError: Connection error, and we cannot find the requested files in the disk cache. Please try again or make sure your Internet connection is on.
OR
cnstd.utils.utils.ModelDownloadingError: Failed to download model: densenet_lite_136-fc-onnx.zip.
Please open your VPN and try again.
If this error persists, please follow the instruction at [CnSTD/CnOCR Doc](https://www.breezedeus.com/cnocr) to manually download the model files.
需要自己去下载模型 https://cnocr.readthedocs.io/zh/latest/models/
paddle转onnx格式
https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.6/deploy/paddle2onnx/readme.md
paddle2onnx --model_dir ./cn_PP-OCRv3_det_infer
--model_filename inference.pdmodel
--params_filename inference.pdiparams
--save_file ./det_onnx/model.onnx
--opset_version 10
--input_shape_dict="{'x':[-1,3,-1,-1]}"
--enable_onnx_checker True
paddle2onnx --model_dir ./ch_PP-OCRv3_rec_infer
--model_filename inference.pdmodel
--params_filename inference.pdiparams
--save_file ./rec_onnx/model.onnx
--opset_version 10
--input_shape_dict="{'x':[-1,3,-1,-1]}"
--enable_onnx_checker True
paddle2onnx --model_dir ./inference/ch_ppocr_mobile_v2.0_cls_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ./inference/cls_onnx/model.onnx \
--opset_version 10 \
--input_shape_dict="{'x':[-1,3,-1,-1]}" \
--enable_onnx_checker True
使用paddle 识别模型报维度错误
import cnocr
image_path = './pic/img_2.png'
paddle_ocr = cnocr.CnOcr(rec_model_name='ch_PP-OCRv3')
paddle_ocr.ocr(image_path)
onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: x for the following indices
index: 2 Got: 32 Expected: 48
改源码rec_image_shape: str = “3, 32, 320”,改成rec_image_shape: str = “3, 48, 320”,
结果: