quickstart.md 10.5 KB
Newer Older
Leif's avatar
Leif committed
1
# PaddleOCR快速开始
Leif's avatar
Leif committed
2

Leif's avatar
Leif committed
3
4
5
6
- [1. 安装](#1)
  - [1.1 安装PaddlePaddle](#11)
  - [1.2 安装PaddleOCR whl包](#12)

MissPenguin's avatar
MissPenguin committed
7
8
- [2. 便捷使用](#2)
  - [2.1 命令行使用](#21)
Evezerest's avatar
Evezerest committed
9
10
11
      - [2.1.1 中英文模型](#211)
      - [2.1.2 多语言模型](#212)
      - [2.1.3 版面分析](#213)
MissPenguin's avatar
MissPenguin committed
12
  - [2.2 Python脚本使用](#22)
Evezerest's avatar
Evezerest committed
13
      - [2.2.1 中英文与多语言使用](#221)
Leif's avatar
Leif committed
14
      - [2.2.2 版面分析](#222)
Leif's avatar
Leif committed
15
- [3.小结](#3)
Leif's avatar
Leif committed
16

Evezerest's avatar
Evezerest committed
17
<a name="1"></a>
WenmuZhou's avatar
WenmuZhou committed
18

Leif's avatar
Leif committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
## 1. 安装

<a name="11"></a>

### 1.1 安装PaddlePaddle

> 如果您没有基础的Python运行环境,请参考[运行环境准备](./environment.md)。

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

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

- 您的机器是CPU,请运行以下命令安装

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

更多的版本需求,请参照[飞桨官网安装文档](https://www.paddlepaddle.org.cn/install/quick)中的说明进行操作。

<a name="12"></a>

### 1.2 安装PaddleOCR whl包
Leif's avatar
Leif committed
44
45
46

```bash
pip install "paddleocr>=2.0.1" # 推荐使用2.0.1+版本
dyning's avatar
dyning committed
47
48
```

Leif's avatar
Leif committed
49
50
51
52
53
54
55
56
57
58
59
- 对于Windows环境用户:

  直接通过pip安装的shapely库可能出现`[winRrror 126] 找不到指定模块的问题`。建议从[这里](https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely)下载shapely安装包完成安装,

- 使用**版面分析**功能时,运行以下命令**安装 Layout-Parser**

  ```bash
  pip3 install -U https://paddleocr.bj.bcebos.com/whl/layoutparser-0.0.0-py3-none-any.whl
  ```


Leif's avatar
Leif committed
60
<a name="2"></a>
Leif's avatar
Leif committed
61
## 2. 便捷使用
Leif's avatar
Leif committed
62
<a name="21"></a>
Leif's avatar
Leif committed
63
64
### 2.1 命令行使用

Leif's avatar
Leif committed
65
PaddleOCR提供了一系列测试图片,点击[这里](https://paddleocr.bj.bcebos.com/dygraph_v2.1/ppocr_img.zip)下载并解压,然后在终端中切换到相应目录
dyning's avatar
dyning committed
66
67

```
Leif's avatar
Leif committed
68
cd /path/to/ppocr_img
dyning's avatar
dyning committed
69
70
```

Leif's avatar
Leif committed
71
如果不使用提供的测试图片,可以将下方`--image_dir`参数替换为相应的测试图片路径
Leif's avatar
Leif committed
72
<a name="211"></a>
Leif's avatar
Leif committed
73
74
#### 2.1.1 中英文模型

75
* 检测+方向分类器+识别全流程:`--use_angle_cls true`设置使用方向分类器识别180度旋转文字,`--use_gpu false`设置不使用GPU
Leif's avatar
Leif committed
76
77

  ```bash
78
  paddleocr --image_dir ./imgs/11.jpg --use_angle_cls true --use_gpu false
Leif's avatar
Leif committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  ```

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

  ```bash
  [[[24.0, 36.0], [304.0, 34.0], [304.0, 72.0], [24.0, 74.0]], ['纯臻营养护发素', 0.964739]]
  [[[24.0, 80.0], [172.0, 80.0], [172.0, 104.0], [24.0, 104.0]], ['产品信息/参数', 0.98069626]]
  [[[24.0, 109.0], [333.0, 109.0], [333.0, 136.0], [24.0, 136.0]], ['(45元/每公斤,100公斤起订)', 0.9676722]]
  ......
  ```

- 单独使用检测:设置`--rec``false`

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

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

  ```bash
  [[26.0, 457.0], [137.0, 457.0], [137.0, 477.0], [26.0, 477.0]]
  [[25.0, 425.0], [372.0, 425.0], [372.0, 448.0], [25.0, 448.0]]
  [[128.0, 397.0], [273.0, 397.0], [273.0, 414.0], [128.0, 414.0]]
  ......
  ```

- 单独使用识别:设置`--det``false`

  ```bash
  paddleocr --image_dir ./imgs_words/ch/word_1.jpg --det false
  ```

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

  ```bash
  ['韩国小馆', 0.9907421]
  ```


118
如需使用2.0模型,请指定参数`--version PP-OCR`,paddleocr默认使用2.1模型(`--versioin PP-OCRv2`)。更多whl包使用可参考[whl包文档](./whl.md)
Leif's avatar
Leif committed
119

Leif's avatar
Leif committed
120
<a name="212"></a>
121

Leif's avatar
Leif committed
122
123
124
125
126
127
#### 2.1.2 多语言模型

Paddleocr目前支持80个语种,可以通过修改`--lang`参数进行切换,对于英文模型,指定`--lang=en`

``` bash
paddleocr --image_dir ./imgs_en/254.jpg --lang=en
dyning's avatar
dyning committed
128
```
Leif's avatar
Leif committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

<div align="center">
    <img src="../imgs_en/254.jpg" width="300" height="600">
    <img src="../imgs_results/multi_lang/img_02.jpg" width="600" height="600">
</div>

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

```text
[('PHO CAPITAL', 0.95723116), [[66.0, 50.0], [327.0, 44.0], [327.0, 76.0], [67.0, 82.0]]]
[('107 State Street', 0.96311164), [[72.0, 90.0], [451.0, 84.0], [452.0, 116.0], [73.0, 121.0]]]
[('Montpelier Vermont', 0.97389287), [[69.0, 132.0], [501.0, 126.0], [501.0, 158.0], [70.0, 164.0]]]
[('8022256183', 0.99810505), [[71.0, 175.0], [363.0, 170.0], [364.0, 202.0], [72.0, 207.0]]]
[('REG 07-24-201706:59 PM', 0.93537045), [[73.0, 299.0], [653.0, 281.0], [654.0, 318.0], [74.0, 336.0]]]
[('045555', 0.99346405), [[509.0, 331.0], [651.0, 325.0], [652.0, 356.0], [511.0, 362.0]]]
[('CT1', 0.9988654), [[535.0, 367.0], [654.0, 367.0], [654.0, 406.0], [535.0, 406.0]]]
......
dyning's avatar
dyning committed
146
147
```

Leif's avatar
Leif committed
148
常用的多语言简写包括
dyning's avatar
dyning committed
149

Leif's avatar
Leif committed
150
151
152
153
154
| 语种     | 缩写        |      | 语种     | 缩写   |      | 语种     | 缩写   |
| -------- | ----------- | ---- | -------- | ------ | ---- | -------- | ------ |
| 中文     | ch          |      | 法文     | fr     |      | 日文     | japan  |
| 英文     | en          |      | 德文     | german |      | 韩文     | korean |
| 繁体中文 | chinese_cht |      | 意大利文 | it     |      | 俄罗斯文 | ru     |
dyning's avatar
dyning committed
155

Leif's avatar
Leif committed
156
全部语种及其对应的缩写列表可查看[多语言模型教程](./multi_languages.md)
Leif's avatar
Leif committed
157
<a name="213"></a>
Leif's avatar
Leif committed
158

Leif's avatar
Leif committed
159
#### 2.1.3 版面分析
dyning's avatar
dyning committed
160

Leif's avatar
Leif committed
161
162
版面分析是指对文档图片中的文字、标题、列表、图片和表格5类区域进行划分。对于前三类区域,直接使用OCR模型完成对应区域文字检测与识别,并将结果保存在txt中。对于表格类区域,经过表格结构化处理后,表格图片转换为相同表格样式的Excel文件。图片区域会被单独裁剪成图像。

Leif's avatar
Leif committed
163
使用PaddleOCR的版面分析功能,需要指定`--type=structure`
dyning's avatar
dyning committed
164

Leif's avatar
Leif committed
165
166
```bash
paddleocr --image_dir=./table/1.png --type=structure
dyning's avatar
dyning committed
167
168
```

Leif's avatar
Leif committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
- **返回结果说明**

  PP-Structure的返回结果为一个dict组成的list,示例如下

  ```shell
  [{   'type': 'Text',
        'bbox': [34, 432, 345, 462],
        'res': ([[36.0, 437.0, 341.0, 437.0, 341.0, 446.0, 36.0, 447.0], [41.0, 454.0, 125.0, 453.0, 125.0, 459.0, 41.0, 460.0]],
                  [('Tigure-6. The performance of CNN and IPT models using difforen', 0.90060663), ('Tent  ', 0.465441)])
    }
  ]
  ```

  其中各个字段说明如下

  | 字段 | 说明                                                         |
  | ---- | ------------------------------------------------------------ |
  | type | 图片区域的类型                                               |
  | bbox | 图片区域的在原图的坐标,分别[左上角x,左上角y,右下角x,右下角y] |
  | res  | 图片区域的OCR或表格识别结果。<br>表格: 表格的HTML字符串; <br>OCR: 一个包含各个单行文字的检测坐标和识别结果的元组 |

  运行完成后,每张图片会在`output`字段指定的目录下有一个同名目录,图片里的每个表格会存储为一个excel,图片区域会被裁剪之后保存下来,excel文件和图片名为表格在图片里的坐标。

  ```
  /output/table/1/
194
    └─ res.txt
Leif's avatar
Leif committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    └─ [454, 360, 824, 658].xlsx  表格识别结果
    └─ [16, 2, 828, 305].jpg			被裁剪出的图片区域
    └─ [17, 361, 404, 711].xlsx		表格识别结果
  ```

- **参数说明**

  | 字段            | 说明                                     | 默认值                                       |
  | --------------- | ---------------------------------------- | -------------------------------------------- |
  | output          | excel和识别结果保存的地址                | ./output/table                               |
  | table_max_len   | 表格结构模型预测时,图像的长边resize尺度 | 488                                          |
  | table_model_dir | 表格结构模型 inference 模型地址          | None                                         |
  | table_char_type | 表格结构模型所用字典地址                 | ../ppocr/utils/dict/table_structure_dict.txt |

Leif's avatar
Leif committed
209
  大部分参数和paddleocr whl包保持一致,见 [whl包文档](./whl.md)
Leif's avatar
Leif committed
210

211

Leif's avatar
Leif committed
212

Leif's avatar
Leif committed
213
<a name="22"></a>
Leif's avatar
Leif committed
214
### 2.2 Python脚本使用
Leif's avatar
Leif committed
215
<a name="221"></a>
Leif's avatar
Leif committed
216
217
#### 2.2.1 中英文与多语言使用

Leif's avatar
Leif committed
218
通过Python脚本使用PaddleOCR whl包,whl包会自动下载ppocr轻量级模型作为默认模型。
Leif's avatar
Leif committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

* 检测+方向分类器+识别全流程

```python
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包含了文本框,文字和识别置信度
WenmuZhou's avatar
WenmuZhou committed
246
247

```bash
Leif's avatar
Leif committed
248
249
250
251
[[[24.0, 36.0], [304.0, 34.0], [304.0, 72.0], [24.0, 74.0]], ['纯臻营养护发素', 0.964739]]
[[[24.0, 80.0], [172.0, 80.0], [172.0, 104.0], [24.0, 104.0]], ['产品信息/参数', 0.98069626]]
[[[24.0, 109.0], [333.0, 109.0], [333.0, 136.0], [24.0, 136.0]], ['(45元/每公斤,100公斤起订)', 0.9676722]]
......
dyning's avatar
dyning committed
252
253
```

Leif's avatar
Leif committed
254
255
256
257
258
结果可视化

<div align="center">
    <img src="../imgs_results/whl/11_det_rec.jpg" width="800">
</div>
Leif's avatar
Leif committed
259
<a name="222"></a>
260

Leif's avatar
Leif committed
261
#### 2.2.2 版面分析
Leif's avatar
Leif committed
262
263
264
265
266

```python
import os
import cv2
from paddleocr import PPStructure,draw_structure_result,save_structure_res
tink2123's avatar
tink2123 committed
267

Leif's avatar
Leif committed
268
table_engine = PPStructure(show_log=True)
dyning's avatar
dyning committed
269

Leif's avatar
Leif committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
save_folder = './output/table'
img_path = './table/paper-image.jpg'
img = cv2.imread(img_path)
result = table_engine(img)
save_structure_res(result, save_folder,os.path.basename(img_path).split('.')[0])

for line in result:
    line.pop('img')
    print(line)

from PIL import Image

font_path = './fonts/simfang.ttf' # PaddleOCR下提供字体包
image = Image.open(img_path).convert('RGB')
im_show = draw_structure_result(image, result,font_path=font_path)
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')
```
Leif's avatar
Leif committed
288
289
290
291
292
293
294
295

<a name="3"></a>

## 3. 小结

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

PaddleOCR是一套丰富领先实用的OCR工具库,打通数据、模型训练、压缩和推理部署全流程,因此在[下一节](./paddleOCR_overview.md)中我们将首先为您介绍PaddleOCR的全景图,然后克隆PaddleOCR项目,正式开启PaddleOCR的应用之旅。