"...lm-evaluation-harness.git" did not exist on "f14571fbc7cebccd9258b044e5d8ccf26e57846a"
readme.md 11.7 KB
Newer Older
MissPenguin's avatar
MissPenguin committed
1
English | [简体中文](readme_ch.md)
WenmuZhou's avatar
WenmuZhou committed
2

MissPenguin's avatar
update  
MissPenguin committed
3
# Server-side C++ Inference
WenmuZhou's avatar
WenmuZhou committed
4

MissPenguin's avatar
MissPenguin committed
5
6
7
8
9
10
11
12
13
14
15
16
- [1. Prepare the Environment](#1)
    - [1.1 Environment](#11)
    - [1.2 Compile OpenCV](#12)
    - [1.3 Compile or Download or the Paddle Inference Library](#13)
- [2. Compile and Run the Demo](#2)
    - [2.1 Export the inference model](#21)
    - [2.2 Compile PaddleOCR C++ inference demo](#22)
    - [2.3 Run the demo](#23)
- [3. FAQ](#3)


This chapter introduces the C++ deployment steps of the PaddleOCR model. C++ is better than Python in terms of performance. Therefore, in CPU and GPU deployment scenarios, C++ deployment is mostly used.
MissPenguin's avatar
update  
MissPenguin committed
17
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.
WenmuZhou's avatar
WenmuZhou committed
18

19

MissPenguin's avatar
MissPenguin committed
20
<a name="1"></a>
MissPenguin's avatar
update  
MissPenguin committed
21
## 1. Prepare the Environment
littletomatodonkey's avatar
littletomatodonkey committed
22

MissPenguin's avatar
MissPenguin committed
23
24
<a name="11"></a>
### 1.1 Environment
littletomatodonkey's avatar
littletomatodonkey committed
25

MissPenguin's avatar
update  
MissPenguin committed
26
27
- Linux, docker is recommended.
- Windows.
28
29


MissPenguin's avatar
MissPenguin committed
30
31
<a name="12"></a>
### 1.2 Compile OpenCV
32

MissPenguin's avatar
update  
MissPenguin committed
33
* 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 committed
34

littletomatodonkey's avatar
littletomatodonkey committed
35
```bash
WenmuZhou's avatar
WenmuZhou committed
36
cd deploy/cpp_infer
littletomatodonkey's avatar
littletomatodonkey committed
37
38
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 committed
39
40
```

MissPenguin's avatar
update  
MissPenguin committed
41
42
43
Finally, you will see the folder of `opencv-3.4.7/` in the current directory.

* 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 committed
44
45
46


```shell
MissPenguin's avatar
update  
MissPenguin committed
47
root_path=your_opencv_root_path
littletomatodonkey's avatar
littletomatodonkey committed
48
49
install_path=${root_path}/opencv3

MissPenguin's avatar
update  
MissPenguin committed
50
51
52
rm -rf build
mkdir build
cd build
littletomatodonkey's avatar
littletomatodonkey committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

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

MissPenguin's avatar
update  
MissPenguin committed
76
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 committed
77

littletomatodonkey's avatar
littletomatodonkey committed
78
79


MissPenguin's avatar
update  
MissPenguin committed
80
The final file structure under the OpenCV installation path is as follows.
littletomatodonkey's avatar
littletomatodonkey committed
81
82
83
84
85
86
87
88
89
90

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

MissPenguin's avatar
MissPenguin committed
91
92
<a name="13"></a>
### 1.3 Compile or Download or the Paddle Inference Library
93

MissPenguin's avatar
update  
MissPenguin committed
94
* There are 2 ways to obtain the Paddle inference library, described in detail below.
littletomatodonkey's avatar
littletomatodonkey committed
95

MissPenguin's avatar
MissPenguin committed
96
#### 1.3.1 Direct download and installation
littletomatodonkey's avatar
littletomatodonkey committed
97

MissPenguin's avatar
update  
MissPenguin committed
98
[Paddle inference library official website](https://paddleinference.paddlepaddle.org.cn/user_guides/download_lib.html#linux). You can review and select the appropriate version of the inference library on the official website.
LDOUBLEV's avatar
LDOUBLEV committed
99

littletomatodonkey's avatar
littletomatodonkey committed
100

MissPenguin's avatar
update  
MissPenguin committed
101
* After downloading, use the following command to extract files.
littletomatodonkey's avatar
littletomatodonkey committed
102
103
104
105
106

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

MissPenguin's avatar
update  
MissPenguin committed
107
108
Finally you will see the the folder of `paddle_inference/` in the current path.

MissPenguin's avatar
MissPenguin committed
109
#### 1.3.2 Compile the inference source code
MissPenguin's avatar
update  
MissPenguin committed
110
111
* 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 committed
112

littletomatodonkey's avatar
littletomatodonkey committed
113
114
115

```shell
git clone https://github.com/PaddlePaddle/Paddle.git
LDOUBLEV's avatar
LDOUBLEV committed
116
git checkout develop
littletomatodonkey's avatar
littletomatodonkey committed
117
118
```

MissPenguin's avatar
update  
MissPenguin committed
119
* Enter the Paddle directory and run the following commands to compile the paddle inference library.
littletomatodonkey's avatar
littletomatodonkey committed
120
121
122
123
124
125
126
127
128

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

cmake  .. \
    -DWITH_CONTRIB=OFF \
    -DWITH_MKL=ON \
littletomatodonkey's avatar
littletomatodonkey committed
129
    -DWITH_MKLDNN=ON  \
littletomatodonkey's avatar
littletomatodonkey committed
130
131
132
133
134
    -DWITH_TESTING=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DWITH_INFERENCE_API_TEST=OFF \
    -DON_INFER=ON \
    -DWITH_PYTHON=ON
littletomatodonkey's avatar
littletomatodonkey committed
135
make -j
littletomatodonkey's avatar
littletomatodonkey committed
136
137
138
make inference_lib_dist
```

MissPenguin's avatar
update  
MissPenguin committed
139
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 committed
140
141


MissPenguin's avatar
update  
MissPenguin committed
142
* After the compilation process, you can see the following files in the folder of `build/paddle_inference_install_dir/`.
littletomatodonkey's avatar
littletomatodonkey committed
143
144

```
LDOUBLEV's avatar
LDOUBLEV committed
145
build/paddle_inference_install_dir/
littletomatodonkey's avatar
littletomatodonkey committed
146
147
148
149
150
151
|-- CMakeCache.txt
|-- paddle
|-- third_party
|-- version.txt
```

MissPenguin's avatar
update  
MissPenguin committed
152
`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 committed
153

littletomatodonkey's avatar
littletomatodonkey committed
154

MissPenguin's avatar
MissPenguin committed
155
<a name="2"></a>
MissPenguin's avatar
update  
MissPenguin committed
156
## 2. Compile and Run the Demo
littletomatodonkey's avatar
littletomatodonkey committed
157

MissPenguin's avatar
MissPenguin committed
158
<a name="21"></a>
MissPenguin's avatar
update  
MissPenguin committed
159
### 2.1 Export the inference model
160

MissPenguin's avatar
update  
MissPenguin committed
161
* 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 committed
162
163
164
165

```
inference/
|-- det_db
MissPenguin's avatar
MissPenguin committed
166
167
|   |--inference.pdiparams
|   |--inference.pdmodel
littletomatodonkey's avatar
littletomatodonkey committed
168
|-- rec_rcnn
MissPenguin's avatar
MissPenguin committed
169
170
|   |--inference.pdiparams
|   |--inference.pdmodel
171
172
173
|-- cls
|   |--inference.pdiparams
|   |--inference.pdmodel
littletomatodonkey's avatar
littletomatodonkey committed
174
175
176
```


MissPenguin's avatar
MissPenguin committed
177
<a name="22"></a>
MissPenguin's avatar
update  
MissPenguin committed
178
179
180
### 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.
littletomatodonkey's avatar
littletomatodonkey committed
181

MissPenguin's avatar
MissPenguin committed
182
```shell
MissPenguin's avatar
MissPenguin committed
183
sh tools/build.sh
littletomatodonkey's avatar
littletomatodonkey committed
184
185
```

MissPenguin's avatar
update  
MissPenguin committed
186
Specifically, you should modify the paths in `tools/build.sh`. The related content is as follows.
littletomatodonkey's avatar
littletomatodonkey committed
187
188

```shell
littletomatodonkey's avatar
littletomatodonkey committed
189
190
191
OPENCV_DIR=your_opencv_dir
LIB_DIR=your_paddle_inference_dir
CUDA_LIB_DIR=your_cuda_lib_dir
MissPenguin's avatar
update  
MissPenguin committed
192
CUDNN_LIB_DIR=your_cudnn_lib_dir
littletomatodonkey's avatar
littletomatodonkey committed
193
194
```

MissPenguin's avatar
update  
MissPenguin committed
195
196
197
`OPENCV_DIR` is the OpenCV installation path; `LIB_DIR` is the download (`paddle_inference` folder)
or the generated Paddle inference library path (`build/paddle_inference_install_dir` folder);
`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 committed
198

littletomatodonkey's avatar
littletomatodonkey committed
199

MissPenguin's avatar
update  
MissPenguin committed
200
* After the compilation is completed, an executable file named `ppocr` will be generated in the `build` folder.
littletomatodonkey's avatar
littletomatodonkey committed
201

MissPenguin's avatar
MissPenguin committed
202

MissPenguin's avatar
MissPenguin committed
203
204
205
<a name="23"></a>
### 2.3 Run the demo

MissPenguin's avatar
update  
MissPenguin committed
206
Execute the built executable file:
MissPenguin's avatar
MissPenguin committed
207
```shell
208
209
210
./build/ppocr [--param1] [--param2] [...]
```

MissPenguin's avatar
update  
MissPenguin committed
211
212
213
Specifically,

##### 1. det+cls+rec:
214
215
216
217
218
219
220
221
222
```shell
./build/ppocr --det_model_dir=inference/det_db \
    --rec_model_dir=inference/rec_rcnn \
    --cls_model_dir=inference/cls \
    --image_dir=../../doc/imgs/12.jpg \
    --use_angle_cls=true \
    --det=true \
    --rec=true \
    --cls=true \
223
    --rec_img_h=48\
224
225
```

MissPenguin's avatar
update  
MissPenguin committed
226
##### 2. det+rec:
227
228
229
230
231
232
233
234
```shell
./build/ppocr --det_model_dir=inference/det_db \
    --rec_model_dir=inference/rec_rcnn \
    --image_dir=../../doc/imgs/12.jpg \
    --use_angle_cls=false \
    --det=true \
    --rec=true \
    --cls=false \
235
    --rec_img_h=48\
236
237
```

MissPenguin's avatar
update  
MissPenguin committed
238
##### 3. det
239
240
241
242
243
```shell
./build/ppocr --det_model_dir=inference/det_db \
    --image_dir=../../doc/imgs/12.jpg \
    --det=true \
    --rec=false
244
```
MissPenguin's avatar
MissPenguin committed
245

MissPenguin's avatar
update  
MissPenguin committed
246
##### 4. cls+rec:
littletomatodonkey's avatar
littletomatodonkey committed
247
```shell
248
249
250
251
252
253
254
./build/ppocr --rec_model_dir=inference/rec_rcnn \
    --cls_model_dir=inference/cls \
    --image_dir=../../doc/imgs_words/ch/word_1.jpg \
    --use_angle_cls=true \
    --det=false \
    --rec=true \
    --cls=true \
255
    --rec_img_h=48\
zhoujun's avatar
zhoujun committed
256
```
257

MissPenguin's avatar
update  
MissPenguin committed
258
##### 5. rec
MissPenguin's avatar
MissPenguin committed
259
```shell
260
261
262
263
264
265
./build/ppocr --rec_model_dir=inference/rec_rcnn \
    --image_dir=../../doc/imgs_words/ch/word_1.jpg \
    --use_angle_cls=false \
    --det=false \
    --rec=true \
    --cls=false \
266
    --rec_img_h=48\
zhoujun's avatar
zhoujun committed
267
```
268

MissPenguin's avatar
update  
MissPenguin committed
269
##### 6. cls
MissPenguin's avatar
MissPenguin committed
270
```shell
271
272
273
./build/ppocr --cls_model_dir=inference/cls \
    --cls_model_dir=inference/cls \
    --image_dir=../../doc/imgs_words/ch/word_1.jpg \
MissPenguin's avatar
MissPenguin committed
274
    --use_angle_cls=true \
275
276
277
    --det=false \
    --rec=false \
    --cls=true \
MissPenguin's avatar
MissPenguin committed
278
279
```

MissPenguin's avatar
update  
MissPenguin committed
280
More parameters are as follows,
MissPenguin's avatar
MissPenguin committed
281

MissPenguin's avatar
update  
MissPenguin committed
282
- Common parameters
MissPenguin's avatar
MissPenguin committed
283

MissPenguin's avatar
update  
MissPenguin committed
284
285
286
287
288
289
290
291
|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|
|enable_mkldnn|bool|true|Whether to use mkdlnn library|
|output|str|./output|Path where visualization results are saved|
MissPenguin's avatar
MissPenguin committed
292

293

MissPenguin's avatar
update  
MissPenguin committed
294
295
296
- forward

|parameter|data type|default|meaning|
297
298
299
300
301
302
| :---: | :---: | :---: | :---: |
|det|bool|true|前向是否执行文字检测|
|rec|bool|true|前向是否执行文字识别|
|cls|bool|false|前向是否执行文字方向分类|


MissPenguin's avatar
update  
MissPenguin committed
303
- Detection related parameters
MissPenguin's avatar
MissPenguin committed
304

MissPenguin's avatar
update  
MissPenguin committed
305
306
307
308
309
310
311
312
313
|parameter|data type|default|meaning|
| --- | --- | --- | --- |
|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|
|det_db_score_mode|string|slow| slow: use polygon box to calculate bbox score, fast: 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 results will be saved in the folder specified by the `output` field on an image with the same name as the input image.|
MissPenguin's avatar
MissPenguin committed
314

MissPenguin's avatar
update  
MissPenguin committed
315
- Classifier related parameters
MissPenguin's avatar
MissPenguin committed
316

MissPenguin's avatar
update  
MissPenguin committed
317
318
319
320
321
322
|parameter|data type|default|meaning|
| --- | --- | --- | --- |
|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|
|cls_batch_num|int|1|batch size of classifier|
MissPenguin's avatar
MissPenguin committed
323

MissPenguin's avatar
update  
MissPenguin committed
324
- Recognition related parameters
MissPenguin's avatar
MissPenguin committed
325

MissPenguin's avatar
update  
MissPenguin committed
326
327
328
329
330
|parameter|data type|default|meaning|
| --- | --- | --- | --- |
|rec_model_dir|string|-|Address of recognition inference model|
|rec_char_dict_path|string|../../ppocr/utils/ppocr_keys_v1.txt|dictionary file|
|rec_batch_num|int|6|batch size of recognition|
andyjpaddle's avatar
andyjpaddle committed
331
332
|rec_img_h|int|32|image height of recognition|
|rec_img_w|int|320|image width of recognition|
MissPenguin's avatar
MissPenguin committed
333

MissPenguin's avatar
update  
MissPenguin committed
334
* 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 `rec_char_dict_path` and `rec_model_dir`.
MissPenguin's avatar
MissPenguin committed
335

zhoujun's avatar
zhoujun committed
336

MissPenguin's avatar
update  
MissPenguin committed
337
The detection results will be shown on the screen, which is as follows.
littletomatodonkey's avatar
littletomatodonkey committed
338

339
340
341
```bash
predict img: ../../doc/imgs/12.jpg
../../doc/imgs/12.jpg
342
343
344
345
0       det boxes: [[74,553],[427,542],[428,571],[75,582]] rec text: 打浦路252935号 rec score: 0.947724
1       det boxes: [[23,507],[513,488],[515,529],[24,548]] rec text: 绿洲仕格维花园公寓 rec score: 0.993728
2       det boxes: [[187,456],[399,448],[400,480],[188,488]] rec text: 打浦路15号 rec score: 0.964994
3       det boxes: [[42,413],[483,391],[484,428],[43,450]] rec text: 上海斯格威铂尔大酒店 rec score: 0.980086
346
347
The detection visualized image saved in ./output//12.jpg
```
littletomatodonkey's avatar
littletomatodonkey committed
348

MissPenguin's avatar
update  
MissPenguin committed
349

MissPenguin's avatar
MissPenguin committed
350
<a name="3"></a>
WenmuZhou's avatar
WenmuZhou committed
351
## 3. FAQ
littletomatodonkey's avatar
littletomatodonkey committed
352

MissPenguin's avatar
update  
MissPenguin committed
353
 1.  Encountered the error `unable to access 'https://github.com/LDOUBLEV/AutoLog.git/': gnutls_handshake() failed: The TLS connection was non-properly terminated.`, change the github address in `deploy/cpp_infer/external-cmake/auto-log.cmake` to the https://gitee.com/Double_V/AutoLog address.