Skip to content

Latest commit

 

History

History
196 lines (134 loc) · 6.53 KB

quickstart.md

File metadata and controls

196 lines (134 loc) · 6.53 KB

PaddleOCR 快速开始

说明: 本文主要介绍PaddleOCR wheel包对PP-OCR系列模型的快速使用,如要体验文档分析相关功能,请参考PP-Structure快速使用教程

1. 安装

1.1 安装PaddlePaddle

如果您没有基础的Python运行环境,请参考运行环境准备

  • 您的机器安装的是CUDA9或CUDA10,请运行以下命令安装

    python3 -m pip install paddlepaddle-gpu -i https://mirror.baidu.com/pypi/simple
  • 您的机器是CPU,请运行以下命令安装

    python3 -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple

更多的版本需求,请参照飞桨官网安装文档中的说明进行操作。

1.2 安装PaddleOCR whl包

pip install "paddleocr>=2.0.1" # 推荐使用2.0.1+版本
  • 对于Windows环境用户:直接通过pip安装的shapely库可能出现[winRrror 126] 找不到指定模块的问题。建议从这里下载shapely安装包完成安装。

2. 便捷使用

2.1 命令行使用

PaddleOCR提供了一系列测试图片,点击这里下载并解压,然后在终端中切换到相应目录

cd /path/to/ppocr_img

如果不使用提供的测试图片,可以将下方--image_dir参数替换为相应的测试图片路径。

注意 whl包默认使用PP-OCRv3模型,识别模型使用的输入shape为3,48,320, 因此如果使用识别功能,需要添加参数--rec_image_shape 3,48,320,如果不使用默认的PP-OCRv3模型,则无需设置该参数。

2.1.1 中英文模型

  • 检测+方向分类器+识别全流程:--use_angle_cls true设置使用方向分类器识别180度旋转文字,--use_gpu false设置不使用GPU

    paddleocr --image_dir ./imgs/11.jpg --use_angle_cls true --use_gpu false --rec_image_shape 3,48,320

    结果是一个list,每个item包含了文本框,文字和识别置信度

    [[[28.0, 37.0], [302.0, 39.0], [302.0, 72.0], [27.0, 70.0]], ('纯臻营养护发素', 0.9658738374710083)]
    ......
  • 单独使用检测:设置--recfalse

    paddleocr --image_dir ./imgs/11.jpg --rec false

    结果是一个list,每个item只包含文本框

    [[27.0, 459.0], [136.0, 459.0], [136.0, 479.0], [27.0, 479.0]]
    [[28.0, 429.0], [372.0, 429.0], [372.0, 445.0], [28.0, 445.0]]
    ......
  • 单独使用识别:设置--detfalse

    paddleocr --image_dir ./imgs_words/ch/word_1.jpg --det false --rec_image_shape 3,48,320

    结果是一个list,每个item只包含识别结果和识别置信度

    ['韩国小馆', 0.994467]

如需使用2.0模型,请指定参数--version PP-OCR,paddleocr默认使用PP-OCRv3模型(--versioin PP-OCRv3)。更多whl包使用可参考whl包文档

2.1.2 多语言模型

Paddleocr目前支持80个语种,可以通过修改--lang参数进行切换,对于英文模型,指定--lang=en, PP-OCRv3目前只支持中文和英文模型,其他多语言模型会陆续更新。

paddleocr --image_dir ./imgs_en/254.jpg --lang=en --rec_image_shape 3,48,320

结果是一个list,每个item包含了文本框,文字和识别置信度

[[[67.0, 51.0], [327.0, 46.0], [327.0, 74.0], [68.0, 80.0]], ('PHOCAPITAL', 0.9944712519645691)]
[[[72.0, 92.0], [453.0, 84.0], [454.0, 114.0], [73.0, 122.0]], ('107 State Street', 0.9744491577148438)]
[[[69.0, 135.0], [501.0, 125.0], [501.0, 156.0], [70.0, 165.0]], ('Montpelier Vermont', 0.9357033967971802)]
......

常用的多语言简写包括

语种 缩写 语种 缩写 语种 缩写
中文 ch 法文 fr 日文 japan
英文 en 德文 german 韩文 korean
繁体中文 chinese_cht 意大利文 it 俄罗斯文 ru

全部语种及其对应的缩写列表可查看多语言模型教程

2.2 Python脚本使用

2.2.1 中英文与多语言使用

通过Python脚本使用PaddleOCR whl包,whl包会自动下载ppocr轻量级模型作为默认模型。

  • 检测+方向分类器+识别全流程
from paddleocr import PaddleOCR, draw_ocr

# Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
ocr = PaddleOCR(use_angle_cls=True, lang="ch")  # need to run only once to download and load model into memory
img_path = './imgs/11.jpg'
result = ocr.ocr(img_path, cls=True)
for line in result:
    print(line)

# 显示结果
from PIL import Image

image = Image.open(img_path).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')

结果是一个list,每个item包含了文本框,文字和识别置信度

[[[28.0, 37.0], [302.0, 39.0], [302.0, 72.0], [27.0, 70.0]], ('纯臻营养护发素', 0.9658738374710083)]
......

结果可视化

3. 小结

通过本节内容,相信您已经熟练掌握PaddleOCR whl包的使用方法并获得了初步效果。

PaddleOCR是一套丰富领先实用的OCR工具库,打通数据、模型训练、压缩和推理部署全流程,您可以参考文档教程,正式开启PaddleOCR的应用之旅。