readme_en.md 9.6 KB
Newer Older
1
# Server-side C++ Inference
littletomatodonkey's avatar
littletomatodonkey 已提交
2

fanruinet's avatar
fanruinet 已提交
3 4 5
This chapter introduces the C++ deployment steps of the PaddleOCR model. The corresponding Python predictive deployment method refers to [document](../../doc/doc_ch/inference.md).
C++ is better than python in terms of performance. Therefore, in CPU and GPU deployment scenarios, C++ deployment is mostly used.
This section will introduce how to configure the C++ environment and deploy PaddleOCR in Linux (CPU\GPU) environment. For Windows deployment please refer to [Windows](./docs/windows_vs2019_build.md) compilation guidelines.
littletomatodonkey's avatar
littletomatodonkey 已提交
6 7


8
## 1. Prepare the Environment
littletomatodonkey's avatar
littletomatodonkey 已提交
9 10 11 12 13 14

### Environment

- Linux, docker is recommended.


15
### 1.1 Compile OpenCV
littletomatodonkey's avatar
littletomatodonkey 已提交
16

fanruinet's avatar
fanruinet 已提交
17
* First of all, you need to download the source code compiled package in the Linux environment from the OpenCV official website. Taking OpenCV 3.4.7 as an example, the download command is as follows.
littletomatodonkey's avatar
littletomatodonkey 已提交
18

littletomatodonkey's avatar
littletomatodonkey 已提交
19
```bash
W
WenmuZhou 已提交
20
cd deploy/cpp_infer
littletomatodonkey's avatar
littletomatodonkey 已提交
21 22
wget https://paddleocr.bj.bcebos.com/libs/opencv/opencv-3.4.7.tar.gz
tar -xf opencv-3.4.7.tar.gz
littletomatodonkey's avatar
littletomatodonkey 已提交
23 24
```

fanruinet's avatar
fanruinet 已提交
25
Finally, you will see the folder of `opencv-3.4.7/` in the current directory.
littletomatodonkey's avatar
littletomatodonkey 已提交
26

fanruinet's avatar
fanruinet 已提交
27
* Compile OpenCV, the OpenCV source path (`root_path`) and installation path (`install_path`) should be set by yourself. Enter the OpenCV source code path and compile it in the following way.
littletomatodonkey's avatar
littletomatodonkey 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59


```shell
root_path=your_opencv_root_path
install_path=${root_path}/opencv3

rm -rf build
mkdir build
cd build

cmake .. \
    -DCMAKE_INSTALL_PREFIX=${install_path} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=OFF \
    -DWITH_IPP=OFF \
    -DBUILD_IPP_IW=OFF \
    -DWITH_LAPACK=OFF \
    -DWITH_EIGEN=OFF \
    -DCMAKE_INSTALL_LIBDIR=lib64 \
    -DWITH_ZLIB=ON \
    -DBUILD_ZLIB=ON \
    -DWITH_JPEG=ON \
    -DBUILD_JPEG=ON \
    -DWITH_PNG=ON \
    -DBUILD_PNG=ON \
    -DWITH_TIFF=ON \
    -DBUILD_TIFF=ON

make -j
make install
```

fanruinet's avatar
fanruinet 已提交
60
In the above commands, `root_path` is the downloaded OpenCV source code path, and `install_path` is the installation path of OpenCV. After `make install` is completed, the OpenCV header file and library file will be generated in this folder for later OCR source code compilation.
littletomatodonkey's avatar
littletomatodonkey 已提交
61 62 63



fanruinet's avatar
fanruinet 已提交
64
The final file structure under the OpenCV installation path is as follows.
littletomatodonkey's avatar
littletomatodonkey 已提交
65 66 67 68 69 70 71 72 73 74

```
opencv3/
|-- bin
|-- include
|-- lib
|-- lib64
|-- share
```

75
### 1.2 Compile or Download or the Paddle Inference Library
littletomatodonkey's avatar
littletomatodonkey 已提交
76 77 78

* There are 2 ways to obtain the Paddle inference library, described in detail below.

