readme.md 8.89 KB
Newer Older
littletomatodonkey's avatar
littletomatodonkey committed
1
2
# 服务器端C++预测

LDOUBLEV's avatar
LDOUBLEV committed
3
4
5
本章节介绍PaddleOCR 模型的的C++部署方法,与之对应的python预测部署方式参考[文档](../../doc/doc_ch/inference.md)
C++在性能计算上优于python,因此,在大多数CPU、GPU部署场景,多采用C++的部署方式,本节将介绍如何在Linux\Windows (CPU\GPU)环境下配置C++环境并完成
PaddleOCR模型部署。
littletomatodonkey's avatar
littletomatodonkey committed
6
7
8
9
10
11


## 1. 准备环境

### 运行准备
- Linux环境,推荐使用docker。
12
13
14
- Windows环境,目前支持基于`Visual Studio 2019 Community`进行编译。

* 该文档主要介绍基于Linux环境的PaddleOCR C++预测流程,如果需要在Windows下基于预测库进行C++预测,具体编译方法请参考[Windows下编译教程](./docs/windows_vs2019_build.md)
littletomatodonkey's avatar
littletomatodonkey committed
15
16
17
18
19
20

### 1.1 编译opencv库

* 首先需要从opencv官网上下载在Linux环境下源码编译的包,以opencv3.4.7为例,下载命令如下。

```
WenmuZhou's avatar
WenmuZhou committed
21
cd deploy/cpp_infer
littletomatodonkey's avatar
littletomatodonkey committed
22
23
24
25
26
27
28
29
30
wget https://github.com/opencv/opencv/archive/3.4.7.tar.gz
tar -xf 3.4.7.tar.gz
```

最终可以在当前目录下看到`opencv-3.4.7/`的文件夹。

* 编译opencv,设置opencv源码路径(`root_path`)以及安装路径(`install_path`)。进入opencv源码路径下,按照下面的方式进行编译。

```shell
littletomatodonkey's avatar
littletomatodonkey committed
31
root_path=your_opencv_root_path
littletomatodonkey's avatar
littletomatodonkey committed
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
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
```

littletomatodonkey's avatar
littletomatodonkey committed
60
61
62

其中`root_path`为下载的opencv源码路径,`install_path`为opencv的安装路径,`make install`完成之后,会在该文件夹下生成opencv头文件和库文件,用于后面的OCR代码编译。

littletomatodonkey's avatar
littletomatodonkey committed
63
64
65
66
67
68
69
70
71
72
73
最终在安装路径下的文件结构如下所示。

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

littletomatodonkey's avatar
littletomatodonkey committed
74
75
76
77
### 1.2 下载或者编译Paddle预测库

* 有2种方式获取Paddle预测库,下面进行详细介绍。

LDOUBLEV's avatar
LDOUBLEV committed
78

littletomatodonkey's avatar
littletomatodonkey committed
79
80
#### 1.2.1 直接下载安装

LDOUBLEV's avatar
LDOUBLEV committed
81
* [Paddle预测库官网](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0/guides/05_inference_deployment/inference/build_and_install_lib_cn.html) 上提供了不同cuda版本的Linux预测库,可以在官网查看并选择合适的预测库版本(*建议选择paddle版本>=2.0.1版本的预测库* )。
littletomatodonkey's avatar
littletomatodonkey committed
82
83
84
85
86
87
88
89
90
91

* 下载之后使用下面的方法解压。

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

最终会在当前的文件夹中生成`paddle_inference/`的子文件夹。

