PaddleOCR光学字符识别

OCR光学字符识别,将不同类型的文档,图像中的字符保存为可编辑和可搜索的数据。

PaddleOCR

PaddleOCR旨在打造一套丰富、领先、且实用的OCR工具库,助力使用者训练出更好的模型,并应用落地。

  • PPOCR系列高质量预训练模型,准确的识别效果
    • 超轻量ppocr_mobile移动端系列:检测(3.0M)+方向分类器(1.4M)+ 识别(5.0M)= 9.4M
    • 通用ppocr_server系列:检测(47.1M)+方向分类器(1.4M)+ 识别(94.9M)= 143.4M
    • 支持中英文数字组合识别、竖排文本识别、长文本识别
    • 支持80+多语言识别,详见多语言模型
  • 丰富易用的OCR相关工具组件
    • 半自动数据标注工具PPOCRLabel:支持快速高效的数据标注
    • 数据合成工具Style-Text:批量合成大量与目标场景类似的图像
  • 支持用户自定义训练,提供丰富的预测推理部署方案
  • 支持PIP快速安装使用
  • 可运行于Linux、Windows、MacOS等多种系统

  • 预安装 pytorch,安装过程参考ubuntu_NVIDIA30xx,开发环境显卡安装

安装paddleocr

在经历上述过程后,很容易安装paddleocr,运行以下命令:

pip install paddleocr

python -m pip install paddlepaddle-gpu==2.1.3.post112 -f https://www.paddlepaddle.org.cn/whl/windows/mkl/avx/stable.html

安装报错无法解决

安装gpu版本

  • pip install paddlepaddle-gpu -i https://mirror.baidu.com/pypi/simple

安装cpu版本

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

输出结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Looking in indexes: https://mirror.baidu.com/pypi/simple
Collecting paddlepaddle-gpu
Downloading https://mirror.baidu.com/pypi/packages/ad/3f/074aa18eb969f452ebd712809756702fa7ad04aeea25960b77ed19c83870/paddlepaddle_gpu-2.1.3-cp38-cp38-win_amd64.whl (462.5 MB)
|████████████████████████████████| 462.5 MB 1.9 kB/s
Downloading https://mirror.baidu.com/pypi/packages/a4/23/13d2991c156cfd22bfd4a9ae6dcb1a9372004a0e16508b680d17f3280eb4/numpy-1.19.3-cp38-cp38-win_amd64.whl (13.3 MB)
|████████████████████████████████| 13.3 MB 57 kB/s
Requirement already satisfied: certifi>=2017.4.17 in d:\users\vadmin\anaconda3\lib\site-packages (from requests>=2.20.0->paddlepaddle-gpu) (2020.12.5)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\users\vadmin\anaconda3\lib\site-packages (from requests>=2.20.0->paddlepaddle-gpu) (1.26.4)
Requirement already satisfied: chardet<5,>=3.0.2 in d:\users\vadmin\anaconda3\lib\site-packages (from requests>=2.20.0->paddlepaddle-gpu) (4.0.0)
Requirement already satisfied: idna<3,>=2.5 in d:\users\vadmin\anaconda3\lib\site-packages (from requests>=2.20.0->paddlepaddle-gpu) (2.10)
Installing collected packages: numpy, gast, astor, paddlepaddle-gpu
Attempting uninstall: numpy
Found existing installation: numpy 1.20.1
Uninstalling numpy-1.20.1:
Successfully uninstalled numpy-1.20.1
Successfully installed astor-0.8.1 gast-0.3.3 numpy-1.19.3 paddlepaddle-gpu-2.1.3

https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely
安装对应3.8的levenshtein、shapely

使用paddleocr

  • URL图片
    Image='https://www.codekp.cn/download/img/ubuntu/NVIDIA1.png'
  • 本地图片
    Image='labelimg.jpg'

result = ocr.ocr(img_path)

结果

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
# Paddleocr supports Chinese, English, French, German, Korean and Japanese.
# You can set the parameter `lang` as `ch`, `en`, `french`, `german`, `korean`, `japan`
# to switch the language model in order.
#ocr = PaddleOCR(use_angle_cls=True, lang='ch') # need to run only once to download and load model into memory
ocr = PaddleOCR(lang='ch',use_gpu=False)
img_path = 'NVIDIA1.png'
result = ocr.ocr(img_path)
print(result)
for line in result:
print(line)
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)
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')

总结

最后一个参数是可信度。可见可信度、准确度更高。识别时延为100ms-1000ms,对于实时性的系统,建议慎用。