littletomatodonkey's avatar
littletomatodonkey 已提交
79
#### 1.2.1 Direct download and installation
littletomatodonkey's avatar
littletomatodonkey 已提交
80

fanruinet's avatar
fanruinet 已提交
81
[Paddle inference library official website](https://paddle-inference.readthedocs.io/en/latest/user_guides/download_lib.html). You can review and select the appropriate version of the inference library on the official website.
littletomatodonkey's avatar
littletomatodonkey 已提交
82 83


fanruinet's avatar
fanruinet 已提交
84
* After downloading, use the following command to extract files.
littletomatodonkey's avatar
littletomatodonkey 已提交
85 86 87 88 89

```
tar -xf paddle_inference.tgz
```

fanruinet's avatar
fanruinet 已提交
90
Finally you will see the the folder of `paddle_inference/` in the current path.
littletomatodonkey's avatar
littletomatodonkey 已提交
91

fanruinet's avatar
fanruinet 已提交
92 93 94
#### 1.2.2 Compile the inference source code
* If you want to get the latest Paddle inference library features, you can download the latest code from Paddle GitHub repository and compile the inference library from the source code. It is recommended to download the inference library with paddle version greater than or equal to 2.0.1.
* You can refer to [Paddle inference library] (https://www.paddlepaddle.org.cn/documentation/docs/en/advanced_guide/inference_deployment/inference/build_and_install_lib_en.html) to get the Paddle source code from GitHub, and then compile To generate the latest inference library. The method of using git to access the code is as follows.
littletomatodonkey's avatar
littletomatodonkey 已提交
95 96 97 98


```shell
git clone https://github.com/PaddlePaddle/Paddle.git
L
fix doc  
LDOUBLEV 已提交
99
git checkout release/2.2
littletomatodonkey's avatar
littletomatodonkey 已提交
100 101
```

fanruinet's avatar
fanruinet 已提交
102
* Enter the Paddle directory and run the following commands to compile the paddle inference library.
littletomatodonkey's avatar
littletomatodonkey 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

```shell
rm -rf build
mkdir build
cd build

cmake  .. \
    -DWITH_CONTRIB=OFF \
    -DWITH_MKL=ON \
    -DWITH_MKLDNN=ON  \
    -DWITH_TESTING=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DWITH_INFERENCE_API_TEST=OFF \
    -DON_INFER=ON \
    -DWITH_PYTHON=ON
make -j
make inference_lib_dist
```

L
LDOUBLEV 已提交
122
For more compilation parameter options, please refer to the [document](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0/guides/05_inference_deployment/inference/build_and_install_lib_cn.html#congyuanmabianyi).
littletomatodonkey's avatar
littletomatodonkey 已提交
123 124


L
LDOUBLEV 已提交
125
* After the compilation process, you can see the following files in the folder of `build/paddle_inference_install_dir/`.
littletomatodonkey's avatar
littletomatodonkey 已提交
126 127

```
L
LDOUBLEV 已提交
128
build/paddle_inference_install_dir/
littletomatodonkey's avatar
littletomatodonkey 已提交
129 130 131 132 133 134
|-- CMakeCache.txt
|-- paddle
|-- third_party
|-- version.txt
```

fanruinet's avatar
fanruinet 已提交
135
`paddle` is the Paddle library required for C++ prediction later, and `version.txt` contains the version information of the current inference library.
littletomatodonkey's avatar
littletomatodonkey 已提交
136 137


138
## 2. Compile and Run the Demo
littletomatodonkey's avatar
littletomatodonkey 已提交
139 140 141

### 2.1 Export the inference model

fanruinet's avatar
fanruinet 已提交
142
* You can refer to [Model inference](../../doc/doc_ch/inference.md) and export the inference model. After the model is exported, assuming it is placed in the `inference` directory, the directory structure is as follows.
littletomatodonkey's avatar
littletomatodonkey 已提交
143 144 145 146

```
inference/
|-- det_db
M
MissPenguin 已提交
147 148
|   |--inference.pdiparams
|   |--inference.pdmodel
littletomatodonkey's avatar
littletomatodonkey 已提交
149
|-- rec_rcnn
M
MissPenguin 已提交
150 151
|   |--inference.pdiparams
|   |--inference.pdmodel
littletomatodonkey's avatar
littletomatodonkey 已提交
152 153 154 155 156 157 158 159 160
```


### 2.2 Compile PaddleOCR C++ inference demo


* The compilation commands are as follows. The addresses of Paddle C++ inference library, opencv and other Dependencies need to be replaced with the actual addresses on your own machines.

```shell
M
MissPenguin 已提交
161
sh tools/build.sh
littletomatodonkey's avatar
littletomatodonkey 已提交
162 163
```

M
MissPenguin 已提交
164
Specifically, you should modify the paths in `tools/build.sh`. The related content is as follows.
littletomatodonkey's avatar
littletomatodonkey 已提交
165 166 167 168 169 170 171 172

```shell
OPENCV_DIR=your_opencv_dir
LIB_DIR=your_paddle_inference_dir
CUDA_LIB_DIR=your_cuda_lib_dir
CUDNN_LIB_DIR=your_cudnn_lib_dir
```

fanruinet's avatar
fanruinet 已提交
173
`OPENCV_DIR` is the OpenCV installation path; `LIB_DIR` is the download (`paddle_inference` folder)
L
LDOUBLEV 已提交
174
or the generated Paddle inference library path (`build/paddle_inference_install_dir` folder);
fanruinet's avatar
fanruinet 已提交
175
`CUDA_LIB_DIR` is the CUDA library file path, in docker; it is `/usr/local/cuda/lib64`; `CUDNN_LIB_DIR` is the cuDNN library file path, in docker it is `/usr/lib/x86_64-linux-gnu/`.
littletomatodonkey's avatar
littletomatodonkey 已提交
176 177


M
MissPenguin 已提交
178
* After the compilation is completed, an executable file named `ppocr` will be generated in the `build` folder.
littletomatodonkey's avatar
littletomatodonkey 已提交
179 180 181


### Run the demo
fanruinet's avatar
fanruinet 已提交
182
Execute the built executable file:
M
MissPenguin 已提交
183 184
```shell
./build/ppocr <mode> [--param1] [--param2] [...]
185
```
fanruinet's avatar
fanruinet 已提交
186 187 188 189 190 191 192 193 194
`mode` is a required parameter,and the valid values are

mode value | Model used
-----|------
det  | Detection only
rec  | Recognition only
system | End-to-end system

Specifically,
M
MissPenguin 已提交
195 196

##### 1. run det demo:
littletomatodonkey's avatar
littletomatodonkey 已提交
197
```shell
M
MissPenguin 已提交
198
./build/ppocr det \
M
MissPenguin 已提交
199
    --det_model_dir=inference/ch_ppocr_mobile_v2.0_det_infer \
M
MissPenguin 已提交
200
    --image_dir=../../doc/imgs/12.jpg
littletomatodonkey's avatar
littletomatodonkey 已提交
201
```
M
MissPenguin 已提交
202
##### 2. run rec demo:
M
MissPenguin 已提交
203
```shell
M
MissPenguin 已提交
204
./build/ppocr rec \
M
MissPenguin 已提交
205
    --rec_model_dir=inference/ch_ppocr_mobile_v2.0_rec_infer \
M
MissPenguin 已提交
206
    --image_dir=../../doc/imgs_words/ch/
Z
zhoujun 已提交
207
```
M
MissPenguin 已提交
208
##### 3. run system demo:
M
MissPenguin 已提交
209 210
```shell
# without text direction classifier
M
MissPenguin 已提交
211
./build/ppocr system \
M
MissPenguin 已提交
212 213
    --det_model_dir=inference/ch_ppocr_mobile_v2.0_det_infer \
    --rec_model_dir=inference/ch_ppocr_mobile_v2.0_rec_infer \
M
MissPenguin 已提交
214 215
    --image_dir=../../doc/imgs/12.jpg
# with text direction classifier
M
MissPenguin 已提交
216
./build/ppocr system \
M
MissPenguin 已提交
217 218 219 220
    --det_model_dir=inference/ch_ppocr_mobile_v2.0_det_infer \
    --use_angle_cls=true \
    --cls_model_dir=inference/ch_ppocr_mobile_v2.0_cls_infer \
    --rec_model_dir=inference/ch_ppocr_mobile_v2.0_rec_infer \
M
MissPenguin 已提交
221 222 223
    --image_dir=../../doc/imgs/12.jpg
```

fanruinet's avatar
fanruinet 已提交
224
More parameters are as follows,
M
MissPenguin 已提交
225

fanruinet's avatar
fanruinet 已提交
226
- Common parameters
M
MissPenguin 已提交
227

M
MissPenguin 已提交
228 229 230 231 232 233 234
|parameter|data type|default|meaning|
| --- | --- | --- | --- |
|use_gpu|bool|false|Whether to use GPU|
|gpu_id|int|0|GPU id when use_gpu is true|
|gpu_mem|int|4000|GPU memory requested|
|cpu_math_library_num_threads|int|10|Number of threads when using CPU inference. When machine cores is enough, the large the value, the faster the inference speed|
|use_mkldnn|bool|true|Whether to use mkdlnn library|
M
MissPenguin 已提交
235

fanruinet's avatar
fanruinet 已提交
236
- Detection related parameters
M
MissPenguin 已提交
237 238 239

|parameter|data type|default|meaning|
| --- | --- | --- | --- |
M
MissPenguin 已提交
240 241 242 243 244 245 246
|det_model_dir|string|-|Address of detection inference model|
|max_side_len|int|960|Limit the maximum image height and width to 960|
|det_db_thresh|float|0.3|Used to filter the binarized image of DB prediction, setting 0.-0.3 has no obvious effect on the result|
|det_db_box_thresh|float|0.5|DB post-processing filter box threshold, if there is a missing box detected, it can be reduced as appropriate|
|det_db_unclip_ratio|float|1.6|Indicates the compactness of the text box, the smaller the value, the closer the text box to the text|
|use_polygon_score|bool|false|Whether to use polygon box to calculate bbox score, false means to use rectangle box to calculate. Use rectangular box to calculate faster, and polygonal box more accurate for curved text area.|
|visualize|bool|true|Whether to visualize the results,when it is set as true, The prediction result will be save in the image file `./ocr_vis.png`.|
M
MissPenguin 已提交
247

fanruinet's avatar
fanruinet 已提交
248
- Classifier related parameters
M
MissPenguin 已提交
249 250 251

|parameter|data type|default|meaning|
| --- | --- | --- | --- |
M
MissPenguin 已提交
252 253 254
|use_angle_cls|bool|false|Whether to use the direction classifier|
|cls_model_dir|string|-|Address of direction classifier inference model|
|cls_thresh|float|0.9|Score threshold of the  direction classifier|
M
MissPenguin 已提交
255

fanruinet's avatar
fanruinet 已提交
256
- Recognition related parameters
M
MissPenguin 已提交
257 258 259

|parameter|data type|default|meaning|
| --- | --- | --- | --- |
M
MissPenguin 已提交
260 261 262 263
|rec_model_dir|string|-|Address of recognition inference model|
|char_list_file|string|../../ppocr/utils/ppocr_keys_v1.txt|dictionary file|

* Multi-language inference is also supported in PaddleOCR, you can refer to [recognition tutorial](../../doc/doc_en/recognition_en.md) for more supported languages and models in PaddleOCR. Specifically, if you want to infer using multi-language models, you just need to modify values of `char_list_file` and `rec_model_dir`.
Z
zhoujun 已提交
264 265


littletomatodonkey's avatar
littletomatodonkey 已提交
266 267 268
The detection results will be shown on the screen, which is as follows.

<div align="center">
littletomatodonkey's avatar
littletomatodonkey 已提交
269
    <img src="./imgs/cpp_infer_pred_12.png" width="600">
littletomatodonkey's avatar
littletomatodonkey 已提交
270 271 272
</div>


Z
zhoujun 已提交
273
### 2.3 Notes
littletomatodonkey's avatar
littletomatodonkey 已提交
274

fanruinet's avatar
fanruinet 已提交
275
* Paddle 2.0.0 inference model library is recommended for this tutorial.