#### 1.2.2 预测库源码编译
littletomatodonkey's avatar
littletomatodonkey committed
92
* 如果希望获取最新预测库特性,可以从Paddle github上克隆最新代码,源码编译预测库。
LDOUBLEV's avatar
LDOUBLEV committed
93
* 可以参考[Paddle预测库安装编译说明](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0/guides/05_inference_deployment/inference/build_and_install_lib_cn.html#congyuanmabianyi) 的说明,从github上获取Paddle代码,然后进行编译,生成最新的预测库。使用git获取代码方法如下。
littletomatodonkey's avatar
littletomatodonkey committed
94
95
96

```shell
git clone https://github.com/PaddlePaddle/Paddle.git
LDOUBLEV's avatar
LDOUBLEV committed
97
git checkout release/2.1
littletomatodonkey's avatar
littletomatodonkey committed
98
99
100
101
102
103
104
105
106
107
108
109
```

* 进入Paddle目录后,编译方法如下。

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

cmake  .. \
    -DWITH_CONTRIB=OFF \
    -DWITH_MKL=ON \
littletomatodonkey's avatar
littletomatodonkey committed
110
    -DWITH_MKLDNN=ON  \
littletomatodonkey's avatar
littletomatodonkey committed
111
112
113
114
115
    -DWITH_TESTING=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DWITH_INFERENCE_API_TEST=OFF \
    -DON_INFER=ON \
    -DWITH_PYTHON=ON
littletomatodonkey's avatar
littletomatodonkey committed
116
make -j
littletomatodonkey's avatar
littletomatodonkey committed
117
118
119
make inference_lib_dist
```

LDOUBLEV's avatar
LDOUBLEV committed
120
更多编译参数选项介绍可以参考[文档说明](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
121
122


LDOUBLEV's avatar
LDOUBLEV committed
123
* 编译完成之后,可以在`build/paddle_inference_install_dir/`文件下看到生成了以下文件及文件夹。
littletomatodonkey's avatar
littletomatodonkey committed
124
125

```
LDOUBLEV's avatar
LDOUBLEV committed
126
build/paddle_inference_install_dir/
littletomatodonkey's avatar
littletomatodonkey committed
127
128
129
130
131
132
|-- CMakeCache.txt
|-- paddle
|-- third_party
|-- version.txt
```

LDOUBLEV's avatar
LDOUBLEV committed
133
其中`paddle`就是C++预测所需的Paddle库,`version.txt`中包含当前预测库的版本信息。
littletomatodonkey's avatar
littletomatodonkey committed
134

littletomatodonkey's avatar
littletomatodonkey committed
135

littletomatodonkey's avatar
littletomatodonkey committed
136
137
138
139
140
141
142
143
144
## 2 开始运行

### 2.1 将模型导出为inference model

* 可以参考[模型预测章节](../../doc/doc_ch/inference.md),导出inference model,用于模型预测。模型导出之后,假设放在`inference`目录下,则目录结构如下。

```
inference/
|-- det_db
MissPenguin's avatar
MissPenguin committed
145
146
|   |--inference.pdiparams
|   |--inference.pdmodel
littletomatodonkey's avatar
littletomatodonkey committed
147
|-- rec_rcnn
MissPenguin's avatar
MissPenguin committed
148
149
|   |--inference.pdiparams
|   |--inference.pdmodel
littletomatodonkey's avatar
littletomatodonkey committed
150
151
152
153
154
155
156
```


### 2.2 编译PaddleOCR C++预测demo

* 编译命令如下,其中Paddle C++预测库、opencv等其他依赖库的地址需要换成自己机器上的实际地址。

MissPenguin's avatar
MissPenguin committed
157
```shell
MissPenguin's avatar
MissPenguin committed
158
sh tools/build.sh
littletomatodonkey's avatar
littletomatodonkey committed
159
160
```

MissPenguin's avatar
MissPenguin committed
161
* 具体的,需要修改`tools/build.sh`中环境路径,相关内容如下:
littletomatodonkey's avatar
littletomatodonkey committed
162
163

```shell
littletomatodonkey's avatar
littletomatodonkey committed
164
165
166
167
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
littletomatodonkey's avatar
littletomatodonkey committed
168
169
```

MissPenguin's avatar
MissPenguin committed
170
其中,`OPENCV_DIR`为opencv编译安装的地址;`LIB_DIR`为下载(`paddle_inference`文件夹)或者编译生成的Paddle预测库地址(`build/paddle_inference_install_dir`文件夹);`CUDA_LIB_DIR`为cuda库文件地址,在docker中为`/usr/local/cuda/lib64``CUDNN_LIB_DIR`为cudnn库文件地址,在docker中为`/usr/lib/x86_64-linux-gnu/`**注意:以上路径都写绝对路径,不要写相对路径。**
littletomatodonkey's avatar
littletomatodonkey committed
171
172


MissPenguin's avatar
MissPenguin committed
173
* 编译完成之后,会在`build`文件夹下生成一个名为`ppocr`的可执行文件。
littletomatodonkey's avatar
littletomatodonkey committed
174
175
176


### 运行demo
MissPenguin's avatar
MissPenguin committed
177
178
179
180
181
182
183
184

运行方式:  
```shell
./build/ppocr <mode> [--param1] [--param2] [...]
```  
其中,`mode`为必选参数,表示选择的功能,取值范围['det', 'rec', 'system'],分别表示调用检测、识别、检测识别串联(包括方向分类器)。具体命令如下:

##### 1. 只调用检测:
littletomatodonkey's avatar
littletomatodonkey committed
185
```shell
MissPenguin's avatar
MissPenguin committed
186
./build/ppocr det \
MissPenguin's avatar
MissPenguin committed
187
    --det_model_dir=inference/ch_ppocr_mobile_v2.0_det_infer \
MissPenguin's avatar
MissPenguin committed
188
    --image_dir=../../doc/imgs/12.jpg
zhoujun's avatar
zhoujun committed
189
```
MissPenguin's avatar
MissPenguin committed
190
##### 2. 只调用识别:
MissPenguin's avatar
MissPenguin committed
191
```shell
MissPenguin's avatar
MissPenguin committed
192
./build/ppocr rec \
MissPenguin's avatar
MissPenguin committed
193
    --rec_model_dir=inference/ch_ppocr_mobile_v2.0_rec_infer \
MissPenguin's avatar
MissPenguin committed
194
    --image_dir=../../doc/imgs_words/ch/
zhoujun's avatar
zhoujun committed
195
```
MissPenguin's avatar
MissPenguin committed
196
##### 3. 调用串联:
MissPenguin's avatar
MissPenguin committed
197
198
```shell
# 不使用方向分类器
MissPenguin's avatar
MissPenguin committed
199
./build/ppocr system \
MissPenguin's avatar
MissPenguin committed
200
201
    --det_model_dir=inference/ch_ppocr_mobile_v2.0_det_infer \
    --rec_model_dir=inference/ch_ppocr_mobile_v2.0_rec_infer \
MissPenguin's avatar
MissPenguin committed
202
203
    --image_dir=../../doc/imgs/12.jpg
# 使用方向分类器
MissPenguin's avatar
MissPenguin committed
204
./build/ppocr system \
MissPenguin's avatar
MissPenguin committed
205
206
207
208
    --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 \
MissPenguin's avatar
MissPenguin committed
209
210
211
212
213
    --image_dir=../../doc/imgs/12.jpg
```

更多参数如下:

MissPenguin's avatar
MissPenguin committed
214
215
- 通用参数

MissPenguin's avatar
MissPenguin committed
216
217
218
219
220
221
222
|参数名称|类型|默认参数|意义|
| --- | --- | --- | --- |
|use_gpu|bool|false|是否使用GPU|
|gpu_id|int|0|GPU id,使用GPU时有效|
|gpu_mem|int|4000|申请的GPU内存|
|cpu_math_library_num_threads|int|10|CPU预测时的线程数,在机器核数充足的情况下,该值越大,预测速度越快|
|use_mkldnn|bool|true|是否使用mkldnn库|
MissPenguin's avatar
MissPenguin committed
223
224
225
226
227

- 检测模型相关

|参数名称|类型|默认参数|意义|
| --- | --- | --- | --- |
MissPenguin's avatar
MissPenguin committed
228
229
230
231
232
233
234
|det_model_dir|string|-|检测模型inference model地址|
|max_side_len|int|960|输入图像长宽大于960时,等比例缩放图像,使得图像最长边为960|
|det_db_thresh|float|0.3|用于过滤DB预测的二值化图像,设置为0.-0.3对结果影响不明显|
|det_db_box_thresh|float|0.5|DB后处理过滤box的阈值,如果检测存在漏框情况,可酌情减小|
|det_db_unclip_ratio|float|1.6|表示文本框的紧致程度,越小则文本框更靠近文本|
|use_polygon_score|bool|false|是否使用多边形框计算bbox score,false表示使用矩形框计算。矩形框计算速度更快,多边形框对弯曲文本区域计算更准确。|
|visualize|bool|true|是否对结果进行可视化,为1时,会在当前文件夹下保存文件名为`ocr_vis.png`的预测结果。|
MissPenguin's avatar
MissPenguin committed
235
236
237
238
239

- 方向分类器相关

|参数名称|类型|默认参数|意义|
| --- | --- | --- | --- |
MissPenguin's avatar
MissPenguin committed
240
241
242
|use_angle_cls|bool|false|是否使用方向分类器|
|cls_model_dir|string|-|方向分类器inference model地址|
|cls_thresh|float|0.9|方向分类器的得分阈值|
MissPenguin's avatar
MissPenguin committed
243
244
245
246
247

- 识别模型相关

|参数名称|类型|默认参数|意义|
| --- | --- | --- | --- |
MissPenguin's avatar
MissPenguin committed
248
249
250
251
252
|rec_model_dir|string|-|识别模型inference model地址|
|char_list_file|string|../../ppocr/utils/ppocr_keys_v1.txt|字典文件|


* PaddleOCR也支持多语言的预测,更多支持的语言和模型可以参考[识别文档](../../doc/doc_ch/recognition.md)中的多语言字典与模型部分,如果希望进行多语言预测,只需将修改`char_list_file`(字典文件路径)以及`rec_model_dir`(inference模型路径)字段即可。
zhoujun's avatar
zhoujun committed
253

littletomatodonkey's avatar
littletomatodonkey committed
254
255
256
最终屏幕上会输出检测结果如下。

<div align="center">
littletomatodonkey's avatar
littletomatodonkey committed
257
    <img src="./imgs/cpp_infer_pred_12.png" width="600">
littletomatodonkey's avatar
littletomatodonkey committed
258
</div>
littletomatodonkey's avatar
littletomatodonkey committed
259
260
261
262


### 2.3 注意

littletomatodonkey's avatar
littletomatodonkey committed
263
* 在使用Paddle预测库时,推荐使用2.0.0版本的预测库。