PaddleOCR安装及使用

概述

在项目中,需要用到OCR识别,经过搜索,决定使用PaddleOCR.

安装

1、安装paddlepaddle

访问官网:https://www.paddlepaddle.org.cn/

根据自己机器的情况,选择相对应的安装方式。

image

 

Linux&&Windows使用pip安装方式是一致的。

python -m pip install paddlepaddle==2.3.2 -i https://pypi.tuna.tsinghua.edu.cn/simple

image

 

2、安装paddleocr

pip install paddleocr -i https://mirror.baidu.com/pypi/simple

image

3、识别代码

# 导入相关包
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包含了文本框,文字和识别置信度。每条数据内容如下:

image

输出的result.jpg图片效果如下:

image

THE END