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

Leif's avatar
Leif committed
3
[TOC]
dyning's avatar
dyning committed
4
5


Leif's avatar
Leif committed
6
## 1. 轻量安装
dyning's avatar
dyning committed
7

Leif's avatar
Leif committed
8
### 1.0 运行环境准备
dyning's avatar
dyning committed
9

Leif's avatar
Leif committed
10
如果您未搭建过Python环境,可以通过[零基础Python环境搭建文档](./environment.)进行环境搭建
dyning's avatar
dyning committed
11

Leif's avatar
Leif committed
12
### 1.1 安装PaddlePaddle2.0
dyning's avatar
dyning committed
13

Leif's avatar
Leif committed
14
- 如果您的机器安装的是CUDA9或CUDA10,请运行以下命令安装
WenmuZhou's avatar
WenmuZhou committed
15

Leif's avatar
Leif committed
16
17
18
```bash
python3 -m pip install paddlepaddle-gpu -i https://mirror.baidu.com/pypi/simple
```
WenmuZhou's avatar
WenmuZhou committed
19

Leif's avatar
Leif committed
20
- 如果您的机器是CPU,请运行以下命令安装
dyning's avatar
dyning committed
21

Leif's avatar
Leif committed
22
23
```bash
python3 -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
dyning's avatar
dyning committed
24
```
Leif's avatar
Leif committed
25
26
27
28
29
30
31

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

### 1.2 安装PaddleOCR whl包

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

Leif's avatar
Leif committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
- 对于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
  ```



## 2. 便捷使用

### 2.1 命令行使用

PaddleOCR提供了一系列测试图片,点击xx下载,然后在终端中切换到相应目录
dyning's avatar
dyning committed
51
52

```
Leif's avatar
Leif committed
53
cd /path/to/ppocr_img
dyning's avatar
dyning committed
54
55
```

Leif's avatar
Leif committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
如果不使用提供的测试图片,可以将下方`--image_dir`参数替换为相应的测试图片路径

#### 2.1.1 中英文模型

* 检测+方向分类器+识别全流程:设置方向分类器参数`--use_angle_cls true`后可对竖排文本进行识别。

  ```bash
  paddleocr --image_dir ./imgs/11.jpg --use_angle_cls true
  ```

  结果是一个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]
  ```


更多whl包使用包括, whl包参数说明


dyning's avatar
dyning committed
106

Leif's avatar
Leif committed
107
108
109
110
111
112
#### 2.1.2 多语言模型

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

``` bash
paddleocr --image_dir ./imgs_en/254.jpg --lang=en
dyning's avatar
dyning committed
113
```
Leif's avatar
Leif committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

<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
131
132
```

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

Leif's avatar
Leif committed
135
136
137
138
139
| 语种     | 缩写        |      | 语种     | 缩写   |      | 语种     | 缩写   |
| -------- | ----------- | ---- | -------- | ------ | ---- | -------- | ------ |
| 中文     | ch          |      | 法文     | fr     |      | 日文     | japan  |
| 英文     | en          |      | 德文     | german |      | 韩文     | korean |
| 繁体中文 | chinese_cht |      | 意大利文 | it     |      | 俄罗斯文 | ru     |
dyning's avatar
dyning committed
140

Leif's avatar
Leif committed
141
全部语种及其对应的缩写列表可查看[多语言模型教程](./multi_languages.md)
dyning's avatar
dyning committed
142

Leif's avatar
Leif committed
143
#### 2.1.3 版面分析
dyning's avatar
dyning committed
144

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

Leif's avatar
Leif committed
147
148
```bash
paddleocr --image_dir=./table/1.png --type=structure
dyning's avatar
dyning committed
149
150
```

Leif's avatar
Leif committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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/
    └─ res.txt										
    └─ [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 |

  大部分参数和paddleocr whl包保持一致,见 [whl包文档](../doc/doc_ch/whl.md)

  
tink2123's avatar
tink2123 committed
194

Leif's avatar
Leif committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
### 2.2 Python脚本使用

#### 2.2.1 中英文与多语言使用

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

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

```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
227
228

```bash
Leif's avatar
Leif committed
229
230
231
232
[[[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
233
234
```

Leif's avatar
Leif committed
235
236
237
238
239
240
241
242
243
244
245
246
结果可视化

<div align="center">
    <img src="../imgs_results/whl/11_det_rec.jpg" width="800">
</div>

#### 2.2.2 版面分析使用

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

Leif's avatar
Leif committed
248
table_engine = PPStructure(show_log=True)
dyning's avatar
dyning committed
249

Leif's avatar
Leif committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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')
```
dyning's avatar
dyning committed
268