readme_en.md 12.8 KB
Newer Older
W
WenmuZhou 已提交
1 2
English | [简体中文](readme.md)

文幕地方's avatar
文幕地方 已提交
3
- [Service deployment based on PaddleHub Serving](#service-deployment-based-on-paddlehub-serving)
文幕地方's avatar
文幕地方 已提交
4 5 6 7 8 9 10 11 12 13 14
  - [1. Update](#1-update)
  - [2. Quick start service](#2-quick-start-service)
    - [2.1 Prepare the environment](#21-prepare-the-environment)
    - [2.2 Download inference model](#22-download-inference-model)
    - [2.3 Install Service Module](#23-install-service-module)
    - [2.4 Start service](#24-start-service)
      - [2.4.1 Start with command line parameters (CPU only)](#241-start-with-command-line-parameters-cpu-only)
      - [2.4.2 Start with configuration file(CPU、GPU)](#242-start-with-configuration-filecpugpu)
  - [3. Send prediction requests](#3-send-prediction-requests)
  - [4. Returned result format](#4-returned-result-format)
  - [5. User defined service module modification](#5-user-defined-service-module-modification)
文幕地方's avatar
文幕地方 已提交
15 16


W
WenmuZhou 已提交
17 18
PaddleOCR provides 2 service deployment methods:
- Based on **PaddleHub Serving**: Code path is "`./deploy/hubserving`". Please follow this tutorial.
L
LDOUBLEV 已提交
19
- Based on **PaddleServing**: Code path is "`./deploy/pdserving`". Please refer to the [tutorial](../../deploy/pdserving/README.md) for usage.
W
WenmuZhou 已提交
20 21 22

# Service deployment based on PaddleHub Serving  

A
an1018 已提交
23
The hubserving service deployment directory includes seven service packages: text detection, text angle class, text recognition, text detection+text angle class+text recognition three-stage series connection, layout analysis, table recognition and PP-Structure. Please select the corresponding service package to install and start service according to your needs. The directory is as follows:  
W
WenmuZhou 已提交
24 25
```
deploy/hubserving/
文幕地方's avatar
文幕地方 已提交
26 27 28
  └─  ocr_det     text detection module service package
  └─  ocr_cls     text angle class module service package
  └─  ocr_rec     text recognition module service package
文幕地方's avatar
文幕地方 已提交
29
  └─  ocr_system  text detection+text angle class+text recognition three-stage series connection service package
A
an1018 已提交
30
  └─  structure_layout  layout analysis service package
文幕地方's avatar
文幕地方 已提交
31
  └─  structure_table  table recognition service package
文幕地方's avatar
文幕地方 已提交
32
  └─  structure_system  PP-Structure service package
33 34
  └─  kie_ser  KIE(SER) service package
  └─  kie_ser_re  KIE(SER+RE) service package
W
WenmuZhou 已提交
35 36 37 38 39 40 41 42 43 44
```

Each service pack contains 3 files. Take the 2-stage series connection service package as an example, the directory is as follows:  
```
deploy/hubserving/ocr_system/
  └─  __init__.py    Empty file, required
  └─  config.json    Configuration file, optional, passed in as a parameter when using configuration to start the service
  └─  module.py      Main module file, required, contains the complete logic of the service
  └─  params.py      Parameter file, required, including parameters such as model path, pre- and post-processing parameters
```
文幕地方's avatar
文幕地方 已提交
45
## 1. Update
W
WenmuZhou 已提交
46

47 48 49 50
* 2022.10.09 add KIE services.
* 2022.08.23 add layout analysis services.
* 2022.03.30 add PP-Structure and table recognition services.
* 2022.05.05 add PP-OCRv3 text detection and recognition services.
文幕地方's avatar
文幕地方 已提交
51 52 53


## 2. Quick start service
W
WenmuZhou 已提交
54 55
The following steps take the 2-stage series service as an example. If only the detection service or recognition service is needed, replace the corresponding file path.

文幕地方's avatar
文幕地方 已提交
56
### 2.1 Prepare the environment
W
WenmuZhou 已提交
57 58
```shell
# Install paddlehub  
W
opt doc  
WenmuZhou 已提交
59
# python>3.6.2 is required bt paddlehub
littletomatodonkey's avatar
littletomatodonkey 已提交
60
pip3 install paddlehub==2.1.0 --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
W
WenmuZhou 已提交
61 62
```

文幕地方's avatar
文幕地方 已提交
63
### 2.2 Download inference model
A
andyjpaddle 已提交
64
Before installing the service module, you need to prepare the inference model and put it in the correct path. By default, the PP-OCRv3 models are used, and the default model path is:  
W
WenmuZhou 已提交
65
```
A
andyjpaddle 已提交
66 67
text detection model: ./inference/ch_PP-OCRv3_det_infer/
text recognition model: ./inference/ch_PP-OCRv3_rec_infer/
文幕地方's avatar
文幕地方 已提交
68
text angle classifier: ./inference/ch_ppocr_mobile_v2.0_cls_infer/
A
an1018 已提交
69
layout parse model: ./inference/picodet_lcnet_x1_0_fgd_layout_infer/
文幕地方's avatar
文幕地方 已提交
70
tanle recognition: ./inference/ch_ppstructure_mobile_v2.0_SLANet_infer/
71 72
KIE(SER): ./inference/ser_vi_layoutxlm_xfund_infer/
KIE(SER+RE): ./inference/re_vi_layoutxlm_xfund_infer/
W
WenmuZhou 已提交
73 74 75 76
```  

**The model path can be found and modified in `params.py`.** More models provided by PaddleOCR can be obtained from the [model library](../../doc/doc_en/models_list_en.md). You can also use models trained by yourself.

文幕地方's avatar
文幕地方 已提交
77
### 2.3 Install Service Module
文幕地方's avatar
文幕地方 已提交
78
PaddleOCR provides 5 kinds of service modules, install the required modules according to your needs.
W
WenmuZhou 已提交
79 80 81

* On Linux platform, the examples are as follows.
```shell
文幕地方's avatar
文幕地方 已提交
82
# Install the text detection service module:
W
WenmuZhou 已提交
83 84
hub install deploy/hubserving/ocr_det/

文幕地方's avatar
文幕地方 已提交
85
# Or, install the text angle class service module:
W
WenmuZhou 已提交
86 87
hub install deploy/hubserving/ocr_cls/

文幕地方's avatar
文幕地方 已提交
88
# Or, install the text recognition service module:
W
WenmuZhou 已提交
89 90 91 92
hub install deploy/hubserving/ocr_rec/

# Or, install the 2-stage series service module:
hub install deploy/hubserving/ocr_system/
文幕地方's avatar
文幕地方 已提交
93 94 95

# Or install table recognition service module
hub install deploy/hubserving/structure_table/
文幕地方's avatar
文幕地方 已提交
96 97 98

# Or install PP-Structure service module
hub install deploy/hubserving/structure_system/
A
an1018 已提交
99

100 101 102 103 104
# Or install KIE(SER) service module
hub install deploy/hubserving/kie_ser/

# Or install KIE(SER+RE) service module
hub install deploy/hubserving/kie_ser_re/
W
WenmuZhou 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
```

* On Windows platform, the examples are as follows.
```shell
# Install the detection service module:
hub install deploy\hubserving\ocr_det\

# Or, install the angle class service module:
hub install deploy\hubserving\ocr_cls\

# Or, install the recognition service module:
hub install deploy\hubserving\ocr_rec\

# Or, install the 2-stage series service module:
hub install deploy\hubserving\ocr_system\
文幕地方's avatar
文幕地方 已提交
120 121 122

# Or install table recognition service module
hub install deploy/hubserving/structure_table/
文幕地方's avatar
文幕地方 已提交
123 124 125

# Or install PP-Structure service module
hub install deploy\hubserving\structure_system\
A
an1018 已提交
126 127 128

# Or install layout analysis service module
hub install deploy\hubserving\structure_layout\
129 130 131 132 133 134

# Or install KIE(SER) service module
hub install deploy\hubserving\kie_ser\

# Or install KIE(SER+RE) service module
hub install deploy\hubserving\kie_ser_re\
W
WenmuZhou 已提交
135 136
```

文幕地方's avatar
文幕地方 已提交
137 138
### 2.4 Start service
#### 2.4.1 Start with command line parameters (CPU only)
W
WenmuZhou 已提交
139 140 141 142 143 144 145 146 147 148 149

**start command:**  
```shell
$ hub serving start --modules [Module1==Version1, Module2==Version2, ...] \
                    --port XXXX \
                    --use_multiprocess \
                    --workers \
```  
**parameters:**  

|parameters|usage|  
文幕地方's avatar
文幕地方 已提交
150
|---|---|  
W
WenmuZhou 已提交
151 152 153 154 155 156 157 158 159 160 161 162
|--modules/-m|PaddleHub Serving pre-installed model, listed in the form of multiple Module==Version key-value pairs<br>*`When Version is not specified, the latest version is selected by default`*|
|--port/-p|Service port, default is 8866|  
|--use_multiprocess|Enable concurrent mode, the default is single-process mode, this mode is recommended for multi-core CPU machines<br>*`Windows operating system only supports single-process mode`*|
|--workers|The number of concurrent tasks specified in concurrent mode, the default is `2*cpu_count-1`, where `cpu_count` is the number of CPU cores|  

For example, start the 2-stage series service:  
```shell
hub serving start -m ocr_system
```  

This completes the deployment of a service API, using the default port number 8866.  

文幕地方's avatar
文幕地方 已提交
163
#### 2.4.2 Start with configuration file(CPU、GPU)
W
WenmuZhou 已提交
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 194 195 196 197 198 199
**start command:**  
```shell
hub serving start --config/-c config.json
```  
Wherein, the format of `config.json` is as follows:
```python
{
    "modules_info": {
        "ocr_system": {
            "init_args": {
                "version": "1.0.0",
                "use_gpu": true
            },
            "predict_args": {
            }
        }
    },
    "port": 8868,
    "use_multiprocess": false,
    "workers": 2
}
```
- The configurable parameters in `init_args` are consistent with the `_initialize` function interface in `module.py`. Among them, **when `use_gpu` is `true`, it means that the GPU is used to start the service**.
- The configurable parameters in `predict_args` are consistent with the `predict` function interface in `module.py`.

**Note:**  
- When using the configuration file to start the service, other parameters will be ignored.
- If you use GPU prediction (that is, `use_gpu` is set to `true`), you need to set the environment variable CUDA_VISIBLE_DEVICES before starting the service, such as: ```export CUDA_VISIBLE_DEVICES=0```, otherwise you do not need to set it.
- **`use_gpu` and `use_multiprocess` cannot be `true` at the same time.**  

For example, use GPU card No. 3 to start the 2-stage series service:
```shell
export CUDA_VISIBLE_DEVICES=3
hub serving start -c deploy/hubserving/ocr_system/config.json
```  

文幕地方's avatar
文幕地方 已提交
200
## 3. Send prediction requests
W
WenmuZhou 已提交
201 202
After the service starts, you can use the following command to send a prediction request to obtain the prediction result:  
```shell
文幕地方's avatar
文幕地方 已提交
203
python tools/test_hubserving.py --server_url=server_url --image_dir=image_path
W
WenmuZhou 已提交
204 205 206 207 208
```  

Two parameters need to be passed to the script:
- **server_url**:service address,format of which is
`http://[ip_address]:[port]/predict/[module_name]`  
文幕地方's avatar
文幕地方 已提交
209
For example, if using the configuration file to start the text angle classification, text detection, text recognition, detection+classification+recognition 3 stages, table recognition and PP-Structure service, then the `server_url` to send the request will be:
文幕地方's avatar
文幕地方 已提交
210

W
WenmuZhou 已提交
211 212 213 214
`http://127.0.0.1:8865/predict/ocr_det`  
`http://127.0.0.1:8866/predict/ocr_cls`  
`http://127.0.0.1:8867/predict/ocr_rec`  
`http://127.0.0.1:8868/predict/ocr_system`  
A
an1018 已提交
215
`http://127.0.0.1:8869/predict/structure_table`  
文幕地方's avatar
文幕地方 已提交
216
`http://127.0.0.1:8870/predict/structure_system`  
A
an1018 已提交
217
`http://127.0.0.1:8870/predict/structure_layout`  
218 219
`http://127.0.0.1:8871/predict/kie_ser`  
`http://127.0.0.1:8872/predict/kie_ser_re`
文幕地方's avatar
文幕地方 已提交
220 221
- **image_dir**:Test image path, can be a single image path or an image directory path
- **visualize**:Whether to visualize the results, the default value is False
文幕地方's avatar
文幕地方 已提交
222
- **output**:The floder to save Visualization result, default value is `./hubserving_result`
W
WenmuZhou 已提交
223 224 225

**Eg.**
```shell
文幕地方's avatar
文幕地方 已提交
226
python tools/test_hubserving.py --server_url=http://127.0.0.1:8868/predict/ocr_system --image_dir=./doc/imgs/ --visualize=false`
W
WenmuZhou 已提交
227 228
```

文幕地方's avatar
文幕地方 已提交
229
## 4. Returned result format
W
WenmuZhou 已提交
230 231 232 233 234 235 236 237
The returned result is a list. Each item in the list is a dict. The dict may contain three fields. The information is as follows:

|field name|data type|description|
|----|----|----|
|angle|str|angle|
|text|str|text content|
|confidence|float|text recognition confidence|
|text_region|list|text location coordinates|
文幕地方's avatar
文幕地方 已提交
238
|html|str|table html str|
文幕地方's avatar
文幕地方 已提交
239
|regions|list|The result of layout analysis + table recognition + OCR, each item is a list, including `bbox` indicating area coordinates, `type` of area type and `res` of area results|
A
an1018 已提交
240
|layout|list|The result of layout analysis, each item is a dict, including `bbox` indicating area coordinates, `label` of area type|
W
WenmuZhou 已提交
241 242 243

The fields returned by different modules are different. For example, the results returned by the text recognition service module do not contain `text_region`. The details are as follows:

244 245 246 247 248 249 250 251 252 253 254
| field name/module name | ocr_det | ocr_cls | ocr_rec | ocr_system | structure_table | structure_system | structure_layout | kie_ser | kie_re |
|  ---  |  ---  |  ---  |  ---  |  ---  | ---  |  ---  |  ---  | ---  |  ---  |
|angle| | ✔ | | ✔ | |||
|text| | |✔|✔| | ✔ |  | ✔ | ✔ |
|confidence| |✔ |✔| | | ✔| |✔ | ✔ |
|text_region| ✔| | |✔ | | ✔| |✔ | ✔ |
|html| | | | |✔ |✔||| |
|regions| | | | |✔ |✔ | || |
|layout| | | | | | | ✔ || |
|ser_res| | | | | | |  |  ✔ | |
|re_res| | | | | | |  | |  ✔ |
W
WenmuZhou 已提交
255 256 257

**Note:** If you need to add, delete or modify the returned fields, you can modify the file `module.py` of the corresponding module. For the complete process, refer to the user-defined modification service module in the next section.

文幕地方's avatar
文幕地方 已提交
258
## 5. User defined service module modification
W
WenmuZhou 已提交
259 260 261 262 263 264 265 266
If you need to modify the service logic, the following steps are generally required (take the modification of `ocr_system` for example):

- 1. Stop service
```shell
hub serving stop --port/-p XXXX
```
- 2. Modify the code in the corresponding files, like `module.py` and `params.py`, according to the actual needs.  
For example, if you need to replace the model used by the deployed service, you need to modify model path parameters `det_model_dir` and `rec_model_dir` in `params.py`. If you want to turn off the text direction classifier, set the parameter `use_angle_cls` to `False`. Of course, other related parameters may need to be modified at the same time. Please modify and debug according to the actual situation. It is suggested to run `module.py` directly for debugging after modification before starting the service test.  
A
andyjpaddle 已提交
267
**Note** The image input shape used by the PPOCR-v3 recognition model is `3, 48, 320`, so you need to modify `cfg.rec_image_shape = "3, 48, 320"` in `params.py`, if you do not use the PPOCR-v3 recognition model, then there is no need to modify this parameter.
W
WenmuZhou 已提交
268 269 270 271 272 273 274 275 276 277 278 279
- 3. Uninstall old service module
```shell
hub uninstall ocr_system
```
- 4. Install modified service module
```shell
hub install deploy/hubserving/ocr_system/
```
- 5. Restart service
```shell
hub serving start -m ocr_system
```