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”,
在这里插入图片描述
结果:
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值