chore: import upstream snapshot with attribution
Build/Publish Develop Docs / deploy (push) Failing after 1s
PaddleOCR Code Style Check / check-code-style (push) Failing after 1s
PaddleOCR PR Tests GPU / detect-changes (push) Failing after 1s
PaddleOCR PR Tests / detect-changes (push) Failing after 1s
PaddleOCR PR Tests GPU / test-pr-gpu (push) Has been cancelled
PaddleOCR PR Tests / test-pr (push) Has been cancelled
PaddleOCR PR Tests GPU / test-pr-gpu-impl (push) Has been cancelled
PaddleOCR PR Tests / test-pr-python (3.13) (push) Has been cancelled
PaddleOCR PR Tests / test-pr-python (3.8) (push) Has been cancelled
PaddleOCR PR Tests / test-pr-python (3.9) (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 11:59:26 +08:00
commit e904b667c6
2490 changed files with 596352 additions and 0 deletions
@@ -0,0 +1,986 @@
# C++ Local Deployment for General OCR Pipeline - Linux
- [1. Environment Preparation](#1-environment-preparation)
- [1.1 Compile OpenCV Library](#11-compile-opencv-library)
- [1.2 Compile Paddle Inference](#12-compile-paddle-inference)
- [2. Getting Started](#2-getting-started)
- [2.1 Compile Prediction Demo](#21-compile-prediction-demo)
- [2.2 Prepare Models](#22-prepare-models)
- [2.3 Run Prediction Demo](#23-run-the-prediction-demo)
- [2.4 C++ API Integration](#24-c-api-integration)
- [3. Extended Features](#3-extended-features)
- [3.1 Multilingual Text Recognition](#31-multilingual-text-recognition)
- [3.2 Visualize Text Recognition Results](#32-visualize-text-recognition-results)
- [4. FAQ](#4-faq)
This section introduces the method for deploying a general OCR pipeline in C++. The general OCR pipeline consists of the following five modules:
1. Document Image Orientation Classification Module (Optional)
2. Text Image Unwarping Module (Optional)
3. Text Line Orientation Classification Module (Optional)
4. Text Detection Module
5. Text Recognition Module
Below, we will explain how to configure the C++ environment and complete the deployment of the general OCR pipeline in a Linux (CPU/GPU) environment.
- Note:
- For specific compilation methods in a Windows environment, please refer to the [Windows Compilation Tutorial](./OCR_windows.en.md). After compilation, the subsequent commands for running the demo are the same as those in Linux.
## 1. Environment Preparation
- **The source code used for compilation and execution in this chapter can be found in the [PaddleOCR/deploy/cpp_infer](https://github.com/PaddlePaddle/PaddleOCR/tree/{{PADDLEOCR_GITHUB_REF}}/deploy/cpp_infer) directory.**
- Linux environment.
- gcc 8.2 (when compiling with the Paddle Inference GPU version, gcc>=11.2)
- cmake 3.18
### 1.1 Compile OpenCV Library
Currently, only OpenCV 4.x versions are supported. Below, we use OpenCV 4.7.0 as an example.
1. Execute the following commands to download the OpenCV source code:
```bash
cd deploy/cpp_infer
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/opencv-4.7.0.tgz
tar -xf opencv-4.7.0.tgz
```
2. Configure and compile the OpenCV library:
- a. In the `tools/build_opencv.sh` script, set `root_path` to the absolute path of the opencv-4.7.0 source code.
- b. Set `install_path`, such as the default `${root_path}/opencv4`. `install_path` will be used as the path to the OpenCV library when compiling the prediction demo later.
- c. After configuration, run the following command to compile OpenCV:
```bash
sh tools/build_opencv.sh
```
### 1.2 Compile Paddle Inference
You can choose to directly download a pre-compiled package or manually compile the source code.
#### 1.2.1 Directly Download Pre-compiled Package (Recommended)
The [Paddle Inference official website](https://www.paddlepaddle.org.cn/inference/v3.0/guides/install/download_lib.html) provides Linux prediction libraries. You can view and select the appropriate pre-compiled package on the website.
After downloading, extract it:
```shell
tar -xvf paddle_inference.tgz
```
This will generate a subfolder `paddle_inference/` in the current directory.
#### 1.2.2 Compile Prediction Library from Source Code
You can choose to compile the prediction library from source code. Compiling from source allows flexible configuration of various features and dependencies to adapt to different hardware and software environments. For detailed steps, please refer to [Source Code Compilation under Linux](https://www.paddlepaddle.org.cn/inference/v3.0/guides/install/compile/source_compile_under_Linux.html).
## 2. Getting Started
### 2.1 Compile Prediction Demo
Before compiling the prediction demo, ensure that you have compiled the OpenCV library and the Paddle Inference prediction library according to sections 1.1 and 1.2.
After modifying the configurations in `tools/build.sh`, execute the following command to compile:
```shell
sh tools/build.sh
```
Detailed descriptions of the relevant configuration parameters are as follows:
<table>
<tr>
<th>Parameter</th>
<th>Description</th>
<th>Default Value</th>
</tr>
<tr>
<td><code>OPENCV_DIR</code></td>
<td>The path where OpenCV is compiled and installed (such as the <code>install_path</code> mentioned when compiling OpenCV above, required).</td>
<td></td>
</tr>
<tr>
<td><code>LIB_DIR</code></td>
<td>The path to the downloaded <code>Paddle Inference</code> pre-compiled package or the manually compiled Paddle Inference library path (such as the <code>build/paddle_inference_install_dir</code> folder), required.</td>
<td></td>
</tr>
<tr>
<td><code>CUDA_LIB_DIR</code></td>
<td>The path to the CUDA library files, usually <code>/usr/local/cuda/lib64</code>. This parameter needs to be set when the Paddle Inference library is the GPU version and <code>-DWITH_GPU=ON</code> is set.</td>
<td></td>
</tr>
<tr>
<td><code>CUDNN_LIB_DIR</code></td>
<td>The path to the cuDNN library files, usually <code>/usr/lib/x86_64-linux-gnu/</code>. This parameter needs to be set when the Paddle Inference library is the GPU version and <code>-DWITH_GPU=ON</code> is set.</td>
<td></td>
</tr>
<tr>
<td><code>WITH_GPU</code></td>
<td>When set to ON, you can compile the GPU version demo, which requires the Paddle Inference library to be the GPU version.</td>
<td>OFF</td>
</tr>
</table>
**Note: The above paths need to be absolute paths.**
### 2.2 Prepare Models
You can directly download the inference models provided by PaddleOCR:
<details>
<summary><b>Document Image Orientation Classification Module (Optional):</b></summary>
<table>
<thead>
<tr>
<th>Model</th><th>Model Download Link</th>
<th>Top-1 Acc (%)</th>
<th>Model Storage Size (MB)</th>
<th>Introduction</th>
</tr>
</thead>
<tbody>
<tr>
<td>PP-LCNet_x1_0_doc_ori</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-LCNet_x1_0_doc_ori_infer.tar">Inference Model</a></td>
<td>99.06</td>
<td>7</td>
<td>A document image classification model based on PP-LCNet_x1_0, with four categories: 0 degrees, 90 degrees, 180 degrees, and 270 degrees.</td>
</tr>
</tbody>
</table>
</details>
<details>
<summary><b>Text Image Unwrapping Module (Optional):</b></summary>
<table>
<thead>
<tr>
<th>Model</th><th>Model Download Link</th>
<th>CER</th>
<th>Model Storage Size (MB)</th>
<th>Introduction</th>
</tr>
</thead>
<tbody>
<tr>
<td>UVDoc</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/UVDoc_infer.tar">Inference Model</a></td>
<td>0.179</td>
<td>30.3</td>
<td>A high-precision text image unwarping model.</td>
</tr>
</tbody>
</table>
</details>
<details>
<summary><b>Text Line Orientation Classification Module (Optional):</b></summary>
<table>
<thead>
<tr>
<th>Model</th>
<th>Model Download Link</th>
<th>Top-1 Acc (%)</th>
<th>Model Storage Size (MB)</th>
<th>Introduction</th>
</tr>
</thead>
<tbody>
<td>PP-LCNet_x1_0_textline_ori (Default)</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-LCNet_x1_0_textline_ori_infer.tar">Inference Model</a></td>
<td>99.42</td>
<td>6.5</td>
<td>A text line classification model based on PP-LCNet_x1_0, with two categories: 0 degrees and 180 degrees.</td>
</tr>
<tr>
<td>PP-LCNet_x0_25_textline_ori</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-LCNet_x0_25_textline_ori_infer.tar">Inference Model</a></td>
<td>98.85</td>
<td>0.96</td>
<td>A text line classification model based on PP-LCNet_x0_25, with two categories: 0 degrees and 180 degrees.</td>
</tr>
</tbody>
</table>
</details>
<details>
<summary><b>Text Detection Module:</b></summary>
<table>
<thead>
<tr>
<th>Model</th><th>Model Download Link</th>
<th>Detection Hmean (%)</th>
<th>Model Storage Size (MB)</th>
<th>Introduction</th>
</tr>
</thead>
<tbody>
<tr>
<td>PP-OCRv5_server_det (Default)</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_server_det_infer.tar">Inference Model</a></td>
<td>83.8</td>
<td>84.3</td>
<td>A server-side text detection model for PP-OCRv5, with higher precision, suitable for deployment on servers with better performance.</td>
</tr>
<tr>
<td>PP-OCRv5_mobile_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_mobile_det_infer.tar">Inference Model</a></td>
<td>79.0</td>
<td>4.7</td>
<td>A mobile-side text detection model for PP-OCRv5, with higher efficiency, suitable for deployment on edge devices.</td>
</tr>
<tr>
<td>PP-OCRv4_server_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv4_server_det_infer.tar">Inference Model</a></td>
<td>69.2</td>
<td>109</td>
<td>A server-side text detection model for PP-OCRv4, with higher precision, suitable for deployment on servers with better performance.</td>
</tr>
<tr>
<td>PP-OCRv4_mobile_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv4_mobile_det_infer.tar">Inference Model</a></td>
<td>63.8</td>
<td>4.7</td>
<td>A mobile-side text detection model for PP-OCRv4, with higher efficiency, suitable for deployment on edge devices.</td>
</tr>
</tbody>
</table>
</details>
<details>
<summary><b>Text Recognition Module:</b></summary>
<table>
<tr>
<th>Model</th><th>Model Download Link</th>
<th>Recognition Avg Accuracy (%)</th>
<th>Model Storage Size (MB)</th>
<th>Introduction</th>
</tr>
<tr>
<td>PP-OCRv5_server_rec (Default)</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/\
PP-OCRv5_server_rec_infer.tar">Inference Model</a></td>
<td>86.38</td>
<td>81</td>
<td rowspan="2">PP-OCRv5_rec is a new-generation text recognition model. It aims to efficiently and accurately support the recognition of four major languages: simplified Chinese, traditional Chinese, English, and Japanese, as well as complex text scenarios such as handwriting, vertical text, pinyin, and rare characters, with a single model. While maintaining recognition effectiveness, it balances inference speed and model robustness, providing efficient and accurate technical support for document understanding in various scenarios.</td>
</tr>
<tr>
<td>PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/\
PP-OCRv5_mobile_rec_infer.tar">Inference Model</a></td>
<td>81.29</td>
<td>16</td>
</tr>
<tr>
<td>PP-OCRv4_server_rec_doc</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/\
PP-OCRv4_server_rec_doc_infer.tar">Inference Model</a></td>
<td>86.58</td>
<td>182</td>
<td>PP-OCRv4_server_rec_doc is trained on a mixed dataset of more Chinese document data and PP-OCR training data, based on PP-OCRv4_server_rec. It enhances the recognition ability of some traditional Chinese characters, Japanese characters, and special characters, supporting over 15,000 characters. In addition to improving the text recognition ability related to documents, it also enhances the general text recognition ability.</td>
</tr>
<tr>
<td>PP-OCRv4_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv4_mobile_rec_infer.tar">Inference Model</a></td>
<td>78.74</td>
<td>10.5</td>
<td>A lightweight recognition model for PP-OCRv4, with high inference efficiency, suitable for deployment on various hardware devices including edge devices.</td>
</tr>
<tr>
<td>PP-OCRv4_server_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv4_server_rec_infer.tar">Inference Model</a></td>
<td>85.19</td>
<td>173</td>
<td>A server-side model for PP-OCRv4, with high inference precision, suitable for deployment on various servers.</td>
</tr>
</tbody>
</table>
</details>
You can also refer to the model export sections of each module, such as [Text Detection Module - Model Export](../../../module_usage/text_detection.en.md#44-model-export), to export the trained models as inference models.
The directory structure of the models is generally as follows:
```
PP-OCRv5_mobile_det
|inference.pdiparams (Model weights file)
|inference.json (Model structure file, in JSON format)
|inference.yml (Model configuration file, in YAML format)
```
### 2.3 Run the Prediction Demo
Before using the General OCR Pipeline C++ locally, please first successfully compile the prediction demo. After compilation, you can experience it via the command line or call the API for secondary development and then recompile to generate the application.
**Please note that if you encounter issues such as the program becoming unresponsive, abnormal program exits, memory resource exhaustion, or extremely slow inference speeds during execution, try adjusting the configuration by referring to the documentation, for example, by disabling unused features or using a lighter model.**
This demo supports both system pipeline calls and individual module calls. Before running the following code, please download the [sample image](https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_002.png) locally:
Running Method:
```shell
./build/ppocr <pipeline_or_module> [--param1] [--param2] [...]
```
Common parameters are as follows:
<li>Input and Output Related</li>
<table>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
<th>Type</th>
<th>Default Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>input</code></td>
<td>The local image to be predicted, required. Only supports images in <code>jpg</code>, <code>png</code>, <code>jpeg</code>, and <code>bmp</code> formats.</td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>save_path</code></td>
<td>Specifies the path where the inference result files will be saved. Both the JSON file and the predicted result image will be saved under this path.</td>
<td><code>str</code></td>
<td><code>./output</code></td>
</tr>
</tbody>
</table>
<details><summary><b>Click to expand for detailed descriptions of more parameters</b></summary>
<li>General Parameters</li>
<table>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
<th>Type</th>
<th>Default Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>device</code></td>
<td>The device used for inference. Supports specifying a specific card number:
<ul>
<li><b>CPU</b>: For example, <code>cpu</code> indicates using the CPU for inference;</li>
<li><b>GPU</b>: For example, <code>gpu:0</code> indicates using the first GPU for inference;</li>
</ul>If not set, it will use the default value initialized by the pipeline. During initialization, if <code>-DWITH_GPU=ON</code> is added during compilation, it will prioritize using the local GPU device 0; otherwise, it will use the CPU device.
</td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>precision</code></td>
<td>The computation precision, such as <code>fp32</code>, <code>fp16</code>.</td>
<td><code>str</code></td>
<td><code>fp32</code></td>
</tr>
<tr>
<td><code>enable_mkldnn</code></td>
<td>Whether to enable MKL-DNN for accelerated inference. If MKL-DNN is not available or the model does not support acceleration via MKL-DNN, acceleration will not be used even if this flag is set.</td>
<td><code>bool</code></td>
<td><code>true</code></td>
</tr>
<tr>
<td><code>mkldnn_cache_capacity</code></td>
<td>
MKL-DNN cache capacity.
</td>
<td><code>int</code></td>
<td><code>10</code></td>
</tr>
<tr>
<td><code>cpu_threads</code></td>
<td>The number of threads for the PaddleInference CPU acceleration library.</td>
<td><code>int</code></td>
<td><code>8</code></td>
</tr>
<tr>
<td><code>paddlex_config</code></td>
<td>The path to the PaddleX pipeline configuration file.</td>
<td><code>str</code></td>
<td></td>
</tr>
</tbody>
</table>
<li>Module Switches</li>
<table>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
<th>Type</th>
<th>Default Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>use_doc_orientation_classify</code></td>
<td>Whether to load and use the document orientation classification module. If not set, it will use the default value initialized by the pipeline, which is <code>true</code> by default.</td>
<td><code>bool</code></td>
<td><code>true</code></td>
</tr>
<tr>
<td><code>use_doc_unwarping</code></td>
<td>Whether to load and use the text image correction module. If not set, it will use the default value initialized by the pipeline, which is <code>true</code> by default.</td>
<td><code>bool</code></td>
<td><code>true</code></td>
</tr>
<tr>
<td><code>use_textline_orientation</code></td>
<td>Whether to load and use the text line orientation module. If not set, it will use the default value initialized by the pipeline, which is <code>true</code> by default.</td>
<td><code>bool</code></td>
<td><code>true</code></td>
</tr>
</tbody>
</table>
<li>Detection Model Related</li>
<table>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
<th>Type</th>
<th>Default Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>text_detection_model_name</code></td>
<td>The name of the text detection model. If not set, it will use the default model of the pipeline. When the model name passed in via the text detection model path is inconsistent with the configured name of the pipeline's default text recognition model, you need to specify the name of the passed-in model.</td>
<td><code>str</code></td>
<td><code>PP-OCRv5_server_det</code></td>
</tr>
<tr>
<td><code>text_detection_model_dir</code></td>
<td>The directory path of the text detection model, required.</td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>text_det_limit_side_len</code></td>
<td>The image side length limit for text detection.
Any integer greater than <code>0</code>. If not set, it will use the default value initialized by the pipeline, which is <code>64</code> by default.
</td>
<td><code>int</code></td>
<td><code>64</code></td>
</tr>
<tr>
<td><code>text_det_limit_type</code></td>
<td>The side length limit type for text detection. Supports <code>min</code> and <code>max</code>, where <code>min</code> means ensuring the shortest side of the image is not less than <code>det_limit_side_len</code>, and <code>max</code> means ensuring the longest side of the image is not greater than <code>limit_side_len</code>. If not set, it will use the default value initialized by the pipeline, which is <code>min</code> by default.
</td>
<td><code>str</code></td>
<td><code>min</code></td>
</tr>
<tr>
<td><code>text_det_thresh</code></td>
<td>The pixel threshold for text detection. Only pixels with scores greater than this threshold in the output probability map will be considered as text pixels.
Any floating-point number greater than <code>0</code>. If not set, it will use the default value initialized by the pipeline.
</td>
<td><code>float</code></td>
<td><code>0.3</code></td>
</tr>
<tr>
<td><code>text_det_box_thresh</code></td>
<td>The bounding box threshold for text detection. When the average score of all pixels within the detected bounding box is greater than this threshold, the result will be considered as a text region.
Any floating-point number greater than <code>0</code>. If not set, it will use the default value initialized by the pipeline (default is <code>0.6</code>).
</td>
<td><code>float</code></td>
<td><code>0.6</code></td>
</tr>
<tr>
<td><code>text_det_unclip_ratio</code></td>
<td>The expansion coefficient for text detection. This method is used to expand the text region. The larger the value, the greater the expanded area. Any floating-point number greater than <code>0</code>. If not set, it will use the default value initialized by the pipeline.
</td>
<td><code>float</code></td>
<td><code>1.5</code></td>
</tr>
<tr>
<td><code>text_det_input_shape</code></td>
<td>The input shape for text detection. You can set 3 values representing C, H, W.</td>
<td><code>str</code></td>
<td>""</td>
</tr>
</tbody>
</table>
<li>Orientation Classifier Related</li>
<table>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
<th>Type</th>
<th>Default Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>doc_orientation_classify_model_name</code></td>
<td>The name of the document orientation classification model. If not set, it will use the default model of the pipeline. When the name of the passed-in document orientation classification model is inconsistent with the configured name of the pipeline's default model, you need to specify the name of the passed-in model.</td>
<td><code>str</code></td>
<td><code>PP-LCNet_x1_0_doc_ori</code></td>
</tr>
<tr>
<td><code>doc_orientation_classify_model_dir</code></td>
<td>The directory path of the document orientation classification model. It can be omitted when setting <code>use_doc_orientation_classify = false</code>.</td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>textline_orientation_model_name</code></td>
<td>The name of the text line orientation classification model. If not set, it will use the default model of the pipeline. When the name of the passed-in text line orientation classification model is inconsistent with the configured name of the pipeline's default model, you need to specify the name of the passed-in model.</td>
<td><code>str</code></td>
<td><code>PP-LCNet_x1_0_textline_ori</code></td>
</tr>
<tr>
<td><code>textline_orientation_model_dir</code></td>
<td>The directory path of the text line orientation classification model. It can be omitted when setting <code>use_textline_orientation = false</code>.</td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>textline_orientation_batch_size</code></td>
<td>The batch size for the text line orientation model. If not set, it will use the default model of the pipeline.</td>
<td><code>int</code></td>
<td><code>6</code></td>
</tr>
</tbody>
</table>
<li>Text Recognition Model Related</li>
<table>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
<th>Type</th>
<th>Default Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>text_recognition_model_name</code></td>
<td>The name of the text recognition model. If not set, it will use the default model of the pipeline. When the name of the passed-in text recognition model path is inconsistent with the configured name of the pipeline's default text recognition model, you need to specify the name of the passed-in model.</td>
<td><code>str</code></td>
<td><code>PP-OCRv5_server_rec</code></td>
</tr>
<tr>
<td><code>text_recognition_model_dir</code></td>
<td>The directory path of the text recognition model, required.</td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>text_recognition_batch_size</code></td>
<td>The batch size for the text recognition model. If not set, it will use the default value of the pipeline.</td>
<td><code>int</code></td>
<td><code>6</code></td>
</tr>
<tr>
<td><code>text_rec_score_thresh</code></td>
<td>The text recognition threshold. Text results with scores greater than this threshold will be retained. Any floating-point number greater than <code>0</code>.</td>
<td><code>float</code></td>
<td><code>0.0</code></td>
</tr>
<tr>
<td><code>text_rec_input_shape</code></td>
<td>The input shape for text recognition. You can set 3 values representing C, H, W.</td>
<td><code>str</code></td>
<td>""</td>
</tr>
</tbody>
</table>
</details>
#### 2.3.1 Example of System Pipeline Call
This section provides an example of a system pipeline call. Please refer to Section 2.1 to prepare the models. Assume the model directory structure is as follows:
```
models
|--PP-LCNet_x1_0_doc_ori_infer
|--UVDoc_infer
|--PP-LCNet_x1_0_textline_ori_infer
|--PP-OCRv5_server_det_infer
|--PP-OCRv5_server_rec_infer
```
=== "Full Pipeline Serialization"
```bash
./build/ppocr ocr --input ./general_ocr_002.png --save_path ./output/ \
--doc_orientation_classify_model_dir models/PP-LCNet_x1_0_doc_ori_infer \
--doc_unwarping_model_dir models/UVDoc_infer \
--textline_orientation_model_dir models/PP-LCNet_x1_0_textline_ori_infer \
--text_detection_model_dir models/PP-OCRv5_server_det_infer \
--text_recognition_model_dir models/PP-OCRv5_server_rec_infer \
--device cpu
```
Example Output (If `save_path` is specified, a standard JSON prediction result file and prediction result image will be generated under this path):
```bash
{
"input_path": "./general_ocr_002.png",
"doc_preprocessor_res": {
"model_settings": {"use_doc_unwarping": true, "use_doc_orientation_classify": true},
"angle": 0
},
...,
"dt_polys": [[[132, 6], [355, 6], [355, 64], [132, 64]],
[[424, 9], [689, 9], [689, 59], [424, 59]],
...,
[[664, 8], [867, 4], [868, 55], [665, 60]],
[[31, 99], [173, 99], [173, 126], [31, 126]]],
...,
"rec_texts": ["登机牌", "BOARDING", "GPASS", ..., ],
...,
}
```
=== "Text Detection + Textline Orientation Classification + Text Recognition"
```bash
./build/ppocr ocr --input ./general_ocr_002.png --save_path ./output/ \
--doc_orientation_classify_model_dir models/PP-LCNet_x1_0_doc_ori_infer \
--doc_unwarping_model_dir models/UVDoc_infer \
--textline_orientation_model_dir models/PP-LCNet_x1_0_textline_ori_infer \
--text_detection_model_dir models/PP-OCRv5_server_det_infer \
--text_recognition_model_dir models/PP-OCRv5_server_rec_infer \
--use_doc_orientation_classify False \
--use_doc_unwarping False \
--device cpu
```
Example Output (If `save_path` is specified, a standard JSON prediction result file and prediction result image will be generated under this path):
```bash
{
"input_path": "./general_ocr_002.png",
...,
"dt_polys": [[[0, 1], [334, 1], [334, 34], [0, 34]],
[[151, 21], [357, 16], [358, 72], [152, 76]],
...,
[[675, 97], [740, 97], [740, 121], [675, 121]],
[[751, 97], [836, 94], [837, 115], [752, 119]],
...,
"rec_texts": ["净小8866-", "登机牌", "BOARDING", "GPASS", ..., ],
...,
}
```
=== "Text Detection + Text Recognition"
```bash
./build/ppocr ocr --input ./general_ocr_002.png --save_path ./output/ \
--doc_orientation_classify_model_dir models/PP-LCNet_x1_0_doc_ori_infer \
--doc_unwarping_model_dir models/UVDoc_infer \
--textline_orientation_model_dir models/PP-LCNet_x1_0_textline_ori_infer \
--text_detection_model_dir models/PP-OCRv5_server_det_infer \
--text_recognition_model_dir models/PP-OCRv5_server_rec_infer \
--use_doc_orientation_classify False \
--use_doc_unwarping False \
--use_textline_orientation False \
--device cpu
```
Example Output (If `save_path` is specified, a standard JSON prediction result file and prediction result image will be generated under this path):
```bash
{
"input_path": "./general_ocr_002.png",
...,
"dt_polys": [[[0, 1], [334, 1], [334, 34], [0, 34]],
[[151, 21], [357, 16], [358, 72], [152, 76]],
...,
[[61, 109], [194, 106], [194, 132], [61, 135]],
[[80, 138], [219, 136], [219, 162], [80, 164]],
...,
"rec_texts": ["www.997788.com中国收藏热线","登机牌", "BOARDING", "GPASS", ..., ],
...,
}
```
The above sample code will generate the following text detection result image:
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/ocr_res.png"/>
If you want to view the text recognition result image, please refer to the **Visualizing Text Recognition Results** section later.
#### 2.3.2 Example of Single Module Call
=== "Document Image Orientation Classification"
```bash
./build/ppocr doc_img_orientation_classification --input ./general_ocr_002.png --save_path ./output/ \
--doc_orientation_classify_model_dir models/PP-LCNet_x1_0_doc_ori_infer \
--device cpu
```
Example Output (if `save_path` is specified, standard JSON prediction result files and prediction result images will be generated at this path):
```bash
{
"res": {
"input_path": {./general_ocr_002.png},
"class_ids": {0},
"scores": {0.926328},
"label_names": {0},
}
```
=== "Document Image Unwarping"
```bash
./build/ppocr text_image_unwarping --input ./general_ocr_002.png --save_path ./output/ \
--doc_unwarping_model_dir models/UVDoc_infer \
--device cpu
```
Example Output (if `save_path` is specified, standard JSON prediction result files and prediction result images will be generated at this path):
```bash
{
"res": {
"input_path": {./general_ocr_002.png},
"doctr_img": {...}
}
```
=== "Text Line Orientation Classification"
```bash
./build/ppocr textline_orientation_classification --input ./general_ocr_002.png --save_path ./output/ \
--textline_orientation_model_dir models/PP-LCNet_x1_0_textline_ori_infer \
--device cpu
```
Example Output (if `save_path` is specified, standard JSON prediction result files and prediction result images will be generated at this path):
```bash
{
"res": {
"input_path": {./general_ocr_002.png},
"class_ids": {0},
"scores": {0.719926},
"label_names": {0_degree},
}
```
=== "Text Detection"
```bash
./build/ppocr text_detection --input ./general_ocr_002.png --save_path ./output/ \
--text_detection_model_dir models/PP-OCRv5_server_det_infer \
--device cpu
```
Example Output (if `save_path` is specified, standard JSON prediction result files and prediction result images will be generated at this path):
```bash
{
"res": {
"input_path": {./general_ocr_002.png },
"dt_polys": [
[[98, 456], [834, 441], [834, 466], [98, 480]],
[[344, 347], [662, 343], [662, 366], [344, 371]],
[[66, 341], [165, 337], [167, 363], [67, 367]],
...,
[[0, 1], [331, 0], [332, 32], [0, 34]],
]},
"dt_scores": [
0.812284, 0.8082, 0.848293, ...,
]
}
}
```
=== "Text Recognition"
```bash
./build/ppocr text_recognition --input ./general_ocr_rec_001.png --save_path ./output/ \
--text_recognition_model_dir models/PP-OCRv5_server_rec_infer \
--device cpu
```
Example Output (if `save_path` is specified, standard JSON prediction result files and prediction result images will be generated at this path):
```bash
{
"res": {
"input_path": {./general_ocr_rec_001.png },
"rec_text": {绿洲仕格维花园公寓 }
"rec_score": {0.982409 }
}
```
### 2.4 C++ API Integration
The command-line interface is for quickly experiencing and viewing the results. Generally, in projects, integration through code is often required. You can achieve rapid inference in production lines with just a few lines of code. The inference code is as follows:
Since the general OCR production line has many configuration parameters, a struct is used for parameter passing during instantiation. The naming rule for the struct is `pipeline_class_name + Params`. For example, the corresponding class name for the general OCR production line is `PaddleOCR`, and the struct is `PaddleOCRParams`.
```c++
#include "src/api/pipelines/ocr.h"
int main(){
PaddleOCRParams params;
params.doc_orientation_classify_model_dir = "models/PP-LCNet_x1_0_doc_ori_infer"; // Path to the document orientation classification model.
params.doc_unwarping_model_dir = "models/UVDoc_infer"; // Path to the text image unwarping model.
params.textline_orientation_model_dir = "models/PP-LCNet_x1_0_textline_ori_infer"; // Path to the text line orientation classification model.
params.text_detection_model_dir = "models/PP-OCRv5_server_det_infer"; // Path to the text detection model.
params.text_recognition_model_dir = "models/PP-OCRv5_server_rec_infer"; // Path to the text recognition model.
// params.device = "gpu"; // Use GPU for inference. Ensure that the -DWITH_GPU=ON option is added during compilation; otherwise, CPU will be used.
// params.use_doc_orientation_classify = false; // Do not use the document orientation classification model.
// params.use_doc_unwarping = false; // Do not use the text image unwarping model.
// params.use_textline_orientation = false; // Do not use the text line orientation classification model.
// params.text_recognition_model_name = "PP-OCRv5_server_rec" // Use the PP-OCRv5_server_rec model for recognition.
// params.vis_font_dir = "your_vis_font_dir"; // When the -DUSE_FREETYPE=ON option is added during compilation, the corresponding ttf font file path must be provided.
auto infer = PaddleOCR(params);
auto outputs = infer.Predict("./general_ocr_002.png");
for (auto& output : outputs) {
output->Print();
output->SaveToImg("./output/");
output->SaveToJson("./output/");
}
}
```
## 3. Extended Features
### 3.1 Multilingual Text Recognition
PP-OCRv5 also provides multilingual text recognition capabilities covering 39 languages, including Korean, Spanish, French, Portuguese, German, Italian, Russian, Thai, Greek, etc. The specific supported languages are as follows:
<table>
<thead>
<tr>
<th>Model</th>
<th>Link</th>
<th>Supported Languages</th>
</tr>
</thead>
<tbody>
<tr>
<td>PP-OCRv5_server_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_server_rec_infer.tar">Inference Model</a></td>
<td>Simplified Chinese, Traditional Chinese, English, Japanese</td>
</tr>
<tr>
<td>PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_mobile_rec_infer.tar">Inference Model</a></td>
<td>Simplified Chinese, Traditional Chinese, English, Japanese</td>
</tr>
<tr>
<td>korean_PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/korean_PP-OCRv5_mobile_rec_infer.tar">Inference Model</a></td>
<td>Korean, English</td>
</tr>
<tr>
<td>latin_PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/latin_PP-OCRv5_mobile_rec_infer.tar">Inference Model</a></td>
<td>English, French, German, Afrikaans, Italian, Spanish, Bosnian, Portuguese, Czech, Welsh, Danish, Estonian, Irish, Croatian, Uzbek, Hungarian, Serbian (latin), Indonesian, Occitan, Icelandic, Lithuanian, Maori, Malay, Dutch, Norwegian, Polish, Slovak, Slovenian, Albanian, Swedish, Swahili, Tagalog, Turkish, Latin</td>
</tr>
<tr>
<td>eslav_PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/eslav_PP-OCRv5_mobile_rec_infer.tar">Inference Model</a></td>
<td>Russian, Belarusian, Ukrainian, English</td>
</tr>
<tr>
<td>th_PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/th_PP-OCRv5_mobile_rec_infer.tar">Inference Model</a></td>
<td>Thai, English</td>
</tr>
<tr>
<td>el_PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/el_PP-OCRv5_mobile_rec_infer.tar">Inference Model</a></td>
<td>Greek, English</td>
</tr>
<tr>
<td>en_PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/en_PP-OCRv5_mobile_rec_infer.tar">Inference Model</a></td>
<td>English</td>
</tr>
</tbody>
</table>
Simply pass the corresponding recognition model when using the pipeline or module. For example, to recognize French text using the text recognition module:
```
./build/ppocr text_recognition \
--input ./french.png \
--text_recognition_model_name latin_PP-OCRv5_mobile_rec \
--text_recognition_model_dir latin_PP-OCRv5_mobile_rec_infer \
--save_path ./output/
```
For more detailed information, refer to the [Introduction to PP-OCRv5 Multilingual Text Recognition](../../../algorithm/PP-OCRv5/PP-OCRv5_multi_languages.en.md).
### 3.2 Visualize Text Recognition Results
We use the FreeType module from the opencv_contrib 4.x version for font rendering. If you want to visualize text recognition results, you need to download the source code of both OpenCV and opencv_contrib and compile OpenCV with the FreeType module included. Make sure that the versions of the two are consistent when downloading the source code. The following instructions use opencv-4.7.0 and opencv_contrib-4.7.0 as examples:
```bash
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/opencv-4.7.0.tgz
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/opencv_contrib-4.7.0.tgz
tar -xf opencv-4.7.0.tgz
tar -xf opencv_contrib-4.7.0.tgz
```
Install FreeType dependencies:
```bash
sudo apt-get update
sudo apt-get install libfreetype6-dev libharfbuzz-dev
```
Follow these steps to compile OpenCV with FreeType support:
- a. Add the following three options to the `tools/build_opencv.sh` script:
- `-DOPENCV_EXTRA_MODULES_PATH=your_opencv_contrib-4.7.0/modules/`
- `-DBUILD_opencv_freetype=ON`
- `-DWITH_FREETYPE=ON`
- b. In `tools/build_opencv.sh`, set `root_path` to the absolute path of your opencv-4.7.0 source code.
- c. In `tools/build_opencv.sh`, set `install_path` (default: `${root_path}/opencv4`). This path will be used as the OpenCV library path when compiling the prediction demo later.
- d. After configuration, run the following command to compile OpenCV:
```bash
sh tools/build_opencv.sh
```
- e. In `tools/build.sh`, add `-DUSE_FREETYPE=ON` to enable text rendering and specify the ttf font file path with `--vis_font_dir your_ttf_path`. Then compile the prediction demo with:
```bash
sh tools/build.sh
```
After compiling and running the prediction demo, you should see visualized text recognition results like this:
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/ocr_res_with_freetype.png"/>
## 4. FAQ
1. If you encounter the error `Model name mismatch, please input the correct model dir. model dir is xxx, but model name is xxx`, it means the specified model name doesn't match the provided model. For example, if the text recognition model expects `PP-OCRv5_server_rec` but you provided `PP-OCRv5_mobile_rec`.
Solution: Adjust either the model name or the provided model. In the example above, you can specify `--text_recognition_model_name PP-OCRv5_mobile_rec` to match the provided model.
2. If you see garbled text in the Windows console, it may be due to the console's default character encoding (GBK). Change it to UTF-8 encoding.
@@ -0,0 +1,981 @@
# 通用 OCR 产线 C++ 部署 - Linux
- [1. 环境准备](#1)
- [1.1 编译 OpenCV 库](#11-opencv)
- [1.2 编译 Paddle Inference](#12-paddle-inference)
- [2. 开始运行](#2)
- [2.1 编译预测 demo](#21-demo)
- [2.2 准备模型](#22)
- [2.3 运行预测 demo](#23-demo)
- [2.4 C++ API 集成](#24-c-api)
- [3. 拓展功能](#3)
- [3.1 多语种文字识别](#31)
- [3.2 可视化文本识别结果](#32)
- [4. FAQ](#4-faq)
本章节介绍通用 OCR 产线 C++ 部署方法。通用 OCR 产线由以下5个模块组成:
1. 文档图像方向分类模块(可选)
2. 文本图像矫正模块 (可选)
3. 文本行方向分类模块(可选)
4. 文本检测模块
5. 文本识别模块
下面将介绍如何在 Linux (CPU/GPU) 环境下配置 C++ 环境并完成通用 OCR 产线部署。
- 备注
- Windows 环境具体编译方法请参考 [Windows 编译教程](./OCR_windows.md),在编译完成后,后续运行 demo 的指令与 Linux 一致。
## 1. 环境准备
- **本章节编译运行时用到的源代码位于 [PaddleOCR/deploy/cpp_infer](https://github.com/PaddlePaddle/PaddleOCR/tree/{{PADDLEOCR_GITHUB_REF}}/deploy/cpp_infer) 目录下。**
- Linux 环境。
- gcc 8.2(编译使用 Paddle Inference GPU 版本时,gcc>=11.2
- cmake 3.18
### 1.1 编译 OpenCV 库
目前仅支持 OpenCV 4.x 版本。下面以 OpenCV 4.7.0 为例。
1. 执行如下命令下载 OpenCV 源码:
```bash
cd deploy/cpp_infer
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/opencv-4.7.0.tgz
tar -xf opencv-4.7.0.tgz
```
2. 配置并编译 OpenCV 库:
- a. 在 `tools/build_opencv.sh` 脚本中,将 `root_path` 设置为 opencv-4.7.0 源码的绝对路径。
- b. 设置 `install_path`,如默认的 `${root_path}/opencv4``install_path` 在后续编译预测 demo 时,将作为 OpenCV 库的路径使用。
- c. 配置完成后,运行以下命令进行 OpenCV 的编译:
```bash
sh tools/build_opencv.sh
```
### 1.2 编译 Paddle Inference
可以选择直接下载预编译包或者手动编译源码。
#### 1.2.1 直接下载预编译包(推荐)
[Paddle Inference 官网](https://www.paddlepaddle.org.cn/inference/v3.0/guides/install/download_lib.html) 上提供了 Linux 预测库,可以在官网查看并选择合适的预编译包。
下载之后解压:
```shell
tar -xvf paddle_inference.tgz
```
最终会在当前的文件夹中生成 `paddle_inference/` 的子文件夹。
#### 1.2.2 源码编译预测库
可以选择通过源码自行编译预测库,源码编译可灵活配置各类功能和依赖,以适应不同的硬件和软件环境。详细步骤请参考 [Linux 下源码编译](https://www.paddlepaddle.org.cn/inference/v3.0/guides/install/compile/source_compile_under_Linux.html)。
## 2. 开始运行
### 2.1 编译预测 demo
在编译预测demo前,请确保您已经按照 1.1 和 1.2 节编译好 OpenCV 库和 Paddle Inference 预测库。
修改 tools/build.sh 中的配置后,执行以下命令进行编译:
```shell
sh tools/build.sh
```
相关配置参数详细介绍如下:
<table>
<tr>
<th>参数</th>
<th>说明</th>
<th>默认值</th>
</tr>
<tr>
<td><code>OPENCV_DIR</code></td>
<td>OpenCV编译安装的路径(如上述编译 OpenCV 时的 <code>install_path</code> ,必填。</td>
<td></td>
</tr>
<tr>
<td><code>LIB_DIR</code></td>
<td>下载的 <code>Paddle Inference</code> 的预编译包或手动编译生成的Paddle Inference库路径(如 <code>build/paddle_inference_install_dir</code> 文件夹),必填。</td>
<td></td>
</tr>
<tr>
<td><code>CUDA_LIB_DIR</code></td>
<td>CUDA库文件路径,通常为<code>/usr/local/cuda/lib64</code>。当Paddle Inference库为GPU版本且设置 <code>-DWITH_GPU=ON</code> 时需要设置该参数。</td>
<td></td>
</tr>
<tr>
<td><code>CUDNN_LIB_DIR</code></td>
<td>cuDNN 库文件路径,通常为 <code>/usr/lib/x86_64-linux-gnu/</code> 。当 Paddle Inference 库为 GPU 版本且设置 <code>-DWITH_GPU=ON</code> 时需要设置该参数。</td>
<td></td>
</tr>
<tr>
<td><code>WITH_GPU</code></td>
<td>当设置为 ON 时可以进行 GPU 版本 demo 的编译,要求 Paddle Inference 库为 GPU 版本。</td>
<td>OFF</td>
</tr>
</table>
**注意:以上路径需要填绝对路径。**
### 2.2 准备模型
可以直接下载 PaddleOCR 提供的推理模型:
<details>
<summary><b>文档图像方向分类模块(可选):</b></summary>
<table>
<thead>
<tr>
<th>模型</th><th>模型下载链接</th>
<th>Top-1 Acc%</th>
<th>模型存储大小(MB</th>
<th>介绍</th>
</tr>
</thead>
<tbody>
<tr>
<td>PP-LCNet_x1_0_doc_ori</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-LCNet_x1_0_doc_ori_infer.tar">推理模型</a></td>
<td>99.06</td>
<td>7</td>
<td>基于 PP-LCNet_x1_0 的文档图像分类模型,含有四个类别,即0度,90度,180度,270度</td>
</tr>
</tbody>
</table>
</details>
<details>
<summary><b>文本图像矫正模块(可选):</b></summary>
<table>
<thead>
<tr>
<th>模型</th><th>模型下载链接</th>
<th>CER </th>
<th>模型存储大小(MB</th>
<th>介绍</th>
</tr>
</thead>
<tbody>
<tr>
<td>UVDoc</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/UVDoc_infer.tar">推理模型</a></td>
<td>0.179</td>
<td>30.3</td>
<td>高精度文本图像矫正模型</td>
</tr>
</tbody>
</table>
</details>
<details>
<summary><b>文本行方向分类模块(可选):</b></summary>
<table>
<thead>
<tr>
<th>模型</th>
<th>模型下载链接</th>
<th>Top-1 Acc%</th>
<th>模型存储大小(MB</th>
<th>介绍</th>
</tr>
</thead>
<tbody>
<td>PP-LCNet_x1_0_textline_ori (默认)</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-LCNet_x1_0_textline_ori_infer.tar">推理模型</a></td>
<td>99.42</td>
<td>6.5</td>
<td>基于 PP-LCNet_x1_0 的文本行分类模型,含有两个类别,即0度,180度</td>
</tr>
<tr>
<td>PP-LCNet_x0_25_textline_ori</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-LCNet_x0_25_textline_ori_infer.tar">推理模型</a></td>
<td>98.85</td>
<td>0.96</td>
<td>基于 PP-LCNet_x0_25 的文本行分类模型,含有两个类别,即0度,180度</td>
</tr>
</tbody>
</table>
</details>
<details>
<summary><b>文本检测模块:</b></summary>
<table>
<thead>
<tr>
<th>模型</th><th>模型下载链接</th>
<th>检测Hmean%</th>
<th>模型存储大小(MB</th>
<th>介绍</th>
</tr>
</thead>
<tbody>
<tr>
<td>PP-OCRv5_server_det (默认)</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_server_det_infer.tar">推理模型</a></td>
<td>83.8</td>
<td>84.3</td>
<td>PP-OCRv5 的服务端文本检测模型,精度更高,适合在性能较好的服务器上部署</td>
</tr>
<tr>
<td>PP-OCRv5_mobile_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_mobile_det_infer.tar">推理模型</a></td>
<td>79.0</td>
<td>4.7</td>
<td>PP-OCRv5 的移动端文本检测模型,效率更高,适合在端侧设备部署</td>
</tr>
<tr>
<td>PP-OCRv4_server_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv4_server_det_infer.tar">推理模型</a></td>
<td>69.2</td>
<td>109</td>
<td>PP-OCRv4 的服务端文本检测模型,精度更高,适合在性能较好的服务器上部署</td>
</tr>
<tr>
<td>PP-OCRv4_mobile_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv4_mobile_det_infer.tar">推理模型</a></td>
<td>63.8</td>
<td>4.7</td>
<td>PP-OCRv4 的移动端文本检测模型,效率更高,适合在端侧设备部署</td>
</tr>
</tbody>
</table>
</details>
<details>
<summary><b>文本识别模块:</b></summary>
<table>
<tr>
<th>模型</th><th>模型下载链接</th>
<th>识别 Avg Accuracy(%)</th>
<th>模型存储大小(MB</th>
<th>介绍</th>
</tr>
<tr>
<td>PP-OCRv5_server_rec (默认)</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_server_rec_infer.tar">推理模型</a></td>
<td>86.38</td>
<td>81</td>
<td rowspan="2">PP-OCRv5_rec 是新一代文本识别模型。该模型致力于以单一模型高效、精准地支持简体中文、繁体中文、英文、日文四种主要语言,以及手写、竖版、拼音、生僻字等复杂文本场景的识别。在保持识别效果的同时,兼顾推理速度和模型鲁棒性,为各种场景下的文档理解提供高效、精准的技术支撑。</td>
</tr>
<tr>
<td>PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_mobile_rec_infer.tar">推理模型</a></td>
<td>81.29</td>
<td>16</td>
</tr>
<tr>
<td>PP-OCRv4_server_rec_doc</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv4_server_rec_doc_infer.tar">推理模型</a></td>
<td>86.58</td>
<td>182</td>
<td>PP-OCRv4_server_rec_doc 是在 PP-OCRv4_server_rec 的基础上,在更多中文文档数据和PP-OCR训练数据的混合数据训练而成,增加了部分繁体字、日文、特殊字符的识别能力,可支持识别的字符为1.5万+,除文档相关的文字识别能力提升外,也同时提升了通用文字的识别能力</td>
</tr>
<tr>
<td>PP-OCRv4_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv4_mobile_rec_infer.tar">推理模型</a></td>
<td>78.74</td>
<td>10.5</td>
<td>PP-OCRv4 的轻量级识别模型,推理效率高,可以部署在包含端侧设备的多种硬件设备中</td>
</tr>
<tr>
<td>PP-OCRv4_server_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv4_server_rec_infer.tar">推理模型</a></td>
<td>85.19</td>
<td>173</td>
<td>PP-OCRv4 的服务器端模型,推理精度高,可以部署在多种不同的服务器上</td>
</tr>
</tbody>
</table>
</details>
也可以参考各模块的模型导出章节,如[文本检测模块-模型导出](../../../module_usage/text_detection.md#44),将训练好的模型导出为推理模型。
模型的目录结构一般如下所示:
```
PP-OCRv5_mobile_det
|--inference.pdiparams
|--inference.json
|--inference.yml
```
### 2.3 运行预测 demo
在本地使用通用 OCR 产线 C++前,请先成功编译预测 demo。编译后,可通过命令行体验或调用 api 进行二次开发并重新编译生成应用程序。
**请注意,如果在执行过程中遇到程序失去响应、程序异常退出、内存资源耗尽、推理速度极慢等问题,请尝试参考文档调整配置,例如关闭不需要使用的功能或使用更轻量的模型。**
本 demo 支持系统串联调用,也支持单个模块的调用,运行以下代码前,请您下载[示例图片](https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_002.png)到本地:
运行方式:
```shell
./build/ppocr <pipeline_or_module> [--param1] [--param2] [...]
```
常用参数如下:
<li>输入输出相关</li>
<table>
<thead>
<tr>
<th>参数</th>
<th>参数说明</th>
<th>参数类型</th>
<th>默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>input</code></td>
<td>本地待预测图片,必填。仅支持<code>jpg</code><code>png</code>, <code>jpeg</code>,<code>bmp</code>格式的图像。
</td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>save_path</code></td>
<td>指定推理结果文件保存的路径,该路径下会保存推理结果的 json 文件和预测结果图片。</td>
<td><code>str</code></td>
<td><code>./output</code></td>
</tr>
</tbody>
</table>
<details><summary><b>点击展开查看更多参数的详细说明</b></summary>
<li>通用参数</li>
<table>
<thead>
<tr>
<th>参数</th>
<th>参数说明</th>
<th>参数类型</th>
<th>默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>device</code></td>
<td>用于推理的设备。支持指定具体卡号:
<ul>
<li><b>CPU</b>:如 <code>cpu</code> 表示使用 CPU 进行推理;</li>
<li><b>GPU</b>:如 <code>gpu:0</code> 表示使用第 1 块 GPU 进行推理;</li>
</ul>如果不设置,将默认使用产线初始化的该参数值,初始化时,如果编译时添加<code>-DWITH_GPU=ON</code>,则会优先使用本地的 GPU 0号设备,否则,将使用 CPU 设备。
</td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>precision</code></td>
<td>计算精度,如 <code>fp32</code>、<code>fp16</code>。</td>
<td><code>str</code></td>
<td><code>fp32</code></td>
</tr>
<tr>
<td><code>enable_mkldnn</code></td>
<td>是否启用 MKL-DNN 加速推理。如果 MKL-DNN 不可用或模型不支持通过 MKL-DNN 加速,即使设置了此标志,也不会使用加速。
</td>
<td><code>bool</code></td>
<td><code>true</code></td>
</tr>
<tr>
<td><code>mkldnn_cache_capacity</code></td>
<td>
MKL-DNN 缓存容量。
</td>
<td><code>int</code></td>
<td><code>10</code></td>
</tr>
<tr>
<td><code>cpu_threads</code></td>
<td>PaddleInference CPU 加速库线程数量</td>
<td><code>int</code></td>
<td><code>8</code></td>
</tr>
<tr>
<td><code>paddlex_config</code></td>
<td>PaddleX产线配置文件路径。</td>
<td><code>str</code></td>
<td></td>
</tr>
</tbody>
</table>
<li>模块开关</li>
<table>
<thead>
<tr>
<th>参数</th>
<th>参数说明</th>
<th>参数类型</th>
<th>默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>use_doc_orientation_classify</code></td>
<td>是否加载并使用文档方向分类模块。如果不设置,将使用产线初始化的该参数值,默认初始化为<code>true</code>。</td>
<td><code>bool</code></td>
<td><code>true</code></td>
</tr>
<tr>
<td><code>use_doc_unwarping</code></td>
<td>是否加载并使用文本图像矫正模块。如果不设置,将使用产线初始化的该参数值,默认初始化为<code>true</code>。</td>
<td><code>bool</code></td>
<td><code>true</code></td>
</tr>
<tr>
<td><code>use_textline_orientation</code></td>
<td>是否加载并使用文本行方向模块。如果不设置,将使用产线初始化的该参数值,默认初始化为<code>true</code>。</td>
<td><code>bool</code></td>
<td><code>true</code></td>
</tr>
</tbody>
</table>
<li>检测模型相关</li>
<table>
<thead>
<tr>
<th>参数</th>
<th>参数说明</th>
<th>参数类型</th>
<th>默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>text_detection_model_name</code></td>
<td>文本检测模型的名称。如果不设置,将会使用产线默认模型。当传入文本检测模型路径的模型名称与产线默认文本识别模型名称配置不一致时,需指定传入模型的名称。</td>
<td><code>str</code></td>
<td><code>PP-OCRv5_server_det</code></td>
</tr>
<tr>
<td><code>text_detection_model_dir</code></td>
<td>文本检测模型的目录路径,必填。</td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>text_det_limit_side_len</code></td>
<td>文本检测的图像边长限制。
大于 <code>0</code> 的任意整数。如果不设置,将使用产线初始化的该参数值,默认初始化为 <code>64</code>。
</td>
<td><code>int</code></td>
<td><code>64</code></td>
</tr>
<tr>
<td><code>text_det_limit_type</code></td>
<td>文本检测的边长度限制类型。支持 <code>min</code> 和 <code>max</code><code>min</code> 表示保证图像最短边不小于 <code>det_limit_side_len</code><code>max</code> 表示保证图像最长边不大于 <code>limit_side_len</code>。如果不设置,将使用产线初始化的该参数值,默认初始化为 <code>min</code>。
</td>
<td><code>str</code></td>
<td><code>min</code></td>
</tr>
<tr>
<td><code>text_det_thresh</code></td>
<td>文本检测像素阈值,输出的概率图中,得分大于该阈值的像素点才会被认为是文字像素点。
大于<code>0</code>的任意浮点数。如果不设置,将使用产线初始化的该参数值。
</td>
<td><code>float</code></td>
<td><code>0.3</code></td>
</tr>
<tr>
<td><code>text_det_box_thresh</code></td>
<td>文本检测框阈值,检测结果边框内,所有像素点的平均得分大于该阈值时,该结果会被认为是文字区域。
大于 <code>0</code> 的任意浮点数。如果不设置,将使用产线初始化的该参数值(默认为 <code>0.6</code>)。
</td>
<td><code>float</code></td>
<td><code>0.6</code></td>
</tr>
<tr>
<td><code>text_det_unclip_ratio</code></td>
<td>文本检测扩张系数,使用该方法对文字区域进行扩张,该值越大,扩张的面积越大。大于 <code>0</code> 的任意浮点数。如果不设置,将使用产线初始化的该参数值。
</td>
<td><code>float</code></td>
<td><code>1.5</code></td>
</tr>
<tr>
<td><code>text_det_input_shape</code></td>
<td>文本检测的输入形状,您可以设置3个值代表C,H,W。</td>
<td><code>str</code></td>
<td>""</td>
</tr>
</tbody>
</table>
<li>方向分类器相关</li>
<table>
<thead>
<tr>
<th>参数</th>
<th>参数说明</th>
<th>参数类型</th>
<th>默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>doc_orientation_classify_model_name</code></td>
<td>文档方向分类模型的名称。如果不设置,将会使用产线默认模型。当传入文档方向分类模型与产线默认模型不一致时,需指定传入模型的名称。</td>
<td><code>str</code></td>
<td><code>PP-LCNet_x1_0_doc_ori</code></td>
</tr>
<tr>
<td><code>doc_orientation_classify_model_dir</code></td>
<td>文档方向分类模型的目录路径。当设置<code>use_doc_orientation_classify = false</code>时,可不添加。</td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>textline_orientation_model_name</code></td>
<td>文本行方向分类模型的名称。如果不设置,将会使用产线默认模型。当传入文本行方向分类模型与产线默认模型不一致时,需指定传入模型的名称。</td>
<td><code>str</code></td>
<td><code>PP-LCNet_x1_0_textline_ori</code></td>
</tr>
<tr>
<td><code>textline_orientation_model_dir</code></td>
<td>文本行方向分类模型的目录路径。当设置<code>use_textline_orientation = false</code>时,可不添加。</td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>textline_orientation_batch_size</code></td>
<td>文本行方向模型的batch size。如果不设置,将会使用产线默认模型。</td>
<td><code>int</code></td>
<td><code>6</code></td>
</tr>
</tbody>
</table>
<li>文字识别模型相关</li>
<table>
<thead>
<tr>
<th>参数</th>
<th>参数说明</th>
<th>参数类型</th>
<th>默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>text_recognition_model_name</code></td>
<td>文本识别模型的名称。如果不设置,将会使用产线默认模型。当传入文本识别模型路径的模型名称与产线默认文本识别模型名称配置不一致时,需指定传入模型的名称。</td>
<td><code>str</code></td>
<td><code>PP-OCRv5_server_rec</code></td>
</tr>
<tr>
<td><code>text_recognition_model_dir</code></td>
<td>文本识别模型的目录路径,必填。</td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>text_recognition_batch_size</code></td>
<td>文本识别模型的batch size。如果不设置,将会使用产线默认值。</td>
<td><code>int</code></td>
<td><code>6</code></td>
</tr>
<tr>
<td><code>text_rec_score_thresh</code></td>
<td>文本识别阈值,得分大于该阈值的文本结果会被保留。大于<code>0</code>的任意浮点数。</td>
<td><code>float</code></td>
<td><code>0.0</code></td>
</tr>
<tr>
<td><code>text_rec_input_shape</code></td>
<td>文本识别的输入形状,您可以设置3个值代表C,H,W。</td>
<td><code>str</code></td>
<td>""</td>
</tr>
</tbody>
</table>
</details>
#### 2.3.1 调用示例-系统串联调用
本节提供了系统串联调用的调用示例,请参考 2.1 节准备好模型,假设模型的目录结构如下所示:
```
models
|--PP-LCNet_x1_0_doc_ori_infer
|--UVDoc_infer
|--PP-LCNet_x1_0_textline_ori_infer
|--PP-OCRv5_server_det_infer
|--PP-OCRv5_server_rec_infer
```
=== "全模块串联"
```bash
./build/ppocr ocr --input ./general_ocr_002.png --save_path ./output/ \
--doc_orientation_classify_model_dir models/PP-LCNet_x1_0_doc_ori_infer \
--doc_unwarping_model_dir models/UVDoc_infer \
--textline_orientation_model_dir models/PP-LCNet_x1_0_textline_ori_infer \
--text_detection_model_dir models/PP-OCRv5_server_det_infer \
--text_recognition_model_dir models/PP-OCRv5_server_rec_infer \
--device cpu
```
输出示例(若指定了 `save_path`,则会在该路径下生成标准的 json 预测结果文件和预测结果图片):
```bash
{
"input_path": "./general_ocr_002.png",
"doc_preprocessor_res": {
"model_settings": {"use_doc_unwarping": true, "use_doc_orientation_classify": true},
"angle": 0
},
...,
"dt_polys": [[[132, 6], [355, 6], [355, 64], [132, 64]],
[[424, 9], [689, 9], [689, 59], [424, 59]],
...,
[[664, 8], [867, 4], [868, 55], [665, 60]],
[[31, 99], [173, 99], [173, 126], [31, 126]]],
...,
"rec_texts": ["登机牌", "BOARDING", "GPASS", ..., ],
...,
}
```
=== "文本检测+文本行方向分类+文本识别"
```bash
./build/ppocr ocr --input ./general_ocr_002.png --save_path ./output/ \
--textline_orientation_model_dir models/PP-LCNet_x1_0_textline_ori_infer \
--text_detection_model_dir models/PP-OCRv5_server_det_infer \
--text_recognition_model_dir models/PP-OCRv5_server_rec_infer \
--use_doc_orientation_classify False \
--use_doc_unwarping False \
--device cpu
```
输出示例(若指定了 `save_path`,则会在该路径下生成标准的 json 预测结果文件和预测结果图片):
```bash
{
"input_path": "./general_ocr_002.png",
...,
"dt_polys": [[[0, 1], [334, 1], [334, 34], [0, 34]],
[[151, 21], [357, 16], [358, 72], [152, 76]],
...,
[[675, 97], [740, 97], [740, 121], [675, 121]],
[[751, 97], [836, 94], [837, 115], [752, 119]],
...,
"rec_texts": ["净小8866-", "登机牌", "BOARDING", "GPASS", ..., ],
...,
}
```
=== "文本检测+文本识别"
```bash
./build/ppocr ocr --input ./general_ocr_002.png --save_path ./output/ \
--text_detection_model_dir models/PP-OCRv5_server_det_infer \
--text_recognition_model_dir models/PP-OCRv5_server_rec_infer \
--use_doc_orientation_classify False \
--use_doc_unwarping False \
--use_textline_orientation False \
--device cpu
```
输出示例(若指定了 `save_path`,则会在该路径下生成标准的 json 预测结果文件和预测结果图片):
```bash
{
"input_path": "./general_ocr_002.png",
...,
"dt_polys": [[[0, 1], [334, 1], [334, 34], [0, 34]],
[[151, 21], [357, 16], [358, 72], [152, 76]],
...,
[[61, 109], [194, 106], [194, 132], [61, 135]],
[[80, 138], [219, 136], [219, 162], [80, 164]],
...,
"rec_texts": ["www.997788.com中国收藏热线","登机牌", "BOARDING", "GPASS", ..., ],
...,
}
```
以上示例代码会生成如下文本检测结果图:
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/ocr_res.png"/>
若需要查看文本识别结果图,请参考后文 **可视化文本识别结果** 小节。
#### 2.3.2 调用示例-单模块调用
=== "文档图像方向分类"
```bash
./build/ppocr doc_img_orientation_classification --input ./general_ocr_002.png --save_path ./output/ \
--doc_orientation_classify_model_dir models/PP-LCNet_x1_0_doc_ori_infer \
--device cpu
```
输出示例(若指定了 `save_path`,则会在该路径下生成标准的 json 预测结果文件和预测结果图片):
```bash
{
"res": {
"input_path": {./general_ocr_002.png},
"class_ids": {0},
"scores": {0.926328},
"label_names": {0},
}
```
=== "文档图像矫正"
```bash
./build/ppocr text_image_unwarping --input ./general_ocr_002.png --save_path ./output/ \
--doc_unwarping_model_dir models/UVDoc_infer \
--device cpu
```
输出示例(若指定了 `save_path`,则会在该路径下生成标准的 json 预测结果文件和预测结果图片):
```bash
{
"res": {
"input_path": {./general_ocr_002.png},
"doctr_img": {...}
}
```
=== "文本行方向分类"
```bash
./build/ppocr textline_orientation_classification --input ./general_ocr_002.png --save_path ./output/ \
--textline_orientation_model_dir models/PP-LCNet_x1_0_textline_ori_infer \
--device cpu
```
输出示例(若指定了 `save_path`,则会在该路径下生成标准的 json 预测结果文件和预测结果图片):
```bash
{
"res": {
"input_path": {./general_ocr_002.png},
"class_ids": {0},
"scores": {0.719926},
"label_names": {0_degree},
}
```
=== "文本检测"
```bash
./build/ppocr text_detection --input ./general_ocr_002.png --save_path ./output/ \
--text_detection_model_dir models/PP-OCRv5_server_det_infer \
--device cpu
```
输出示例(若指定了 `save_path`,则会在该路径下生成标准的 json 预测结果文件和预测结果图片):
```bash
{
"res": {
"input_path": {./general_ocr_002.png },
"dt_polys": [
[[98, 456], [834, 441], [834, 466], [98, 480]],
[[344, 347], [662, 343], [662, 366], [344, 371]],
[[66, 341], [165, 337], [167, 363], [67, 367]],
...,
[[0, 1], [331, 0], [332, 32], [0, 34]],
]},
"dt_scores": [
0.812284, 0.8082, 0.848293, ...,
]
}
}
```
=== "文本识别"
```bash
./build/ppocr text_recognition --input ./general_ocr_rec_001.png --save_path ./output/ \
--text_recognition_model_dir models/PP-OCRv5_server_rec_infer \
--device cpu
```
输出示例(若指定了 `save_path`,则会在该路径下生成标准的 json 预测结果文件和预测结果图片):
```bash
{
"res": {
"input_path": {./general_ocr_rec_001.png },
"rec_text": {绿洲仕格维花园公寓 }
"rec_score": {0.982409 }
}
```
### 2.4 C++ API 集成
命令行方式是为了快速体验查看效果,一般来说,在项目中,往往需要通过代码集成,您可以通过几行代码即可完成产线的快速推理,推理代码如下:
由于通用 OCR 产线配置参数较多,故采用结构体传参进行实例化,结构体命名规则为 `pipeline_class_name + Params`,如通用 OCR 产线对应的类名为 `PaddleOCR` ,结构体为 `PaddleOCRParams`。
```c++
#include "src/api/pipelines/ocr.h"
int main(){
PaddleOCRParams params;
params.doc_orientation_classify_model_dir = "models/PP-LCNet_x1_0_doc_ori_infer"; // 文档方向分类模型路径。
params.doc_unwarping_model_dir = "models/UVDoc_infer"; // 文本图像矫正模型路径。
params.textline_orientation_model_dir = "models/PP-LCNet_x1_0_textline_ori_infer"; // 文本行方向分类模型路径。
params.text_detection_model_dir = "models/PP-OCRv5_server_det_infer"; // 文本检测模型路径
params.text_recognition_model_dir = "models/PP-OCRv5_server_rec_infer"; // 文本识别模型路径
// params.device = "gpu"; // 推理时使用GPU。请确保编译时添加 -DWITH_GPU=ON 选项,否则使用CPU。
// params.use_doc_orientation_classify = false; // 不使用文档方向分类模型。
// params.use_doc_unwarping = false; // 不使用文本图像矫正模型。
// params.use_textline_orientation = false; // 不使用文本行方向分类模型。
// params.text_detection_model_name = "PP-OCRv5_server_det"; // 使用 PP-OCRv5_server_det 模型进行检测。
// params.text_recognition_model_name = "PP-OCRv5_server_rec"; // 使用 PP-OCRv5_server_rec 模型进行识别。
// params.vis_font_dir = "your_vis_font_dir"; // 当编译时添加 -DUSE_FREETYPE=ON 选项,必须提供相应 ttf 字体文件路径。
auto infer = PaddleOCR(params);
auto outputs = infer.Predict("./general_ocr_002.png");
for (auto& output : outputs) {
output->Print();
output->SaveToImg("./output/");
output->SaveToJson("./output/");
}
}
```
## 3. 拓展功能
### 3.1 多语种文字识别
PP-OCRv5 还提供了覆盖 39 种语言的多语种文字识别能力,包括韩文、西班牙文、法文、葡萄牙文、德文、意大利文、俄罗斯文、泰文、希腊文等,具体支持语种如下:
<table>
<thead>
<tr>
<th>模型</th>
<th>链接</th>
<th>支持语种</th>
</tr>
</thead>
<tbody>
<tr>
<td>PP-OCRv5_server_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_server_rec_infer.tar">推理模型</a></td>
<td>简体中文、繁体中文、英文、日文</td>
</tr>
<tr>
<td>PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_mobile_rec_infer.tar">推理模型</a></td>
<td>简体中文、繁体中文、英文、日文</td>
</tr>
<tr>
<td>korean_PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/korean_PP-OCRv5_mobile_rec_infer.tar">推理模型</a></td>
<td>韩文、英文</td>
</tr>
<tr>
<td>latin_PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/latin_PP-OCRv5_mobile_rec_infer.tar">推理模型</a></td>
<td>英文、法文、德文、南非荷兰文、意大利文、西班牙文、波斯尼亚文、葡萄牙文、捷克文、威尔士文、丹麦文、爱沙尼亚文、爱尔兰文、克罗地亚文、乌兹别克文、匈牙利文、塞尔维亚文(latin)、印度尼西亚文、欧西坦文、冰岛文、立陶宛文、毛利文、马来文、荷兰文、挪威文、波兰文、斯洛伐克文、斯洛文尼亚文、阿尔巴尼亚文、瑞典文、西瓦希里文、塔加洛文、土耳其文、拉丁文</td>
</tr>
<tr>
<td>eslav_PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/eslav_PP-OCRv5_mobile_rec_infer.tar">推理模型</a></td>
<td>俄罗斯文、白俄罗斯文、乌克兰文、英文</td>
</tr>
<tr>
<td>th_PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/th_PP-OCRv5_mobile_rec_infer.tar">推理模型</a></td>
<td>泰文、英文</td>
</tr>
<tr>
<td>el_PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/el_PP-OCRv5_mobile_rec_infer.tar">推理模型</a></td>
<td>希腊文、英文</td>
</tr>
<tr>
<td>en_PP-OCRv5_mobile_rec</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/en_PP-OCRv5_mobile_rec_infer.tar">推理模型</a></td>
<td>英文</td>
</tr>
</tbody>
</table>
在使用产线或模块时传入对应的识别模型即可,如使用文本识别模块进行法文识别:
```
./build/ppocr text_recognition \
--input ./french.png \
--text_recognition_model_name latin_PP-OCRv5_mobile_rec \
--text_recognition_model_dir latin_PP-OCRv5_mobile_rec_infer \
--save_path ./output/
```
更多详细说明可参考 [PP-OCRv5多语种文字识别介绍](../../../algorithm/PP-OCRv5/PP-OCRv5_multi_languages.md)。
### 3.2 可视化文本识别结果
我们使用 4.x 版本的 opencv_contrib 模块中的 FreeType 进行字体渲染,如果想要可视化文本识别结果,需要下载 OpenCV 和 opencv_contrib 的源码并编译包含 FreeType 模块的 OpenCV。下载源码时需确保两者的版本一致。以下以 opencv-4.7.0 和 opencv_contrib-4.7.0 为例进行说明:
```bash
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/opencv-4.7.0.tgz
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/opencv_contrib-4.7.0.tgz
tar -xf opencv-4.7.0.tgz
tar -xf opencv_contrib-4.7.0.tgz
```
安装FreeType依赖库
```bash
sudo apt-get update
sudo apt-get install libfreetype6-dev libharfbuzz-dev
```
编译包含 FreeType 模块的 OpenCV 的步骤如下:
- a. 在 `tools/build_opencv.sh` 脚本中增加如下三个选项:
- -DOPENCV_EXTRA_MODULES_PATH=your_opencv_contrib-4.7.0/modules/
- -DBUILD_opencv_freetype=ON
- -DWITH_FREETYPE=ON
- b. 在 `tools/build_opencv.sh` 脚本中,将 `root_path` 设置为 opencv-4.7.0 源码的绝对路径。
- c. 在 `tools/build_opencv.sh` 脚本中,设置 `install_path`,如默认的 `${root_path}/opencv4`。`install_path` 在后续编译预测 demo 时,将作为 OpenCV 库的路径使用。
- d. 配置完成后,运行以下命令进行 OpenCV 的编译:
```bash
sh tools/build_opencv.sh
```
- e. 在 `tools/build.sh` 设置 `-DUSE_FREETYPE=ON` 开启文字渲染功能,设置 `--vis_font_dir your_ttf_path` 提供相应 ttf 字体文件路径。运行以下命令进行预测 demo 的编译:
```bash
sh tools/build.sh
```
编译并运行预测 demo 可以得到如下可视化文本识别结果:
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/ocr_res_with_freetype.png"/>
## 4. FAQ
1. 如果遇到 `Model name mismatch, please input the correct model dir. model dir is xxx, but model name is xxx` 的报错,说明指定的模型名称和传入模型不匹配。比如文本识别模型指定名称是 `PP-OCRv5_server_rec `,但传入模型是 `PP-OCRv5_mobile_rec`。
解决:需要调整模型名称或传入的模型。例如上述例子,可以使用 `--text_recognition_model_name PP-OCRv5_mobile_rec` 指定和传入模型匹配的模型名称。
2. 在 Windows 的控制台中输出出现乱码,原因可能是 Windows 控制台的字符编码是 GBK,请设置为 UTF-8 编码。
@@ -0,0 +1,240 @@
# C++ Local Deployment for General OCR Pipeline - Windows
- [1. Environment Preparation](#1-environment-preparation)
- [1.1 Compile OpenCV Library](#11-compile-opencv-library)
- [1.2 Compile Paddle Inference](#12-compile-paddle-inference)
- [2. Getting Started](#2-getting-started)
- [2.1 Compile Prediction Demo](#21-compile-prediction-demo)
- [2.2 Prepare the Model](#22-prepare-the-model)
- [2.3 Run Prediction Demo](#23-run-the-prediction-demo)
- [2.4 C++ API Integration](#24-c-api-integration)
- [3. Extended Features](#3-extended-features)
- [3.1 Multilingual Text Recognition](#31-multilingual-text-recognition)
- [3.2 Visualize Text Recognition Results](#32-visualize-text-recognition-results)
## 1. Environment Preparation
- **The source code used in this compilation and runtime section is located in the [PaddleOCR/deploy/cpp_infer](https://github.com/PaddlePaddle/PaddleOCR/tree/{{PADDLEOCR_GITHUB_REF}}/deploy/cpp_infer) directory.**
- Windows Environment:
- Visual Studio 2022
- CMake 3.29
### 1.1 Compile OpenCV Library
You can choose to directly download a pre-compiled package or manually compile the source code.
#### 1.1.1 Directly Download Pre-compiled Package (Recommended)
Download the .exe pre-compiled package for Windows from the [OpenCV Official Website](https://opencv.org/releases/). Running it will automatically extract the pre-compiled OpenCV library and related folders.
Taking OpenCV 4.7.0 as an example, download [opencv-4.7.0-windows.exe](https://github.com/opencv/opencv/releases/download/4.7.0/opencv-4.7.0-windows.exe). After running it, a `opencv/` subfolder will be generated in the current folder, where `opencv/build` contains the pre-compiled library. This will be used as the path for the OpenCV installation library when compiling the universal OCR pipeline prediction demo later.
#### 1.1.2 Compile from Source Code
First, download the OpenCV source code. Taking OpenCV 4.7.0 as an example, download the [opencv 4.7.0](https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/opencv-4.7.0.tgz) source code. After extracting it, an `opencv-4.7.0/` folder will be generated in the current folder.
- Step 1: Build Visual Studio Project
Specify the `opencv-4.7.0` source code path in cmake-gui, and set the compilation output directory to `opencv-4.7.0/build`. The default installation path is `opencv-4.7.0/build/install`. This installation path will be used for subsequent demo compilation.
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_step1.png"/>
- Step 2: Select Target Platform
Choose x64 as the target platform and then click Finish.
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_step2.png"/>
- Step 3: Generate Visual Studio Project
Search for `BUILD_opencv_world` and check it.
Click Configure -> Generate -> Open in Project sequentially to enter the Visual Studio 2022 compilation interface.
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_step3.png"/>
- Step 4: Execute Compilation
Click Start Solution Generation. After compilation, click INSTALL to complete the installation.
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_step4.png"/>
### 1.2 Compile Paddle Inference
You can choose to directly download a pre-compiled package or manually compile the source code.
#### 1.2.1 Directly Download Pre-compiled Package (Recommended)
The [Paddle Inference Official Website](https://www.paddlepaddle.org.cn/inference/v3.0/guides/install/download_lib.html#windows) provides Windows prediction libraries. You can view and select the appropriate pre-compiled package on the official website.
After downloading and extracting it, a `paddle_inference/` subfolder will be generated in the current folder. The directory structure is as follows:
```
paddle_inference
├── paddle # Paddle core library and header files
├── third_party # Third-party dependency libraries and header files
└── version.txt # Version and compilation information
```
#### 1.2.2 Compile Prediction Library from Source Code
You can choose to compile the prediction library from source code. Compiling from source allows flexible configuration of various features and dependencies to adapt to different hardware and software environments. For detailed steps, please refer to [Compiling from Source under Windows](https://www.paddlepaddle.org.cn/inference/v3.0/guides/install/compile/source_compile_under_Windows.html).
## 2. Getting Started
### 2.1 Compile Prediction Demo
Before compiling the prediction demo, please ensure that you have compiled the OpenCV library and Paddle Inference prediction library according to Sections 1.1 and 1.2.
The compilation steps are as follows:
- Step 1: Build Visual Studio Project
Specify the `deploy\cpp_infer` source code path in cmake-gui, and set the compilation output directory to `deploy\cpp_infer\build`. The following compilation steps will use `D:\PaddleOCR\deploy\cpp_infer` as an example source code path. It is normal to encounter an error during the first Configure click. In the subsequent compilation options that pop up, add the installation path for OpenCV and the Paddle Inference prediction library path.
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/windows_step1.png"/>- Step 2: Select the target platform
Select the target platform as x64 and click Finish.
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/windows_step2.png"/>
- Step 3Configure CMake build options
- OPENCV_DIR: Specify the installation path of OpenCV.
- OpenCV_DIR: Same as OPENCV_DIR.
- PADDLE_LIB: Path to the Paddle Inference library.
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/windows_step3.png"/>
- Step 4Generate Visual Studio project
Click Configure → Generate → Open in Project to open the Visual Studio 2022 compilation interface.
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/windows_step4.png"/>
- Step 5Compile the code
Before building the solution:
1. Change the build configuration from `Debug` to `Release`.
2. Download [dirent.h](https://paddleocr.bj.bcebos.com/deploy/cpp_infer/cpp_files/dirent.h) and copy it to the Visual Studio include folder (e.g., `C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\VC\Auxiliary\VS\include`).
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/windows_step5.png"/>
After compilation, the executable file will be located at `deploy/cpp_infer/build/Release/ppocr.exe`.
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/windows_step6.png"/>
- Step 6Run the prediction demo
Copy the following files to the `deploy\cpp_infer\build\Release\` directory, then follow Sections 2.2 and 2.3 to run the demo:
1. `paddle_inference\paddle\lib\paddle_inference.dll`
2. `paddle_inference\paddle\lib\common.dll`
3. `deploy\cpp_infer\build\bin\Release\abseil_dll.dll`
4. `deploy\cpp_infer\build\third_party\clipper_ver6.4.2\cpp\Release\polyclipping.dll`
5. `opencv-4.7.0\build\install\x64\vc16\bin\opencv_world470.dll`
### 2.2 Prepare the Model
Refer to the [General OCR Pipeline C++ Deployment - Linux → 2.2 Prepare the Model](./OCR.en.md#22-prepare-models) section.
### 2.3 Run the Prediction Demo
Refer to the [General OCR Pipeline C++ Deployment - Linux → 2.3 Run the Prediction Demo](./OCR.en.md#23-run-the-prediction-demo) section.
### 2.4 C++ API Integration
Refer to the [General OCR Pipeline C++ Deployment - Linux → 2.4 C++ API Integration](./OCR.en.md#24-c-api-integration) section.
## 3. Extended Features
### 3.1 Multilingual Text Recognition
Refer to the [General OCR Pipeline C++ Deployment - Linux → 3.1 Multilingual Text Recognition](./OCR.en.md#31-multilingual-text-recognition) section.
### 3.2 Visualize Text Recognition Results
To visualize text recognition results, you need to compile OpenCV with the FreeType module from the `opencv_contrib` repository (version 4.x). Ensure the OpenCV and `opencv_contrib` versions match. Below is an example using `opencv-4.7.0` and `opencv_contrib-4.7.0`:
[Download opencv-4.7.0](https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/opencv-4.7.0.tgz)
[Download opencv_contrib-4.7.0](https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/opencv_contrib-4.7.0.tgz)
- Step 1Compile FreeType and HarfBuzz
- [Download pkg-config](https://sourceforge.net/projects/pkgconfiglite/)
- [Download freetype2](https://download.savannah.gnu.org/releases/freetype/)
- [Download harfbuzz](https://github.com/harfbuzz/harfbuzz)
Add the `bin` directory of the extracted `pkg-config` to the system PATH.
For FreeType compilation, manually specify the installation path (example below):
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_freetype_step1.png"/>
Click Configure → Generate, then open the project in Visual Studio to compile.
In VS, build `ALL_BUILD` and `INSTALL` to generate the required `include` and `lib` files in the `install` directory.
Add the FreeType installation path to the system environment variables.
For HarfBuzz compilation, manually specify the installation path (example below):
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_freetype_step2.png"/>
After setting the paths, click Configure again, go to **Advanced Options**, and specify the FreeType installation path.
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_freetype_step3.png"/>
Add the HarfBuzz installation path to the system environment variables.- Step 2: Modify `modules/freetype/CMakeLists.txt` under opencv_contrib-4.7.0
```bash
set(the_description "FreeType module. It enables to draw strings with outlines and mono-bitmaps/gray-bitmaps.")
find_package(Freetype REQUIRED)
# find_package(HarfBuzz) is not included in cmake
set(HARFBUZZ_DIR "$ENV{HARFBUZZ_DIR}" CACHE PATH "HarfBuzz directory")
find_path(HARFBUZZ_INCLUDE_DIRS
NAMES hb-ft.h PATH_SUFFIXES harfbuzz
HINTS ${HARFBUZZ_DIR}/include)
find_library(HARFBUZZ_LIBRARIES
NAMES harfbuzz
HINTS ${HARFBUZZ_DIR}/lib)
find_package_handle_standard_args(HARFBUZZ
DEFAULT_MSG HARFBUZZ_LIBRARIES HARFBUZZ_INCLUDE_DIRS)
if(NOT FREETYPE_FOUND)
message(STATUS "freetype2: NO")
else()
message(STATUS "freetype2: YES")
endif()
if(NOT HARFBUZZ_FOUND)
message(STATUS "harfbuzz: NO")
else()
message(STATUS "harfbuzz: YES")
endif()
if(FREETYPE_FOUND AND HARFBUZZ_FOUND)
ocv_define_module(freetype opencv_core opencv_imgproc PRIVATE_REQUIRED ${FREETYPE_LIBRARIES} ${HARFBUZZ_LIBRARIES} WRAP python)
ocv_include_directories(${FREETYPE_INCLUDE_DIRS} ${HARFBUZZ_INCLUDE_DIRS})
else()
ocv_module_disable(freetype)
endif()
```
- Step 3: Compile OpenCV
1. Set the `OPENCV_EXTRA_MODULES_PATH` item and fill in the path to the `modules` directory under the opencv-contrib-4.7.0 directory.
2. Check the `WITH_FREETYPE` item. Note that you must first compile freetype and harfbuzz.
3. If you need to support freetype, you need to add the relevant paths of freetype in the CMake configuration of OpenCV.
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_freetype_step4.png"/>
Search for harfbuzz, add harfbuzz, and add the relevant paths.
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_freetype_step5.png"/>
After completing the above steps, click Configure again in the CMake interface. After ensuring there are no errors, click Generate, and then click Open Project to open Visual Studio. Switch from Debug to Release, right-click on ALL_BUILD and select Build. After the compilation is completed, right-click on INSTALL and select Build.
Note: If you have compiled OpenCV with FreeType included, when compiling the demo for the General OCR Pipeline in Section 2.1 Step 3, you need to check the `USE_FREETYPE` option to enable text rendering functionality. Additionally, when running the demo, you need to provide the path to the corresponding TTF font file using the `--vis_font_dir your_ttf_path` parameter.
After compiling and running the prediction demo, you can obtain the following visualized text recognition results:
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/ocr_res_with_freetype.png"/>
@@ -0,0 +1,247 @@
# 通用 OCR 产线 C++ 部署 - Windows
- [1. 环境准备](#1)
- [1.1 编译 OpenCV 库](#11-opencv)
- [1.2 编译 Paddle Inference](#12-paddle-inference)
- [2. 开始运行](#2)
- [2.1 编译预测 demo](#21-demo)
- [2.2 准备模型](#22)
- [2.3 运行预测 demo](#23-demo)
- [2.4 C++ API 集成](#24-c-api)
- [3. 拓展功能](#3)
- [3.1 多语种文字识别](#31)
- [3.2 可视化文本识别结果](#32)
## 1. 环境准备
- **本章节编译运行时用到的源代码位于 [PaddleOCR/deploy/cpp_infer](https://github.com/PaddlePaddle/PaddleOCR/tree/{{PADDLEOCR_GITHUB_REF}}/deploy/cpp_infer) 目录下。**
- Windows 环境:
- visual studio 2022
- cmake 3.29
### 1.1 编译 OpenCV 库
可以选择直接下载预编译包或者手动编译源码。
#### 1.1.1 直接下载预编译包(推荐)
在 [OpenCV 官网](https://opencv.org/releases/) 下载适用于 Windows 的 .exe 预编译包,运行后自动解压出 OpenCV 的预编译库和相关文件夹。
以 opencv 4.7.0为例,下载 [opencv-4.7.0-windows.exe](https://github.com/opencv/opencv/releases/download/4.7.0/opencv-4.7.0-windows.exe),运行后会在当前的文件夹中生成 `opencv/` 的子文件夹,其中 `opencv/build` 为预编译库,在后续编译通用 OCR 产线 预测 demo 时,将作为 OpenCV 安装库的路径使用。
#### 1.1.2 源码编译
首先需要下载 OpenCV 源码,以 opencv 4.7.0 为例,下载 [opencv 4.7.0](https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/opencv-4.7.0.tgz) 源码,解压后会在当前的文件夹中生成 `opencv-4.7.0/` 的文件夹。
- Step 1:构建 Visual Studio 项目
在 cmake-gui 中指定 `opencv-4.7.0` 源码路径,并指定编译生成目录为 `opencv-4.7.0/build`,默认安装路径为 `opencv-4.7.0/build/install`,此安装路径用于后续编译 demo。
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_step1.png"/>
- Step 2:选择目标平台
选择目标平台为 x64 ,然后点击 finish。
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_step2.png"/>
- Step 3 :生成 Visual Studio 项目
搜索 `BUILD_opencv_world` 并勾选。
依次点击 Configure -> Generate -> Open in Project,将进入 Visual Studio 2022 编译界面。
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_step3.png"/>
- Step 4:执行编译
点击开始生成解决方案,完成编译后,点击 INSTALL,运行后完成安装。
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_step4.png"/>
### 1.2 编译 Paddle Inference
可以选择直接下载预编译包或者手动编译源码。
#### 1.2.1 直接下载预编译包(推荐)
[Paddle Inference 官网](https://www.paddlepaddle.org.cn/inference/v3.0/guides/install/download_lib.html#windows) 上提供了 Windows 预测库,可以在官网查看并选择合适的预编译包。
下载后解压,会在当前的文件夹中生成 `paddle_inference/` 的子文件夹。目录结构为:
```
paddle_inference
├── paddle # paddle核心库和头文件
├── third_party # 第三方依赖库和头文件
└── version.txt # 版本和编译信息
```
#### 1.2.2 源码编译预测库
可以选择通过源码自行编译预测库,源码编译可灵活配置各类功能和依赖,以适应不同的硬件和软件环境。详细步骤请参考 [Windows 下源码编译](https://www.paddlepaddle.org.cn/inference/v3.0/guides/install/compile/source_compile_under_Windows.html)。
## 2. 开始运行
### 2.1 编译预测 demo
在编译预测 demo 前,请确保您已经按照 1.1 和 1.2 节编译好 OpenCV 库和 Paddle Inference 预测库。
编译步骤如下:
- Step 1:构建 Visual Studio 项目
在 cmake-gui 中指定 `deploy\cpp_infer` 源码路径,并指定编译生成目录为 `deploy\cpp_infer\build`,以下编译步骤说明均以 `D:\PaddleOCR\deploy\cpp_infer` 作为示例源码路径。第一次点击 Configure 报错是正常的,在后续弹出的编译选项中,添加 OpenCV 的安装路径和 Paddle Inference 预测库路径。
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/windows_step1.png"/>
- Step 2:选择目标平台
选择目标平台为 x64 ,然后点击 Finish。
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/windows_step2.png"/>
- Step 3:配置 cmake 编译选项
- OPENCV_DIR:填写 OpenCV 安装路径。
- OpenCV_DIR:同 OPENCV_DIR。
- PADDLE_LIBPaddle Inference 预测库路径。
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/windows_step3.png"/>
- Step 4:生成 Visual Studio 项目
依次点击 Configure -> Generate -> Open in Project,将进入 Visual Studio 2022 编译界面。
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/windows_step4.png"/>
- Step 5:执行编译
在开始生成解决方案之前,执行下面步骤:
1.`Debug` 改为 `Release`
2. 下载[dirent.h](https://paddleocr.bj.bcebos.com/deploy/cpp_infer/cpp_files/dirent.h),并拷贝到 Visual Studio 的 include 文件夹下,如 `C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\VC\Auxiliary\VS\include`
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/windows_step5.png"/>
编译完成后,可执行文件位于 `deploy/cpp_infer/build/Release/ppocr.exe`
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/windows_step6.png"/>
- Step 6:运行预测 demo
将下面文件拷贝到 `deploy\cpp_infer\build\Release\` 路径下后,参考后续 2.2 和 2.3 小节即可运行预测 demo。
1. `paddle_inference\paddle\lib\paddle_inference.dll`
2. `paddle_inference\paddle\lib\common.dll`
3. `deploy\cpp_infer\build\bin\Release\abseil_dll.dll`
4. `deploy\cpp_infer\build\third_party\clipper_ver6.4.2\cpp\Release\polyclipping.dll`
5. `opencv-4.7.0\build\install\x64\vc16\bin\opencv_world470.dll`
### 2.2 准备模型
该步骤参考 [通用 OCR 产线 C++ 部署 - Linux —— 2.2 准备模型](./OCR.md#22) 小节。
### 2.3 运行预测 demo
参考 [通用 OCR 产线 C++ 部署 - Linux —— 2.3 运行预测 demo](./OCR.md#23-demo) 小节。
### 2.4 C++ API 集成
参考 [通用 OCR 产线 C++ 部署 - Linux —— 2.4 C++ API 集成](./OCR.md#24-c-api) 小节。
## 3. 拓展功能
### 3.1 多语种文字识别
参考 [通用 OCR 产线 C++ 部署 - Linux —— 3.1 多语种文字识别](./OCR.md#31) 小节。
### 3.2 可视化文本识别结果
我们使用 4.x 版本的 opencv_contrib 模块中的 FreeType 进行字体渲染,如果想要可视化文本识别结果,需要下载 OpenCV 和 opencv_contrib 的源码并编译包含 FreeType 模块的 OpenCV。下载源码时需确保两者的版本一致。以下以 opencv-4.7.0 和 opencv_contrib-4.7.0 为例进行说明:
[下载 opencv-4.7.0](https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/opencv-4.7.0.tgz)
[下载 opencv_contrib-4.7.0](https://paddle-model-ecology.bj.bcebos.com/paddlex/cpp/libs/opencv_contrib-4.7.0.tgz)
- Step 1:编译 freetype 和 harfbuzz
- [下载pkg-config](https://sourceforge.net/projects/pkgconfiglite/)
- [下载freetype2](https://download.savannah.gnu.org/releases/freetype/)
- [下载harfbuzz](https://github.com/harfbuzz/harfbuzz)
解压 pkg-config 后添加其 bin 目录到系统 PATH 环境变量。
freetype 编译,需手动更改其安装路径,示例如下:
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_freetype_step1.png"/>
再次 Configure 然后点击 Generate 完成后,点击 Open Project 按钮,打开 VS ,编译。
VS里ALL_BUILD, INSTALL. 会在构建文件夹的 install 目录下生成所需的 include 和 lib 文件。
然后将 freetype 安装路径添加至系统环境变量。
harfbuzz 编译,需手动更改其安装路径,示例如下:
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_freetype_step2.png"/>
设置好上面两项后,再次点击 Configure 按钮,选择 Advanced Options ,填写 freetype 安装路径。
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_freetype_step3.png"/>
然后将 harfbuzz 安装路径添加至系统环境变量。
- Step 2:修改 opencv_contrib-4.7.0 下的 `modules/freetype/CMakeLists.txt`
```bash
set(the_description "FreeType module. It enables to draw strings with outlines and mono-bitmaps/gray-bitmaps.")
find_package(Freetype REQUIRED)
# find_package(HarfBuzz) is not included in cmake
set(HARFBUZZ_DIR "$ENV{HARFBUZZ_DIR}" CACHE PATH "HarfBuzz directory")
find_path(HARFBUZZ_INCLUDE_DIRS
NAMES hb-ft.h PATH_SUFFIXES harfbuzz
HINTS ${HARFBUZZ_DIR}/include)
find_library(HARFBUZZ_LIBRARIES
NAMES harfbuzz
HINTS ${HARFBUZZ_DIR}/lib)
find_package_handle_standard_args(HARFBUZZ
DEFAULT_MSG HARFBUZZ_LIBRARIES HARFBUZZ_INCLUDE_DIRS)
if(NOT FREETYPE_FOUND)
message(STATUS "freetype2: NO")
else()
message(STATUS "freetype2: YES")
endif()
if(NOT HARFBUZZ_FOUND)
message(STATUS "harfbuzz: NO")
else()
message(STATUS "harfbuzz: YES")
endif()
if(FREETYPE_FOUND AND HARFBUZZ_FOUND)
ocv_define_module(freetype opencv_core opencv_imgproc PRIVATE_REQUIRED ${FREETYPE_LIBRARIES} ${HARFBUZZ_LIBRARIES} WRAP python)
ocv_include_directories(${FREETYPE_INCLUDE_DIRS} ${HARFBUZZ_INCLUDE_DIRS})
else()
ocv_module_disable(freetype)
endif()
```
- Step 3 编译 OpenCV
1. 设置 `OPENCV_EXTRA_MODULES_PATH` 项,填入 opencv-contrib-4.7.0 的目录下的 modules 目录。
2. 勾选 `WITH_FREETYPE` 项,必须先编译 freetype 和 harfbuzz。
3. 如果需要支持 freetype,则需要在 Opencv 的 Cmake 配置中加入 freetype 的相关路径。
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_freetype_step4.png"/>
搜索 harfbuzz,加入 harfbuzz,加入 的相关路径。
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/opencv_freetype_step5.png"/>
完成后,再次在 Cmake 界面,点击 configure 确定没报错后,点击 Generate,最后点击 Open Project,打开 Visual studio,将 Debug 切换为 Release,找到 ALL_BUILD 右键 Build 等待编译完成后, 找到 INSTALL 右键 Build。
注意:如果完成编译包含 FreeType 的 OpenCV,在编译通用 OCR 产线 demo 时,需要在 2.1节 Step 3 配置编译选项时勾选 `USE_FREETYPE` 开启文字渲染功能,并且在运行 demo 时通过 `--vis_font_dir your_ttf_path` 提供相应 ttf 字体文件路径。
编译并运行预测 demo 可以得到如下可视化文本识别结果:
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/paddleocr/deployment/cpp/ocr_res_with_freetype.png"/>
@@ -0,0 +1,9 @@
# C++ Local Deployment
Linux
[C++ Local Deployment for General OCR Pipeline - Linux](./OCR.en.md)
Windows:
[C++ Local Deployment for General OCR Pipeline - Windows](./OCR_windows.en.md)
@@ -0,0 +1,9 @@
# C++ 本地部署
Linux
[通用 OCR 产线 C++ 本地部署 - Linux](./OCR.md)
Windows:
[通用 OCR 产线 C++ 本地部署 - Windows](./OCR_windows.md)