概述
在项目中,需要用到OCR识别,经过搜索,决定使用PaddleOCR.
安装
1、安装paddlepaddle
访问官网:https://www.paddlepaddle.org.cn/
根据自己机器的情况,选择相对应的安装方式。
Linux&&Windows使用pip安装方式是一致的。
1 |
python -m pip install paddlepaddle==2.3.2 -i https://pypi.tuna.tsinghua.edu.cn/simple |
2、安装paddleocr
1 |
pip install paddleocr -i https://mirror.baidu.com/pypi/simple |
3、识别代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# 导入相关包 from paddleocr import PaddleOCR, paddleocr, draw_ocr # 定义ocr ocr = PaddleOCR(se_angle_cls=False, lang="ch", det_model_dir="./inference/det/", rec_model_dir="./inference/rec/", enable_mkldnn=True,use_tensorrt=True,use_angle_cls=False) # 通过paddleocr将图片中的文本转换 def ocr_To_Text(Img): # 识别到的文本以列表形式存储至变量 result = ocr.ocr(Img, cls=False) # 对图片进行标识 from PIL import Image image = Image.open(Img).convert('RGB') boxes = [line[0] for line in result] txts = [line[1][0] for line in result] scores = [line[1][1] for line in result] im_show = draw_ocr(image, boxes, txts, scores, font_path='./fonts/simfang.ttf') im_show = Image.fromarray(im_show) # 保存识别后的图片 im_show.save('result.jpg') ocr_To_Text('.\行程码模板.jpg') |
4、识别结果
paddleocr识别到的数据结果是一个list,每个item包含了文本框,文字和识别置信度。每条数据内容如下:
输出的result.jpg图片效果如下: