first commit
Some checks are pending
Build/Publish Develop Docs / deploy (push) Waiting to run

This commit is contained in:
2025-07-02 08:57:16 +03:00
commit 56532cc9a9
1901 changed files with 457695 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
# High-Performance Inference
In real-world production environments, many applications have stringent performance requirements for deployment strategies, particularly regarding response speed, to ensure efficient system operation and a smooth user experience. PaddleOCR provides high-performance inference capabilities, allowing users to enhance model inference speed with a single click without worrying about complex configurations or underlying details. Specifically, PaddleOCR's high-performance inference functionality can:
- Automatically select an appropriate inference backend (e.g., Paddle Inference, OpenVINO, ONNX Runtime, TensorRT) based on prior knowledge and configure acceleration strategies (e.g., increasing the number of inference threads, setting FP16 precision inference);
- Automatically convert PaddlePaddle static graph models to ONNX format as needed to leverage better inference backends for acceleration.
This document primarily introduces the installation and usage methods for high-performance inference.
## 1. Prerequisites
### 1.1 Install High-Performance Inference Dependencies
Install the dependencies required for high-performance inference using the PaddleOCR CLI:
```bash
paddleocr install_hpi_deps {device_type}
```
The supported device types are:
- `cpu`: For CPU-only inference. Currently supports Linux systems, x86-64 architecture processors, and Python 3.8-3.12.
- `gpu`: For inference using either CPU or NVIDIA GPU. Currently supports Linux systems, x86-64 architecture processors, and Python 3.8-3.12. If you want to use the full high-performance inference capabilities, you also need to ensure that a compatible version of TensorRT is installed in your environment. Refer to the next subsection for detailed instructions.
Only one type of device dependency should exist in the same environment. For Windows systems, it is currently recommended to install within a Docker container or [WSL](https://learn.microsoft.com/en-us/windows/wsl/install) environment.
**It is recommended to use the official PaddlePaddle Docker image to install high-performance inference dependencies.** The corresponding images for each device type are as follows:
- `cpu`: `ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlepaddle/paddle:3.0.0`
- `gpu`:
- CUDA 11.8: `ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlepaddle/paddle:3.0.0-gpu-cuda11.8-cudnn8.9-trt8.6`
### 1.2 Detailed GPU Environment Instructions
First, ensure that the environment has the required CUDA and cuDNN versions installed. Currently, PaddleOCR only supports CUDA and cuDNN versions compatible with CUDA 11.8 + cuDNN 8.9. Below are the installation instructions for CUDA 11.8 and cuDNN 8.9:
- [Install CUDA 11.8](https://developer.nvidia.com/cuda-11-8-0-download-archive)
- [Install cuDNN 8.9](https://docs.nvidia.com/deeplearning/cudnn/archives/cudnn-890/install-guide/index.html)
If using the official PaddlePaddle image, the CUDA and cuDNN versions in the image already meet the requirements, and no additional installation is needed.
If installing PaddlePaddle via pip, the relevant Python packages for CUDA and cuDNN will typically be installed automatically. In this case, **you still need to install the non-Python-specific CUDA and cuDNN versions.** It is also recommended to install CUDA and cuDNN versions that match the Python package versions in your environment to avoid potential issues caused by coexisting library versions. You can check the versions of the CUDA and cuDNN-related Python packages with the following commands:
```bash
# CUDA-related Python package versions
pip list | grep nvidia-cuda
# cuDNN-related Python package versions
pip list | grep nvidia-cudnn
```
Secondly, it is recommended to ensure that a compatible version of TensorRT is installed in the environment; otherwise, the Paddle Inference TensorRT subgraph engine will be unavailable, and the program may not achieve optimal inference performance. Currently, PaddleOCR only supports TensorRT 8.6.1.6. If using the official PaddlePaddle image, you can install the TensorRT wheel package with the following command:
```bash
python -m pip install /usr/local/TensorRT-*/python/tensorrt-*-cp310-none-linux_x86_64.whl
```
For other environments, refer to the [TensorRT documentation](https://docs.nvidia.com/deeplearning/tensorrt/archives/index.html) to install TensorRT. Here is an example:
```bash
# Download the TensorRT tar file
wget https://developer.nvidia.com/downloads/compute/machine-learning/tensorrt/secure/8.6.1/tars/TensorRT-8.6.1.6.Linux.x86_64-gnu.cuda-11.8.tar.gz
# Extract the TensorRT tar file
tar xvf TensorRT-8.6.1.6.Linux.x86_64-gnu.cuda-11.8.tar.gz
# Install the TensorRT wheel package
python -m pip install TensorRT-8.6.1.6/python/tensorrt-8.6.1-cp310-none-linux_x86_64.whl
# Add the absolute path of the TensorRT `lib` directory to LD_LIBRARY_PATH
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:TensorRT-8.6.1.6/lib"
```
## 2. Executing High-Performance Inference
For the PaddleOCR CLI, specify `--enable_hpi` as `True` to execute high-performance inference. For example:
```bash
paddleocr ocr --enable_hpi True ...
```
For the PaddleOCR Python API, set `enable_hpi` to `True` when initializing the pipeline or module object to enable high-performance inference when calling the inference method. For example:
```python
from paddleocr import PaddleOCR
pipeline = PaddleOCR(enable_hpi=True)
result = pipeline.predict(...)
```
## 3. Notes
1. For some models, the first execution of high-performance inference may take longer to complete the construction of the inference engine. Relevant information about the inference engine will be cached in the model directory after the first construction, and subsequent initializations can reuse the cached content to improve speed.
2. Currently, due to reasons such as not using static graph format models or the presence of unsupported operators, some models may not achieve inference acceleration.
3. During high-performance inference, PaddleOCR automatically handles the conversion of model formats and selects the optimal inference backend whenever possible. Additionally, PaddleOCR supports users specifying ONNX models. For information on converting PaddlePaddle static graph models to ONNX format, refer to [Obtaining ONNX Models](./obtaining_onnx_models.en.md).
4. The high-performance inference capabilities of PaddleOCR rely on PaddleX and its high-performance inference plugins. By passing in a custom PaddleX production line configuration file, you can configure the inference backend and other related settings. Please refer to [Using PaddleX Production Line Configuration Files](../paddleocr_and_paddlex.en.md#3-Using-PaddleX-Pipeline-Configuration-Files) and the [PaddleX High-Performance Inference Guide](https://paddlepaddle.github.io/PaddleX/3.0/en/pipeline_deploy/high_performance_inference.html#22) to learn how to adjust the high-performance inference configurations.

View File

@@ -0,0 +1,91 @@
# 高性能推理
在实际生产环境中许多应用对部署策略的性能指标尤其是响应速度有着较严苛的标准以确保系统的高效运行与用户体验的流畅性。PaddleOCR 提供高性能推理能力让用户无需关注复杂的配置和底层细节一键提升模型的推理速度。具体而言PaddleOCR 的高性能推理功能能够:
- 结合先验知识自动选择合适的推理后端Paddle Inference、OpenVINO、ONNX Runtime、TensorRT等并配置加速策略如增大推理线程数、设置 FP16 精度推理);
- 根据需要自动将飞桨静态图模型转换为 ONNX 格式,以使用更优的推理后端实现加速。
本文档主要介绍高性能推理功能的安装与使用方法。
## 1. 前置条件
## 1.1 安装高性能推理依赖
通过 PaddleOCR CLI 安装高性能推理所需依赖:
```bash
paddleocr install_hpi_deps {设备类型}
```
支持的设备类型包括:
- `cpu`:仅使用 CPU 推理。目前支持 Linux 系统、x86-64 架构处理器、Python 3.8-3.12。
- `gpu`:使用 CPU 或 NVIDIA GPU 推理。目前支持 Linux 系统、x86-64 架构处理器、Python 3.8-3.12。如果希望使用完整的高性能推理功能,还需要确保环境中安装有符合要求的 TensorRT。请查看下一小节的详细说明。
同一环境中只应该存在一种设备类型的依赖。对于 Windows 系统,目前建议在 Docker 容器或者 [WSL](https://learn.microsoft.com/zh-cn/windows/wsl/install) 环境中安装。
**推荐使用飞桨官方 Docker 镜像安装高性能推理依赖。** 各设备类型对应的镜像如下:
- `cpu``ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlepaddle/paddle:3.0.0`
- `gpu`
- CUDA 11.8`ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlepaddle/paddle:3.0.0-gpu-cuda11.8-cudnn8.9-trt8.6`
## 1.2 GPU 环境详细说明
首先,需要确保环境中安装有符合要求的 CUDA 与 cuDNN。目前 PaddleOCR 仅支持与 CUDA 11.8 + cuDNN 8.9 兼容的 CUDA 和 cuDNN版本。以下分别是 CUDA 11.8 和 cuDNN 8.9 的安装说明文档:
- [安装 CUDA 11.8](https://developer.nvidia.com/cuda-11-8-0-download-archive)
- [安装 cuDNN 8.9](https://docs.nvidia.com/deeplearning/cudnn/archives/cudnn-890/install-guide/index.html)
如果使用飞桨官方镜像,则镜像中的 CUDA 和 cuDNN 版本已经是满足要求的,无需额外安装。
如果通过 pip 安装飞桨,通常 CUDA、cuDNN 的相关 Python 包将被自动安装。在这种情况下,**仍需要通过安装非 Python 专用的 CUDA 与 cuDNN**。同时,建议安装的 CUDA 和 cuDNN 版本与环境中存在的 Python 包版本保持一致,以避免不同版本的库共存导致的潜在问题。可以通过如下方式可以查看 CUDA 和 cuDNN 相关 Python 包的版本:
```bash
# CUDA 相关 Python 包版本
pip list | grep nvidia-cuda
# cuDNN 相关 Python 包版本
pip list | grep nvidia-cudnn
```
其次,建议确保环境中安装有符合要求的 TensorRT否则 Paddle Inference TensorRT 子图引擎将不可用,程序可能无法取得最佳推理性能。目前 PaddleOCR 仅支持 TensorRT 8.6.1.6。如果使用飞桨官方镜像,可执行如下命令安装 TensorRT wheel 包:
```bash
python -m pip install /usr/local/TensorRT-*/python/tensorrt-*-cp310-none-linux_x86_64.whl
```
对于其他环境,请参考 [TensorRT 文档](https://docs.nvidia.com/deeplearning/tensorrt/archives/index.html) 安装 TensorRT。示例如下
```bash
# 下载 TensorRT tar 文件
wget https://developer.nvidia.com/downloads/compute/machine-learning/tensorrt/secure/8.6.1/tars/TensorRT-8.6.1.6.Linux.x86_64-gnu.cuda-11.8.tar.gz
# 解压 TensorRT tar 文件
tar xvf TensorRT-8.6.1.6.Linux.x86_64-gnu.cuda-11.8.tar.gz
# 安装 TensorRT wheel 包
python -m pip install TensorRT-8.6.1.6/python/tensorrt-8.6.1-cp310-none-linux_x86_64.whl
# 添加 TensorRT 的 `lib` 目录的绝对路径到 LD_LIBRARY_PATH 中
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:TensorRT-8.6.1.6/lib"
```
## 2. 执行高性能推理
对于 PaddleOCR CLI指定 `--enable_hpi``True` 即可执行高性能推理。例如:
```bash
paddleocr ocr --enable_hpi True ...
```
对于 PaddleOCR Python API在初始化产线对象或者模块对象时设置 `enable_hpi``True` 即可在调用推理方法时执行高性能推理。例如:
```python
from paddleocr import PaddleOCR
pipeline = PaddleOCR(enable_hpi=True)
result = pipeline.predict(...)
```
## 3. 说明
1. 对于部分模型,在首次执行高性能推理时,可能需要花费较长时间完成推理引擎的构建。推理引擎相关信息将在第一次构建完成后被缓存在模型目录,后续可复用缓存中的内容以提升初始化速度。
2. 目前,由于使用的不是静态图格式模型、存在不支持算子等原因,部分模型可能无法获得推理加速。
3. 在进行高性能推理时PaddleOCR 会自动处理模型格式的转换并尽可能选择最优的推理后端。同时PaddleOCR 也支持用户指定 ONNX 模型。有关如何飞桨静态图模型转换为 ONNX 格式,可参考 [获取 ONNX 模型](./obtaining_onnx_models.md)。
4. PaddleOCR 的高性能推理能力依托于 PaddleX 及其高性能推理插件。通过传入自定义 PaddleX 产线配置文件,可以对推理后端等进行配置。请参考 [使用 PaddleX 产线配置文件](../paddleocr_and_paddlex.md#3-使用-paddlex-产线配置文件) 和 [PaddleX 高性能推理指南](https://paddlepaddle.github.io/PaddleX/3.0/pipeline_deploy/high_performance_inference.html#22) 了解如何调整高性能推理配置。

View File

@@ -0,0 +1,312 @@
# PaddleOCR MCP Server
[![PaddleOCR](https://img.shields.io/badge/OCR-PaddleOCR-orange)](https://github.com/PaddlePaddle/PaddleOCR)
[![FastMCP](https://img.shields.io/badge/Built%20with-FastMCP%20v2-blue)](https://gofastmcp.com)
This project provides a lightweight [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) server designed to integrate PaddleOCR capabilities into various LLM applications.
## Key Features
- **Currently Supported Tools**
- **OCR**: Performs text detection and recognition on images and PDF files.
- **PP-StructureV3**: Identifies and extracts text blocks, titles, paragraphs, images, tables, and other layout elements from images or PDF files, converting the input into Markdown documents.
- **Supported Working Modes**
- **Local Python Library**: Runs PaddleOCR pipelines directly on the local machine. This mode requires a suitable local environment and hardware, and is ideal for offline use or privacy-sensitive scenarios.
- **AI Studio Community Service**: Invokes services hosted on the [PaddlePaddle AI Studio Community](https://aistudio.baidu.com/pipeline/mine). This is suitable for quick testing, prototyping, or no-code scenarios.
- **Self-hosted Service**: Invokes the user's self-hosted PaddleOCR services. This mode offers the advantages of serving and high flexibility. It is suitable for scenarios requiring customized service configurations, as well as those with strict data privacy requirements. **Currently, only the basic serving solution is supported.**
### Table of Contents
- [Table of Contents](#table-of-contents)
- [1. Installation](#1-installation)
- [2. Using with Claude for Desktop](#2-using-with-claude-for-desktop)
- [2.1 Quick Start](#21-quick-start)
- [2.2 MCP Host Configuration Details](#22-mcp-host-configuration-details)
- [2.3 Working Modes Explained](#23-working-modes-explained)
- [2.4 Using `uvx`](#24-using-uvx)
- [3. Running the Server](#3-running-the-server)
- [4. Parameter Reference](#4-parameter-reference)
- [5. Known Limitations](#5-known-limitations)
## 1. Installation
This section explains how to install the `paddleocr-mcp` library via pip.
- For the local Python library mode, you need to install both `paddleocr-mcp` and the PaddlePaddle framework along with PaddleOCR, as per the [PaddleOCR installation documentation](../installation.en.md).
- For the AI Studio community service or the self-hosted service modes, if used within MCP hosts like Claude for Desktop, the server can also be run without installation via tools like `uvx`. See [2. Using with Claude for Desktop](#2-using-with-claude-for-desktop) for details.
To install `paddleocr-mcp` using pip:
```bash
# Install the wheel
pip install https://paddle-model-ecology.bj.bcebos.com/paddlex/PaddleX3.0/mcp/paddleocr_mcp/releases/v0.1.0/paddleocr_mcp-0.1.0-py3-none-any.whl
# Or install from source
# git clone https://github.com/PaddlePaddle/PaddleOCR.git
# pip install -e mcp_server
```
To verify successful installation:
```bash
paddleocr_mcp --help
```
If the help message is printed, the installation succeeded. This project depends on the `python-magic` library. If you see the following error:
```
...
ImportError: failed to find libmagic. Check your installation
```
You are likely missing a required native library for python-magic. Please refer to the [official python-magic documentation](https://github.com/ahupp/python-magic?tab=readme-ov-file#installation) for installation instructions.
## 2. Using with Claude for Desktop
This section explains how to use the PaddleOCR MCP server within Claude for Desktop. The steps are also applicable to other MCP hosts with minor adjustments.
### 2.1 Quick Start
1. **Install `paddleocr-mcp`**
Refer to [1. Installation](#1-installation). To avoid dependency conflicts, **it is strongly recommended to install in an isolated virtual environment**.
2. **Install PaddleOCR**
Install the PaddlePaddle framework and PaddleOCR, as per the [PaddleOCR installation documentation](../installation.en.md).
3. **Add MCP Server Configuration**
Locate the `claude_desktop_config.json` configuration file:
- **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
- **Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
- **Linux**: `~/.config/Claude/claude_desktop_config.json`
Edit the file as follows:
```json
{
"mcpServers": {
"paddleocr-ocr": {
"command": "paddleocr_mcp",
"args": [],
"env": {
"PADDLEOCR_MCP_PIPELINE": "OCR",
"PADDLEOCR_MCP_PPOCR_SOURCE": "local"
}
}
}
}
```
**Notes**:
- `PADDLEOCR_MCP_PIPELINE_CONFIG` is optional; if not set, the default pipeline configuration will be used. If you need to adjust the configuration, such as changing the model, please refer to the [PaddleOCR documentation](../paddleocr_and_paddlex.md) to export the pipeline configuration file, and set `PADDLEOCR_MCP_PIPELINE_CONFIG` to the absolute path of this configuration file.
- **Inference Performance Tips**:
If you encounter issues such as long inference time or insufficient memory during use, you may consider adjusting the pipeline configuration according to the following recommendations.
- **OCR Pipeline**: It is recommended to switch to the `mobile` series models. For example, you can modify the pipeline configuration file to use `PP-OCRv5_mobile_det` for detection and `PP-OCRv5_mobile_rec` for recognition.
- **PP-StructureV3 Pipeline**:
- Disable unused features, e.g., set `use_formula_recognition` to `False` to disable formula recognition.
- Use lightweight models, such as replacing the OCR model with the `mobile` version or switching to a lightweight formula recognition model like PP-FormulaNet-S.
The following sample code can be used to obtain the pipeline configuration file, in which most optional features of the PP-StructureV3 pipeline are disabled, and some key models are replaced with lightweight versions.
```python
from paddleocr import PPStructureV3
pipeline = PPStructureV3(
use_doc_orientation_classify=False, # Disable document image orientation classification
use_doc_unwarping=False, # Disable text image unwarping
use_textline_orientation=False, # Disable text line orientation classification
use_formula_recognition=False, # Disable formula recognition
use_seal_recognition=False, # Disable seal text recognition
use_table_recognition=False, # Disable table recognition
use_chart_recognition=False, # Disable chart parsing
# Use lightweight models
text_detection_model_name="PP-OCRv5_mobile_det",
text_recognition_model_name="PP-OCRv5_mobile_rec",
layout_detection_model_name="PP-DocLayout-S",
)
# The configuration file is saved to `PP-StructureV3.yaml`
pipeline.export_paddlex_config_to_yaml("PP-StructureV3.yaml")
```
**Important**:
- If `paddleocr_mcp` is not in your system's `PATH`, set `command` to the absolute path of the executable.
4. **Restart the MCP Host**
Restart Claude for Desktop. The `paddleocr-ocr` tool should now be available in the application.
### 2.2 MCP Host Configuration Details
In the configuration file for Claude for Desktop, you need to define how the MCP server is started. The key fields are as follows:
- `command`: `paddleocr_mcp` (if the executable can be found in the `PATH`) or the absolute path.
- `args`: Configurable command-line arguments, such as `["--verbose"]`. See [4. Parameter Reference](#4-parameter-reference) for details.
- `env`: Configurable environment variables. See [4. Parameter Reference](#4-parameter-reference) for details.
### 2.3 Working Modes Explained
You can configure the MCP server according to your requirements to run in different working modes. The operational procedures vary for different modes, which will be explained in detail below.
#### Mode 1: Local Python Library
See [2.1 Quick Start](#21-quick-start).
#### Mode 2: AI Studio Community Service
1. Install `paddleocr-mcp`.
2. Set up AI Studio community service.
- Visit [PaddlePaddle AI Studio Community](https://aistudio.baidu.com/pipeline/mine) and log in. **Note: A China mainland phone number is required to use this service.** If unavailable, consider other modes.
- Under "PaddleX Pipeline" in the "More" section on the left, click in sequence: [Create Pipeline] - [OCR] - [General OCR] - [Deploy Directly] - [Start Deployment].
- After deployment, obtain your **service base URL** (e.g., `https://xxxxxx.aistudio-hub.baidu.com`).
- Get your **access token** from [this page](https://aistudio.baidu.com/index/accessToken).
3. Refer to the configuration example below to modify the contents of the `claude_desktop_config.json` file.
3. Restart the MCP host.
Configuration example:
```json
{
"mcpServers": {
"paddleocr-ocr": {
"command": "paddleocr_mcp",
"args": [],
"env": {
"PADDLEOCR_MCP_PIPELINE": "OCR",
"PADDLEOCR_MCP_PPOCR_SOURCE": "aistudio",
"PADDLEOCR_MCP_SERVER_URL": "<your-server-url>",
"PADDLEOCR_MCP_AISTUDIO_ACCESS_TOKEN": "<your-access-token>"
}
}
}
}
```
**Notes**:
- Replace `<your-server-url>` with your AI Studio service base URL, e.g., `https://xxxxx.aistudio-hub.baidu.com`. Make sure not to include the endpoint path (such as `/ocr`).
- Replace `<your-access-token>` with your access token.
**Important**:
- Do not expose your access token.
You may also train and deploy custom models on the platform.
#### Mode 3: Self-hosted Service
1. In the environment where you need to run the PaddleOCR inference server, run the inference server as per the [PaddleOCR serving documentation](./serving.en.md).
2. Install `paddleocr-mcp` where the MCP server will run.
3. Refer to the configuration example below to modify the contents of the `claude_desktop_config.json` file.
4. Set `PADDLEOCR_MCP_SERVER_URL` (e.g., `"http://127.0.0.1:8000"`).
5. Restart the MCP host.
Configuration example:
```json
{
"mcpServers": {
"paddleocr-ocr": {
"command": "paddleocr_mcp",
"args": [],
"env": {
"PADDLEOCR_MCP_PIPELINE": "OCR",
"PADDLEOCR_MCP_PPOCR_SOURCE": "self_hosted",
"PADDLEOCR_MCP_SERVER_URL": "<your-server-url>"
}
}
}
}
```
**Note**:
- Replace `<your-server-url>` with your services base URL (e.g., `http://127.0.0.1:8000`).
### 2.4 Using `uvx`
Currently, for both the AI Studio and self-hosted modes, starting the MCP server via `uvx` is also supported. With this approach, manual installation of `paddleocr-mcp` is not required. The main steps are as follows:
1. Install [uv](https://docs.astral.sh/uv/#installation).
2. Modify `claude_desktop_config.json`. Example for self-hosted mode:
```json
{
"mcpServers": {
"paddleocr-ocr": {
"command": "uvx",
"args": [
"--from",
"paddleocr-mcp@https://paddle-model-ecology.bj.bcebos.com/paddlex/PaddleX3.0/mcp/paddleocr_mcp/releases/v0.1.0/paddleocr_mcp-0.1.0-py3-none-any.whl",
"paddleocr_mcp"
],
"env": {
"PADDLEOCR_MCP_PIPELINE": "OCR",
"PADDLEOCR_MCP_PPOCR_SOURCE": "self_hosted",
"PADDLEOCR_MCP_SERVER_URL": "<your-server-url>"
}
}
}
}
```
Due to the different startup methods used, the settings for `command` and `args` in the configuration file differ significantly from those described in [2.1 Quick Start](#21-quick-start). However, the command-line arguments and environment variables (such as `PADDLEOCR_MCP_SERVER_URL`) supported by the MCP service itself can still be set in the same way.
## 3. Running the Server
In addition to MCP hosts like Claude for Desktop, you can also run the PaddleOCR MCP server via the CLI.
To view help:
```bash
paddleocr_mcp --help
```
Example commands:
```bash
# OCR + AI Studio community service + stdio
PADDLEOCR_MCP_AISTUDIO_ACCESS_TOKEN=xxxxxx paddleocr_mcp --pipeline OCR --ppocr_source aistudio --server_url https://xxxxxx.aistudio-hub.baidu.com
# PP-StructureV3 + local Python library + stdio
paddleocr_mcp --pipeline PP-StructureV3 --ppocr_source local
# OCR + self-hosted service + Streamable HTTP
paddleocr_mcp --pipeline OCR --ppocr_source self_hosted --server_url http://127.0.0.1:8080 --http
```
You can find all the supported parameters of the PaddleOCR MCP server in [4. Parameter Reference](#4-parameter-reference).
## 4. Parameter Reference
You can control the MCP server via environment variables or CLI arguments.
| Environment Variable | CLI Argument | Type | Description | Options | Default |
| ------------------------------------- | ------------------------- | ------ | --------------------------------------------------------------------- | ---------------------------------------- | ------------- |
| `PADDLEOCR_MCP_PIPELINE` | `--pipeline` | `str` | Pipeline to run. | `"OCR"`, `"PP-StructureV3"` | `"OCR"` |
| `PADDLEOCR_MCP_PPOCR_SOURCE` | `--ppocr_source` | `str` | Source of PaddleOCR capabilities. | `"local"` (local Python library), `"aistudio"` (AI Studio community service), `"self_hosted"` (self-hosted service) | `"local"` |
| `PADDLEOCR_MCP_SERVER_URL` | `--server_url` | `str` | Base URL for the underlying service (`aistudio` or `self_hosted` mode only). | - | `None` |
| `PADDLEOCR_MCP_AISTUDIO_ACCESS_TOKEN` | `--aistudio_access_token` | `str` | AI Studio access token (`aistudio` mode only). | - | `None` |
| `PADDLEOCR_MCP_TIMEOUT` | `--timeout` | `int` | Read timeout for the underlying requests (seconds). | - | `60` |
| `PADDLEOCR_MCP_DEVICE` | `--device` | `str` | Device for inference (`local` mode only). | - | `None` |
| `PADDLEOCR_MCP_PIPELINE_CONFIG` | `--pipeline_config` | `str` | Path to pipeline config file (`local` mode only). | - | `None` |
| - | `--http` | `bool` | Use Streamable HTTP instead of stdio (for remote/multi-client use). | - | `False` |
| - | `--host` | `str` | Host for the Stremable HTTP mode. | - | `"127.0.0.1"` |
| - | `--port` | `int` | Port for the Streamable HTTP mode. | - | `8000` |
| - | `--verbose` | `bool` | Enable verbose logging for debugging. | - | `False` |
## 5. Known Limitations
- In the local Python library mode, the current tools cannot process PDF document inputs that are Base64 encoded.
- In the local Python library mode, the current tools do not infer the file type based on the model's `file_type` prompt, and may fail to process some complex URLs.
- For the PP-StructureV3 pipeline, if the input file contains images, the returned results may significantly increase token usage. If image content is not needed, you can explicitly exclude it through prompts to reduce resource consumption.

View File

@@ -0,0 +1,308 @@
# PaddleOCR MCP 服务器
[![PaddleOCR](https://img.shields.io/badge/OCR-PaddleOCR-orange)](https://github.com/PaddlePaddle/PaddleOCR)
[![FastMCP](https://img.shields.io/badge/Built%20with-FastMCP%20v2-blue)](https://gofastmcp.com)
本项目提供轻量级的 [Model Context ProtocolMCP](https://modelcontextprotocol.io/introduction) 服务器,旨在将 PaddleOCR 的能力集成到各种大模型应用中。
主要功能如下:
- **当前支持的工具**
- **OCR**:对图像和 PDF 文件进行文本检测与识别。
- **PP-StructureV3**:从图像或 PDF 文件中识别和提取文本块、标题、段落、图片、表格以及其他版面元素,将输入转换为 Markdown 文档。
- **支持运行在如下工作模式**
- **本地 Python 库**:在本机直接运行 PaddleOCR 产线。此模式对本地环境与计算机性能有一定要求,适用于需要离线使用、对数据隐私有严格要求的场景。
- **星河社区服务**:调用托管在 [飞桨星河社区](https://aistudio.baidu.com/pipeline/mine) 的服务。此模式适合快速体验功能、快速验证方案等,也适用于零代码开发场景。
- **自托管服务**:调用用户自托管的 PaddleOCR 服务。此模式具备服务化部署优势及高度灵活性,适用于需要自定义服务配置的场景,同时也适用于对数据隐私有严格要求的场景。**目前暂时只支持基础服务化部署方案。**
### 目录
- [目录](#目录)
- [1. 安装](#1-安装)
- [2. 在 Claude for Desktop 中使用](#2-在-claude-for-desktop-中使用)
- [2.1 快速开始](#21-快速开始)
- [2.2 MCP 主机配置说明](#22-mcp-主机配置说明)
- [2.3 工作模式说明](#23-工作模式说明)
- [2.4 使用 `uvx`](#24-使用-uvx)
- [3. 运行服务器](#3-运行服务器)
- [4. 参数说明](#4-参数说明)
- [5. 已知局限性](#5-已知局限性)
## 1. 安装
本节将介绍如何通过 pip 安装 `paddleocr-mcp` 库。
- 对于本地 Python 库模式,除了安装 `paddleocr-mcp` 外,还需要参考 [PaddleOCR 安装文档](../installation.md) 安装飞桨框架和 PaddleOCR。
- 对于星河社区服务和自托管服务模式,如果希望在 Claude for Desktop 等 MCP 主机中使用,也支持通过 `uvx` 等方式免安装运行服务器。详情请参考 [2. 在 Claude for Desktop 中使用](#2-在-claude-for-desktop-中使用) 中的说明。
使用 pip 安装 `paddleocr-mcp` 库的命令如下:
```bash
# 安装 wheel 包
pip install https://paddle-model-ecology.bj.bcebos.com/paddlex/PaddleX3.0/mcp/paddleocr_mcp/releases/v0.1.0/paddleocr_mcp-0.1.0-py3-none-any.whl
# 或者,从项目源码安装
# git clone https://github.com/PaddlePaddle/PaddleOCR.git
# pip install -e mcp_server
```
可通过以下命令检查是否安装成功:
```bash
paddleocr_mcp --help
```
如果执行上述命令后打印出了帮助信息,则说明安装成功。本项目依赖 python-magic 库。如果在执行上述命令时出现如下错误提示:
```
...
ImportError: failed to find libmagic. Check your installation
```
很可能是因为缺少 python-magic 库所需的底层库。请参考 [python-magic 官方文档](https://github.com/ahupp/python-magic?tab=readme-ov-file#installation) 完成相应依赖库的安装。
## 2. 在 Claude for Desktop 中使用
本节将介绍如何在 Claude for Desktop 中使用 PaddleOCR MCP 服务器。对于其他 MCP 主机,也可参照本节的步骤,并根据实际情况进行相应调整。
### 2.1 快速开始
接下来以 **星河社区服务** 工作模式为例,引导您快速上手。此模式无需在本地安装复杂的依赖,因此比较适合用于快速体验。
1. **安装 `paddleocr-mcp`**
请参考 [1. 安装](#1-安装)。
2. **准备星河社区服务**
- 访问 [飞桨星河社区](https://aistudio.baidu.com/pipeline/mine) 并登录。**请注意,目前星河社区要求用户绑定中国大陆手机号。** 如果您不具备此条件,请考虑使用其他工作模式。
- 在左侧"更多内容"下的 "PaddleX 产线" 部分,依次点击:【创建产线】 - 【OCR】- 【通用OCR】-【直接部署】-【开始部署】。
- 部署成功后,获取您的 **服务基础 URL**(示例:`https://xxxxxx.aistudio-hub.baidu.com`)。
- 在 [此页面](https://aistudio.baidu.com/index/accessToken) 获取您的 **访问令牌**
3. **添加 MCP 服务器配置**
在以下位置之一找到 Claude for Desktop 配置文件:
- **macOS**`~/Library/Application Support/Claude/claude_desktop_config.json`
- **Windows**`%APPDATA%\Claude\claude_desktop_config.json`
- **Linux**`~/.config/Claude/claude_desktop_config.json`
打开 `claude_desktop_config.json` 文件,参考如下示例调整配置,填充到 `claude_desktop_config.json` 中。
```json
{
"mcpServers": {
"paddleocr-ocr": {
"command": "paddleocr_mcp",
"args": [],
"env": {
"PADDLEOCR_MCP_PIPELINE": "OCR",
"PADDLEOCR_MCP_PPOCR_SOURCE": "aistudio",
"PADDLEOCR_MCP_SERVER_URL": "<your-server-url>",
"PADDLEOCR_MCP_AISTUDIO_ACCESS_TOKEN": "<your-access-token>"
}
}
}
}
```
**说明**
- 将 `<your-server-url>` 替换为您的星河社区服务的基础 URL例如 `https://xxxxx.aistudio-hub.baidu.com`,注意不要带有端点路径(如 `/ocr`)。
- 将 `<your-access-token>` 替换为您的访问令牌。
**注意**
- 请勿泄漏您的 **访问令牌**。
- 如果 `paddleocr_mcp` 无法在系统 `PATH` 中找到,请将 `command` 设置为可执行文件的绝对路径。
5. **重启 MCP 主机**
重启 Claude for Desktop。新的 `paddleocr-ocr` 工具现在应该可以在应用中使用了。
### 2.2 MCP 主机配置说明
在 Claude for Desktop 的配置文件中,您需要定义 MCP 服务器的启动方式。关键字段如下:
- `command``paddleocr_mcp`(如果可执行文件可在 `PATH` 中找到)或绝对路径。
- `args`:可配置命令行参数,如 `["--verbose"]`。详见 [4. 参数说明](#4-参数说明)。
- `env`:可配置环境变量。详见 [4. 参数说明](#4-参数说明)。
### 2.3 工作模式说明
您可以根据需求配置 MCP 服务器,使其运行在不同的工作模式。不同工作模式需要的操作流程有所不同,下面将详细介绍。
#### 模式一:本地 Python 库
1. 安装 `paddleocr-mcp`。
2. 安装飞桨框架和 PaddleOCR。为避免依赖冲突**强烈建议在独立的虚拟环境中安装**。
3. 参考下方的配置示例更改 `claude_desktop_config.json` 文件内容。
4. 重启 MCP 主机。
配置示例:
```json
{
"mcpServers": {
"paddleocr-ocr": {
"command": "paddleocr_mcp",
"args": [],
"env": {
"PADDLEOCR_MCP_PIPELINE": "OCR",
"PADDLEOCR_MCP_PPOCR_SOURCE": "local"
}
}
}
}
```
**说明**
- `PADDLEOCR_MCP_PIPELINE_CONFIG` 为可选项,不设置时使用产线默认配置。如需调整配置,例如更换模型,请参考 [PaddleOCR 文档](../paddleocr_and_paddlex.md) 导出产线配置文件,并将 `PADDLEOCR_MCP_PIPELINE_CONFIG` 设置为配置文件的绝对路径。
- **推理性能提示**
如果使用过程中出现推理耗时过长、内存不足等问题,可考虑参考如下建议调整产线配置:
- **OCR 产线**:建议更换 `mobile` 系列模型。例如,您可以在产线配置文件中将检测和识别模型分别修改为 `PP-OCRv5_mobile_det` 和 `PP-OCRv5_mobile_rec`。
- **PP-StructureV3 产线**
- 关闭不需要用到的功能,例如设置 `use_formula_recognition` 为 `False` 以禁用公式识别。
- 使用轻量级的模型,例如将 OCR 模型替换为 `mobile` 版本、换用轻量的公式识别模型 PP-FormulaNet-S 等。
以下示例代码可用于获取产线配置文件,其中关闭了 PP-StructureV3 产线的大部分可选功能,同时将部分关键模型更换为轻量级版本。
```python
from paddleocr import PPStructureV3
pipeline = PPStructureV3(
use_doc_orientation_classify=False, # 禁用文档图像方向分类
use_doc_unwarping=False, # 禁用文本图像矫正
use_textline_orientation=False, # 禁用文本行方向分类
use_formula_recognition=False, # 禁用公式识别
use_seal_recognition=False, # 禁用印章文本识别
use_table_recognition=False, # 禁用表格识别
use_chart_recognition=False, # 禁用图表解析
# 使用轻量级模型
text_detection_model_name="PP-OCRv5_mobile_det",
text_recognition_model_name="PP-OCRv5_mobile_rec",
layout_detection_model_name="PP-DocLayout-S",
)
# 配置文件保存到 `PP-StructureV3.yaml` 中
pipeline.export_paddlex_config_to_yaml("PP-StructureV3.yaml")
```
#### 模式二:星河社区服务
请参考 [2.1 快速开始](#21-快速开始)。
除了使用平台预设的模型方案,您也可以在平台上自行训练并部署自定义模型。
#### 模式三:自托管服务
1. 在需要运行 PaddleOCR 推理服务器的环境中,参考 [PaddleOCR 服务化部署文档](./serving.md) 运行推理服务器。
3. 在需要运行 MCP 服务器的环境中安装 `paddleocr-mcp`。
4. 参考下方的配置示例更改 `claude_desktop_config.json` 文件内容。
5. 将您的服务地址填入 `PADDLEOCR_MCP_SERVER_URL` (例如:`"http://127.0.0.1:8000"`)。
6. 重启 MCP 主机。
配置示例:
```json
{
"mcpServers": {
"paddleocr-ocr": {
"command": "paddleocr_mcp",
"args": [],
"env": {
"PADDLEOCR_MCP_PIPELINE": "OCR",
"PADDLEOCR_MCP_PPOCR_SOURCE": "self_hosted",
"PADDLEOCR_MCP_SERVER_URL": "<your-server-url>"
}
}
}
}
```
**说明**
- 将 `<your-server-url>` 替换为底层服务的基础 URL`http://127.0.0.1:8000`)。
### 2.4 使用 `uvx`
对于星河社区服务和自托管服务模式,目前也支持通过 `uvx` 启动 MCP 服务器。这种方式不需要手动安装 `paddleocr-mcp`。主要步骤如下:
1. 安装 [uv](https://docs.astral.sh/uv/#installation)。
2. 修改 `claude_desktop_config.json` 文件的内容。以自托管服务模式为例:
```json
{
"mcpServers": {
"paddleocr-ocr": {
"command": "uvx",
"args": [
"--from",
"paddleocr-mcp@https://paddle-model-ecology.bj.bcebos.com/paddlex/PaddleX3.0/mcp/paddleocr_mcp/releases/v0.1.0/paddleocr_mcp-0.1.0-py3-none-any.whl",
"paddleocr_mcp"
],
"env": {
"PADDLEOCR_MCP_PIPELINE": "OCR",
"PADDLEOCR_MCP_PPOCR_SOURCE": "self_hosted",
"PADDLEOCR_MCP_SERVER_URL": "<your-server-url>"
}
}
}
}
```
由于使用了不一样的启动方式,配置文件中 `command` 和 `args` 的设置都与 [2.1 快速开始](#21-快速开始) 介绍的方式存在显著不同,但 MCP 服务本身支持的命令行参数与环境变量(如 `PADDLEOCR_MCP_SERVER_URL`)仍然可以以相同的方式设置。
## 3. 运行服务器
除了在 Claude for Desktop 等 MCP 主机中使用外,您也可以通过 CLI 运行 PaddleOCR MCP 服务器。
执行以下命令可以打印帮助信息:
```bash
paddleocr_mcp --help
```
示例命令如下:
```bash
# OCR + 星河社区服务 + stdio
PADDLEOCR_MCP_AISTUDIO_ACCESS_TOKEN=xxxxxx paddleocr_mcp --pipeline OCR --ppocr_source aistudio --server_url https://xxxxxx.aistudio-hub.baidu.com
# PP-StructureV3 + 本地 Python 库 + stdio
paddleocr_mcp --pipeline PP-StructureV3 --ppocr_source local
# OCR + 本地服务 + Streamable HTTP
paddleocr_mcp --pipeline OCR --ppocr_source self_hosted --server_url http://127.0.0.1:8080 --http
```
在 [4. 参数说明](#4-参数说明) 中可以了解 PaddleOCR MCP 服务器支持的全部参数。
## 4. 参数说明
您可以通过环境变量或命令行参数来控制 MCP 服务器的行为。
| 环境变量 | 命令行参数 | 类型 | 描述 | 可选值 | 默认值 |
|:---------|:-----------|:-----|:-----|:-------|:-------|
| `PADDLEOCR_MCP_PIPELINE` | `--pipeline` | `str` | 要运行的产线。 | `"OCR"``"PP-StructureV3"` | `"OCR"` |
| `PADDLEOCR_MCP_PPOCR_SOURCE` | `--ppocr_source` | `str` | PaddleOCR 能力来源。 | `"local"`(本地 Python 库),`"aistudio"`(星河社区服务),`"self_hosted"`(自托管服务) | `"local"` |
| `PADDLEOCR_MCP_SERVER_URL` | `--server_url` | `str` | 底层服务基础 URL`aistudio` 或 `self_hosted` 模式下必需)。 | - | `None` |
| `PADDLEOCR_MCP_AISTUDIO_ACCESS_TOKEN` | `--aistudio_access_token` | `str` | AI Studio 访问令牌(`aistudio` 模式下必需)。 | - | `None` |
| `PADDLEOCR_MCP_TIMEOUT` | `--timeout` | `int` | 底层服务请求的读取超时时间(秒)。 | - | `60` |
| `PADDLEOCR_MCP_DEVICE` | `--device` | `str` | 指定运行推理的设备(仅在 `local` 模式下生效)。 | - | `None` |
| `PADDLEOCR_MCP_PIPELINE_CONFIG` | `--pipeline_config` | `str` | PaddleOCR 产线配置文件路径(仅在 `local` 模式下生效)。 | - | `None` |
| - | `--http` | `bool` | 使用 Streamable HTTP 传输而非 stdio适用于远程部署和多客户端。 | - | `False` |
| - | `--host` | `str` | Streamable HTTP 模式的主机地址。 | - | `"127.0.0.1"` |
| - | `--port` | `int` | Streamable HTTP 模式的端口。 | - | `8000` |
| - | `--verbose` | `bool` | 启用详细日志记录,便于调试。 | - | `False` |
## 5. 已知局限性
- 在本地 Python 库模式下,当前提供的工具无法处理 Base64 编码的 PDF 文档输入。
- 在本地 Python 库模式下,当前提供的工具不会根据模型提示的 `file_type` 推断文件类型,对于一些复杂 URL 可能处理失败。
- 对于 PP-StructureV3 产线,若输入文件中包含图像,返回结果可能会显著增加 token 使用量。若无需图像内容,可通过提示词明确排除,以降低资源消耗。

View File

@@ -0,0 +1,51 @@
# Obtaining ONNX Models
PaddleOCR provides a rich collection of pre-trained models, all stored in PaddlePaddle's static graph format. To use these models in ONNX format during deployment, you can convert them using the Paddle2ONNX plugin provided by PaddleX. For more information about PaddleX and its relationship with PaddleOCR, refer to [Differences and Connections Between PaddleOCR and PaddleX](../paddleocr_and_paddlex.en.md#1-Differences-and-Connections-Between-PaddleOCR-and-PaddleX).
First, install the Paddle2ONNX plugin for PaddleX using the following command via the PaddleX CLI:
```bash
# Windows users need to use the following command to install the dev version of paddlepaddle
# python -m pip install --pre paddlepaddle -i https://www.paddlepaddle.org.cn/packages/nightly/cpu/
paddlex --install paddle2onnx
```
Then, execute the following command to complete the model conversion:
```bash
paddlex \
--paddle2onnx \ # Use the paddle2onnx feature
--paddle_model_dir /your/paddle_model/dir \ # Specify the directory containing the Paddle model
--onnx_model_dir /your/onnx_model/output/dir \ # Specify the output directory for the converted ONNX model
--opset_version 7 # Specify the ONNX opset version to use
```
The parameters are described as follows:
<table>
<thead>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>paddle_model_dir</td>
<td>str</td>
<td>The directory containing the Paddle model.</td>
</tr>
<tr>
<td>onnx_model_dir</td>
<td>str</td>
<td>The output directory for the ONNX model. It can be the same as the Paddle model directory. Defaults to <code>onnx</code>.</td>
</tr>
<tr>
<td>opset_version</td>
<td>int</td>
<td>The ONNX opset version to use. If conversion fails with a lower opset version, a higher version will be automatically selected for conversion. Defaults to <code>7</code>.</td>
</tr>
</tbody>
</table>

View File

@@ -0,0 +1,51 @@
# 获取 ONNX 模型
PaddleOCR 提供了丰富的预训练模型,这些模型均采用飞桨的静态图格式进行存储。若需在部署阶段使用 ONNX 格式的模型,可借助 PaddleX 提供的 Paddle2ONNX 插件进行转换。关于 PaddleX 及其与 PaddleOCR 之间的关系,请参考 [PaddleOCR 与 PaddleX 的区别与联系](../paddleocr_and_paddlex.md#1-paddleocr-与-paddlex-的区别与联系)。
首先,执行如下命令,通过 PaddleX CLI 安装 PaddleX 的 Paddle2ONNX 插件:
```bash
# Windows 用户需使用以下命令安装 paddlepaddle dev版本
# python -m pip install --pre paddlepaddle -i https://www.paddlepaddle.org.cn/packages/nightly/cpu/
paddlex --install paddle2onnx
```
然后,执行如下命令完成模型转换:
```bash
paddlex \
--paddle2onnx \ # 使用paddle2onnx功能
--paddle_model_dir /your/paddle_model/dir \ # 指定 Paddle 模型所在的目录
--onnx_model_dir /your/onnx_model/output/dir \ # 指定转换后 ONNX 模型的输出目录
--opset_version 7 # 指定要使用的 ONNX opset 版本
```
参数说明如下:
<table>
<thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>paddle_model_dir</td>
<td>str</td>
<td>包含 Paddle 模型的目录。</td>
</tr>
<tr>
<td>onnx_model_dir</td>
<td>str</td>
<td>ONNX 模型的输出目录,可以与 Paddle 模型目录相同。默认为 <code>onnx</code>。</td>
</tr>
<tr>
<td>opset_version</td>
<td>int</td>
<td>使用的 ONNX opset 版本。当使用低版本 opset 无法完成转换时,将自动选择更高版本的 opset 进行转换。默认为 <code>7</code>。</td>
</tr>
</tbody>
</table>

View File

@@ -0,0 +1,475 @@
# OCR On-Device Deployment Demo Usage Guide
- [Quick Start](#quick-start)
- [Environment Preparation](#environment-preparation)
- [Deployment Steps](#deployment-steps)
- [Code Introduction](#code-introduction)
- [Project Explanation](#project-explanation)
- [Advanced Usage](#advanced-usage)
- [Update Prediction Library](#update-prediction-library)
- [Convert NB Model](#convert-nb-model)
- [Update Model, Label File, and Prediction Image](#update-model-label-file-and-prediction-image)
- [Update Model](#update-model)
- [Update Label File](#update-label-file)
- [Update Prediction Image](#update-prediction-image)
- [Update Input/Output Preprocessing](#update-inputoutput-preprocessing)
This guide mainly introduces how to run the PaddleX on-device deployment demo for OCR text recognition on an Android shell.
The following OCR models are supported in this guide:
- PP-OCRv3_mobile (cpu)
- PP-OCRv4_mobile (cpu)
- PP-OCRv5_mobile (cpu)
## Quick Start
### Environment Preparation
1. Install the CMAKE compilation tool in your local environment and download an NDK package for your current system from the [Android NDK official website](https://developer.android.google.cn/ndk/downloads). For example, if developing on a Mac, download the NDK package for the Mac platform from the Android NDK official website.
**Environment Requirements**
- `CMake >= 3.10` (the minimum version has not been verified; 3.20 or above is recommended)
- `Android NDK >= r17c` (the minimum version has not been verified; r20b or above is recommended)
**Test Environment Used in This Guide**:
- `cmake == 3.20.0`
- `android-ndk == r20b`
2. Prepare an Android phone and enable USB debugging mode. Method: `Phone Settings -> Find Developer Options -> Enable Developer Options and USB Debugging Mode`
3. Install the ADB tool on your computer for debugging. The installation methods for ADB are as follows:
3.1. Install ADB on a Mac:
```shell
brew cask install android-platform-tools
```
3.2. Install ADB on Linux:
```shell
sudo apt update
sudo apt install -y wget adb
```
3.3. Install ADB on Windows:
For installation on Windows, download and install the ADB software package from Google's Android platform: [Link](https://developer.android.com/studio)
Open a terminal, connect your phone to the computer, and enter the following command in the terminal:
```shell
adb devices
```
If there is a `device` output, the installation is successful.
```shell
List of devices attached
744be294 device
```
### Material Preparation
1. Clone the `feature/paddle-x` branch of the `Paddle-Lite-Demo` repository to the `PaddleX-Lite-Deploy` directory.
```shell
git clone -b feature/paddle-x https://github.com/PaddlePaddle/Paddle-Lite-Demo.git PaddleX-Lite-Deploy
```
2. Fill out the [questionnaire](https://paddle.wjx.cn/vm/eaaBo0H.aspx#) to download the compressed package. Place the compressed package in the specified extraction directory, switch to the specified extraction directory, and execute the extraction command.
```shell
# 1. Switch to the specified extraction directory
cd PaddleX-Lite-Deploy/ocr/android/shell/ppocr_demo
# 2. Execute the extraction command
unzip ocr.zip
```
### Deployment Steps
1. Switch the working directory to `PaddleX-Lite-Deploy/libs` and run the `download.sh` script to download the required Paddle Lite prediction library. This step only needs to be executed once to support each demo.
2. Switch the working directory to `PaddleX-Lite-Deploy/ocr/assets` and run the `download.sh` script to download the [paddle_lite_opt tool](https://www.paddlepaddle.org.cn/lite/v2.10/user_guides/model_optimize_tool.html)-optimized NB model file, prediction images, dictionary files, and other materials.
3. Switch the working directory to `PaddleX-Lite-Deploy/ocr/android/shell/cxx/ppocr_demo` and run the `build.sh` script to complete the compilation of the executable file.
4. Switch the working directory to `PaddleX-Lite-Deploy/ocr/android/shell/cxx/ppocr_demo` and run the `run.sh` script to complete the on-device prediction.
**Notes**:
- Before running the `build.sh` script, change the path specified by `NDK_ROOT` to the actual installation path of NDK.
- On Windows systems, you can use Git Bash to execute the deployment steps.
- If compiling on a Windows system, set `CMAKE_SYSTEM_NAME` to `windows` in `CMakeLists.txt`.
- If compiling on a Mac system, set `CMAKE_SYSTEM_NAME` to `darwin` in `CMakeLists.txt`.
- Keep the ADB connection active when running the `run.sh` script.
- The `download.sh` and `run.sh` scripts support passing parameters to specify the model. If no model is specified, the `PP-OCRv5_mobile` model is used by default. The following models are currently supported:
- `PP-OCRv3_mobile`
- `PP-OCRv4_mobile`
- `PP-OCRv5_mobile`
Here is an example of the actual operation:
```shell
# 1. Download the required Paddle Lite prediction library
cd PaddleX-Lite-Deploy/libs
sh download.sh
# 2. Download the paddle_lite_opt tool-optimized NB model file, prediction images, dictionary files, and other materials
cd ../ocr/assets
sh download.sh PP-OCRv5_mobile
# 3. Complete the compilation of the executable file
cd ../android/shell/ppocr_demo
sh build.sh
# 4. Prediction
sh run.sh PP-OCRv5_mobile
```
The output is as follows:
```text
The detection visualized image saved in ./test_img_result.jpg
0 纯臻营养护发素 0.998541
1 产品信息/参数 0.999094
2 (45元/每公斤100公斤起订 0.948841
3 每瓶22元1000瓶起订) 0.961245
4 【品牌】:代加工方式/OEMODM 0.970401
5 【品名】:纯臻营养护发素 0.977496
6 ODMOEM 0.955396
7 【产品编号】YM-X-3011 0.977864
8 【净含量】220ml 0.970538
9 【适用人群】:适合所有肤质 0.995907
10 【主要成分】:鲸蜡硬脂醇、燕麦β-葡聚 0.975813
11 糖、椰油酰胺丙基甜菜碱、泛醌 0.964397
12 (成品包材) 0.97298
13 【主要功能】:可紧致头发磷层,从而达到 0.989097
14 即时持久改善头发光泽的效果,给干燥的头 0.990088
15 发足够的滋养 0.998037
```
![Prediction Result](https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/pipeline_deploy/edge_PP-OCRv5_mobile.jpg)
## Code Introduction
```
.
├── ...
├── ocr
│ ├── ...
│ ├── android
│ │ ├── ...
│ │ └── shell
│ │ └── ppocr_demo
│ │ ├── src # Contains prediction code
│ │ | ├── cls_process.cc # Full inference process for orientation classifier, including preprocessing, prediction, and postprocessing
│ │ | ├── rec_process.cc # Full inference process for recognition model CRNN, including preprocessing, prediction, and postprocessing
│ │ | ├── det_process.cc # Full inference process for detection model CRNN, including preprocessing, prediction, and postprocessing
│ │ | ├── det_post_process.cc # Postprocessing file for detection model DB
│ │ | ├── pipeline.cc # Full inference process code for OCR text recognition demo
│ │ | └── MakeFile # MakeFile file for prediction code
│ │ |
│ │ ├── CMakeLists.txt # CMake file that defines the compilation method for the executable
│ │ ├── README.md
│ │ ├── build.sh # Used for compiling the executable
│ │ └── run.sh # Used for prediction
│ └── assets # Stores models, test images, label files, and config files
│ ├── images # Stores test images
│ ├── labels # Stores dictionary files (see remarks below for details)
│ ├── models # Stores nb models
│ ├── config.txt
│ └── download.sh # Download script for paddle_lite_opt tool-optimized models
└── libs # Stores prediction libraries and OpenCV libraries for different platforms.
├── ...
└── download.sh # Download script for Paddle Lite prediction libraries and OpenCV libraries
```
**Remarks**:
- The `PaddleX-Lite-Deploy/ocr/assets/labels/` directory contains the dictionary files `ppocr_keys_v1.txt` for PP-OCRv3 and PP-OCRv4 models, and `ppocr_keys_ocrv5.txt` for the PP-OCRv5 model. The appropriate dictionary file is automatically selected during inference based on the model name, so no manual intervention is required.
- If you are using an English/numeric or other language model, you need to replace it with the corresponding language dictionary. The PaddleOCR repository provides [some dictionary files](https://github.com/PaddlePaddle/PaddleOCR/tree/release/2.3/ppocr/utils).
```shell
# Parameters of the executable in run.sh script:
adb shell "cd ${ppocr_demo_path} \
&& chmod +x ./ppocr_demo \
&& export LD_LIBRARY_PATH=${ppocr_demo_path}:${LD_LIBRARY_PATH} \
&& ./ppocr_demo \
\"./models/${MODEL_NAME}_det.nb\" \
\"./models/${MODEL_NAME}_rec.nb\" \
./models/${CLS_MODEL_FILE} \
./images/test.jpg \
./test_img_result.jpg \
./labels/${LABEL_FILE} \
./config.txt"
First parameter: ppocr_demo executable
Second parameter: ./models/${MODEL_NAME}_det.nb Detection model .nb file
Third parameter: ./models/${MODEL_NAME}_rec.nb Recognition model .nb file
Fourth parameter: ./models/${CLS_MODEL_FILE} Text line orientation classification model .nb file (automatically selected based on model name by default)
Fifth parameter: ./images/test.jpg Test image
Sixth parameter: ./test_img_result.jpg Result save file
Seventh parameter: ./labels/${LABEL_FILE} Label file (automatically selected based on model name by default)
Eighth parameter: ./config.txt Configuration file containing hyperparameters for the detection and classification models
```
```shell
# List of Specific Parameters in config.txt:
max_side_len 960 # When the width or height of the input image is greater than 960, the image is scaled proportionally so that the longest side of the image is 960.
det_db_thresh 0.3 # Used to filter the binarized images predicted by DB; setting it to 0.3 has no significant impact on the results.
det_db_box_thresh 0.5 # Threshold for filtering boxes in the DB post-processing; if there are missing boxes in detection, you may reduce this value.
det_db_unclip_ratio 1.6 # Represents the compactness of the text box; the smaller the value, the closer the text box is to the text.
use_direction_classify 0 # Whether to use a direction classifier: 0 means not using it, 1 means using it.
```
## Engineering Details
The OCR text recognition demo accomplishes the OCR text recognition function using three models collaboratively. First, the input image undergoes detection processing via the `${MODEL_NAME}_det.nb` model, followed by text direction classification using the `ch_ppocr_mobile_v2.0_cls_slim_opt.nb` model, and finally, text recognition with the `${MODEL_NAME}_rec.nb` model.
1. `pipeline.cc`: Full-process prediction code for the OCR text recognition demo
This file handles the entire process control for serial inference of the three models, including scheduling for the entire processing flow.
- The `Pipeline::Pipeline(...)` method initializes the three model class constructors, accomplishes model loading, thread count, core binding, and predictor creation.
- The `Pipeline::Process(...)` method manages the entire process control for serial inference of the three models.
2. `cls_process.cc`: Prediction file for the direction classifier
This file handles the preprocessing, prediction, and postprocessing for the direction classifier.
- The `ClsPredictor::ClsPredictor()` method initializes model loading, thread count, core binding, and predictor creation.
- The `ClsPredictor::Preprocess()` method handles model preprocessing.
- The `ClsPredictor::Postprocess()` method handles model postprocessing.
3. `rec_process.cc`: Prediction file for the CRNN recognition model
This file handles the preprocessing, prediction, and postprocessing for the CRNN recognition model.
- The `RecPredictor::RecPredictor()` method initializes model loading, thread count, core binding, and predictor creation.
- The `RecPredictor::Preprocess()` method handles model preprocessing.
- The `RecPredictor::Postprocess()` method handles model postprocessing.
4. `det_process.cc`: Prediction file for the DB detection model
This file handles the preprocessing, prediction, and postprocessing for the DB detection model.
- The `DetPredictor::DetPredictor()` method initializes model loading, thread count, core binding, and predictor creation.
- The `DetPredictor::Preprocess()` method handles model preprocessing.
- The `DetPredictor::Postprocess()` method handles model postprocessing.
5. `db_post_process`: Postprocessing functions for the DB detection model, including calls to the clipper library
This file implements third-party library calls and other postprocessing methods for the DB detection model.
- The `std::vector<std::vector<std::vector<int>>> BoxesFromBitmap(...)` method retrieves detection boxes from a Bitmap.
- The `std::vector<std::vector<std::vector<int>>> FilterTagDetRes(...)` method retrieves target box positions based on recognition results.
## Advanced Usage
If the quick start section does not meet your needs, refer to this section for custom modifications to the demo.
This section mainly includes four parts:
- Updating the prediction library;
- Converting `.nb` models;
- Updating models, label files, and prediction images;
- Updating input/output preprocessing.
### Updating the Prediction Library
The prediction library used in this guide is the latest version (214rc), and manual updates are not recommended.
If you need to use a different version, follow these steps to update the prediction library:
* Paddle Lite project: https://github.com/PaddlePaddle/Paddle-Lite
* Refer to the [Paddle Lite Source Code Compilation Documentation](https://www.paddlepaddle.org.cn/lite/develop/source_compile/compile_env.html) to compile the Android prediction library.
* The final compilation output is located in `build.lite.xxx.xxx.xxx` under `inference_lite_lib.xxx.xxx`.
* Replace the C++ library:
* Header files:
Replace the `PaddleX-Lite-Deploy/libs/android/cxx/include` folder in the demo with the generated `build.lite.android.xxx.gcc/inference_lite_lib.android.xxx/cxx/include` folder.
* armeabi-v7a:
Replace the `PaddleX-Lite-Deploy/libs/android/cxx/libs/armeabi-v7a/libpaddle_lite_api_shared.so` library in the demo with the generated `build.lite.android.armv7.gcc/inference_lite_lib.android.armv7/cxx/libs/libpaddle_lite_api_shared.so` library.
* arm64-v8a:
Replace the `PaddleX-Lite-Deploy/libs/android/cxx/libs/arm64-v8a/libpaddle_lite_api_shared.so` library in the demo with the generated `build.lite.android.armv8.gcc/inference_lite_lib.android.armv8/cxx/libs/libpaddle_lite_api_shared.so` library.
### Converting .nb Models
If you want to use your own trained models, follow the process below to obtain `.nb` models.
#### Terminal Command Method (Supports Mac/Ubuntu)
1. Navigate to the [release interface](https://github.com/PaddlePaddle/Paddle-Lite/releases) of the Paddle-Lite GitHub repository and download the corresponding conversion tool, opt, for the desired version (the latest version is recommended).
2. After downloading the opt tool, execute the following command (using the 2.14rc version of the linux_x86 opt tool to convert the PP-OCRv5_mobile_det model as an example):
```bash
./opt_linux_x86 \
--model_file=PP-OCRv5_mobile_det/inference.pdmodel \
--param_file=PP-OCRv5_mobile_det/inference.pdiparams \
--optimize_out=PP-OCRv5_mobile_det \
--valid_targets=arm
```
For detailed instructions on converting `.nb` models using the terminal command method, refer to the [Using the Executable opt](https://www.paddlepaddle.org.cn/lite/v2.12/user_guides/opt/opt_bin.html) section in the Paddle-Lite repository.
#### Python Script Method (Supports Windows/Mac/Ubuntu)
1. Install the latest version of the paddlelite wheel package.
```bash
pip install --pre paddlelite
```
2. Use the Python script to convert the model. Below is an example code snippet for converting the PP-OCRv5_mobile_det model:
```python
from paddlelite.lite import Opt
# 1. Create an Opt instance
opt = Opt()
# 2. Specify the input model paths
opt.set_model_file("./PP-OCRv5_mobile_det/inference.pdmodel")
opt.set_param_file("./PP-OCRv5_mobile_det/inference.pdiparams")
# 3. Specify the target platform for optimization
opt.set_valid_places("arm")
# 4. Specify the output path for the optimized model
opt.set_optimize_out("./PP-OCRv5_mobile_det")
# 5. Execute model optimization
opt.run()
```
For detailed instructions on converting `.nb` models using the Python script method, refer to the [Python Script opt Usage](https://www.paddlepaddle.org.cn/lite/v2.12/api_reference/python_api/opt.html) section in the Paddle-Lite repository.
**Notes**
- For detailed information about the model optimization tool `opt`, refer to Paddle-Lite's [Model Optimization Tool opt](https://www.paddlepaddle.org.cn/lite/v2.12/user_guides/model_optimize_tool.html).
- Currently, only static graph models in `.pdmodel` format can be converted to `.nb` format.
### Updating Models, Label Files, and Prediction Images
#### Updating Models
This guide has only validated the `PP-OCRv3_mobile`, `PP-OCRv4_mobile`, and `PP-OCRv5_mobile` models. Other models may not be compatible.
If you fine-tune the `PP-OCRv5_mobile` model and generate a new model named `PP-OCRv5_mobile_ft`, follow these steps to replace the original model with your fine-tuned model:
1. Place the `.nb` models of `PP-OCRv5_mobile_ft` into the directory `PaddleX-Lite-Deploy/ocr/assets/models/`. The resulting file structure should be:
```text
.
├── ocr
│ ├── ...
│ └── assets
│ ├── models
│ │ ├── ...
│ │ ├── PP-OCRv5_mobile_ft_det.nb
│ │ └── PP-OCRv5_mobile_ft_rec.nb
│ └── ...
└── ...
```
2. Add the model name to the `MODEL_LIST` in the `run.sh` script.
```shell
MODEL_LIST="PP-OCRv3_mobile PP-OCRv4_mobile PP-OCRv5_mobile PP-OCRv5_mobile_ft" # Models are separated by spaces
```
3. Specify the model directory name when running the `run.sh` script.
```shell
sh run.sh PP-OCRv5_mobile_ft
```
**Notes**:
- If the input Tensor, Shape, or Dtype of the model is updated:
- For the text direction classifier model, update the `ClsPredictor::Preprocess` function in `ppocr_demo/src/cls_process.cc`.
- For the detection model, update the `DetPredictor::Preprocess` function in `ppocr_demo/src/det_process.cc`.
- For the recognition model, update the `RecPredictor::Preprocess` function in `ppocr_demo/src/rec_process.cc`.
- If the output Tensor or Dtype of the model is updated:
- For the text direction classifier model, update the `ClsPredictor::Postprocess` function in `ppocr_demo/src/cls_process.cc`.
- For the detection model, update the `DetPredictor::Postprocess` function in `ppocr_demo/src/det_process.cc`.
- For the recognition model, update the `RecPredictor::Postprocess` function in `ppocr_demo/src/rec_process.cc`.
#### Updating Label Files
To update the label file, place the new label file in the directory `PaddleX-Lite-Deploy/ocr/assets/labels/` and update the execution command in `PaddleX-Lite-Deploy/ocr/android/shell/ppocr_demo/run.sh` following the model update method.
For example, to update to `new_labels.txt`:
```shell
# File: `PaddleX-Lite-Deploy/ocr/android/shell/ppocr_demo/run.sh`
# Original command
adb shell "cd ${ppocr_demo_path} \
&& chmod +x ./ppocr_demo \
&& export LD_LIBRARY_PATH=${ppocr_demo_path}:${LD_LIBRARY_PATH} \
&& ./ppocr_demo \
\"./models/${MODEL_NAME}_det.nb\" \
\"./models/${MODEL_NAME}_rec.nb\" \
./models/${CLS_MODEL_FILE} \
./images/test.jpg \
./test_img_result.jpg \
./labels/${LABEL_FILE} \
./config.txt"
# Updated command
adb shell "cd ${ppocr_demo_path} \
&& chmod +x ./ppocr_demo \
&& export LD_LIBRARY_PATH=${ppocr_demo_path}:${LD_LIBRARY_PATH} \
&& ./ppocr_demo \
\"./models/${MODEL_NAME}_det.nb\" \
\"./models/${MODEL_NAME}_rec.nb\" \
./models/${CLS_MODEL_FILE} \
./images/test.jpg \
./test_img_result.jpg \
./labels/new_labels.txt \
./config.txt"
```
#### Updating Prediction Images
If you need to update the prediction images, place the updated images in the `PaddleX-Lite-Deploy/ocr/assets/images/` directory and update the execution command in the `PaddleX-Lite-Deploy/ocr/android/shell/ppocr_demo/run.sh` file.
Here is an example of updating to `new_pics.jpg`:
```shell
# File: `PaddleX-Lite-Deploy/ocr/android/shell/ppocr_demo/run.sh`
## Original command
adb shell "cd ${ppocr_demo_path} \
&& chmod +x ./ppocr_demo \
&& export LD_LIBRARY_PATH=${ppocr_demo_path}:${LD_LIBRARY_PATH} \
&& ./ppocr_demo \
\"./models/${MODEL_NAME}_det.nb\" \
\"./models/${MODEL_NAME}_rec.nb\" \
./models/${CLS_MODEL_FILE} \
./images/test.jpg \
./test_img_result.jpg \
./labels/${LABEL_FILE} \
./config.txt"
# Updated command
adb shell "cd ${ppocr_demo_path} \
&& chmod +x ./ppocr_demo \
&& export LD_LIBRARY_PATH=${ppocr_demo_path}:${LD_LIBRARY_PATH} \
&& ./ppocr_demo \
\"./models/${MODEL_NAME}_det.nb\" \
\"./models/${MODEL_NAME}_rec.nb\" \
./models/${CLS_MODEL_FILE} \
./images/new_pics.jpg \
./test_img_result.jpg \
./labels/${LABEL_FILE} \
./config.txt"
```
### Updating Input/Output Preprocessing
- Updating Input Preprocessing
- For the text direction classifier model, update the `ClsPredictor::Preprocess` function in `ppocr_demo/src/cls_process.cc`.
- For the detection model, update the `DetPredictor::Preprocess` function in `ppocr_demo/src/det_process.cc`.
- For the recognition model, update the `RecPredictor::Preprocess` function in `ppocr_demo/src/rec_process.cc`.
- Updating Output Preprocessing
- For the text direction classifier model, update the `ClsPredictor::Postprocess` function in `ppocr_demo/src/cls_process.cc`.
- For the detection model, update the `DetPredictor::Postprocess` function in `ppocr_demo/src/det_process.cc`.
- For the recognition model, update the `RecPredictor::Postprocess` function in `ppocr_demo/src/rec_process.cc`.

View File

@@ -0,0 +1,475 @@
# OCR 端侧部署 demo 使用指南
- [快速开始](#快速开始)
- [环境准备](#环境准备)
- [部署步骤](#部署步骤)
- [代码介绍](#代码介绍)
- [工程详解](#工程详解)
- [进阶使用](#进阶使用)
- [更新预测库](#更新预测库)
- [转换 nb 模型](#转换-nb-模型)
- [更新模型、标签文件和预测图片](#更新模型标签文件和预测图片)
- [更新模型](#更新模型)
- [更新标签文件](#更新标签文件)
- [更新预测图片](#更新预测图片)
- [更新输入/输出预处理](#更新输入输出预处理)
本指南主要介绍 PaddleX 端侧部署——OCR文字识别 demo 在 Android shell 上的运行方法。
本指南适配了以下 OCR 模型:
- PP-OCRv3_mobilecpu
- PP-OCRv4_mobilecpu
- PP-OCRv5_mobilecpu
## 快速开始
### 环境准备
1. 在本地环境安装好 CMAKE 编译工具,并在 [Android NDK 官网](https://developer.android.google.cn/ndk/downloads)下载当前系统的某个版本的 NDK 软件包。例如,在 Mac 上开发,需要在 Android NDK 官网下载 Mac 平台的 NDK 软件包
**环境要求**
- `CMake >= 3.10`(最低版本未经验证,推荐 3.20 及以上)
- `Android NDK >= r17c`(最低版本未经验证,推荐 r20b 及以上)
**本指南所使用的测试环境:**
- `cmake == 3.20.0`
- `android-ndk == r20b`
2. 准备一部 Android 手机,并开启 USB 调试模式。开启方法: `手机设置 -> 查找开发者选项 -> 打开开发者选项和 USB 调试模式`
3. 电脑上安装 ADB 工具用于调试。ADB 安装方式如下:
3.1. Mac 电脑安装 ADB:
```shell
brew cask install android-platform-tools
```
3.2. Linux 安装 ADB
```shell
sudo apt update
sudo apt install -y wget adb
```
3.3. Windows 安装 ADB
win 上安装需要去谷歌的安卓平台下载 ADB 软件包进行安装:[链接](https://developer.android.com/studio)
打开终端,手机连接电脑,在终端中输入
```shell
adb devices
```
如果有 device 输出,则表示安装成功。
```shell
List of devices attached
744be294 device
```
### 物料准备
1. 克隆 `Paddle-Lite-Demo` 仓库的 `feature/paddle-x` 分支到 `PaddleX-Lite-Deploy` 目录。
```shell
git clone -b feature/paddle-x https://github.com/PaddlePaddle/Paddle-Lite-Demo.git PaddleX-Lite-Deploy
```
2. 填写 [问卷](https://paddle.wjx.cn/vm/eaaBo0H.aspx#) 下载压缩包,将压缩包放到指定解压目录,切换到指定解压目录后执行解压命令。
```shell
# 1. 切换到指定解压目录
cd PaddleX-Lite-Deploy/ocr/android/shell/ppocr_demo
# 2. 执行解压命令
unzip ocr.zip
```
### 部署步骤
1. 将工作目录切换到 `PaddleX-Lite-Deploy/libs` 目录,运行 `download.sh` 脚本,下载需要的 Paddle Lite 预测库。此步骤只需执行一次,即可支持每个 demo 使用。
2. 将工作目录切换到 `PaddleX-Lite-Deploy/ocr/assets` 目录,运行 `download.sh` 脚本,下载 [paddle_lite_opt 工具](https://www.paddlepaddle.org.cn/lite/v2.10/user_guides/model_optimize_tool.html) 优化后的 nb 模型文件及预测图片、字典文件等物料。
3. 将工作目录切换到 `PaddleX-Lite-Deploy/ocr/android/shell/cxx/ppocr_demo` 目录,运行 `build.sh` 脚本,完成可执行文件的编译。
4. 将工作目录切换到 `PaddleX-Lite-Deploy/ocr/android/shell/cxx/ppocr_demo`,运行 `run.sh` 脚本,完成在端侧的预测。
**注意事项:**
- 在运行 `build.sh` 脚本前,需要更改 `NDK_ROOT` 指定的路径为实际安装的 NDK 路径。
- 在 Windows 系统上可以使用 Git Bash 执行部署步骤。
- 若在 Windows 系统上编译,需要将 `CMakeLists.txt` 中的 `CMAKE_SYSTEM_NAME` 设置为 `windows`。
- 若在 Mac 系统上编译,需要将 `CMakeLists.txt` 中的 `CMAKE_SYSTEM_NAME` 设置为 `darwin`。
- 在运行 `run.sh` 脚本时需保持 ADB 连接。
- `download.sh` 和 `run.sh` 支持传入参数来指定模型,若不指定则默认使用 `PP-OCRv5_mobile` 模型。目前适配了以下模型:
- `PP-OCRv3_mobile`
- `PP-OCRv4_mobile`
- `PP-OCRv5_mobile`
以下为实际操作时的示例:
```shell
# 1. 下载需要的 Paddle Lite 预测库
cd PaddleX-Lite-Deploy/libs
sh download.sh
# 2. 下载 paddle_lite_opt 工具优化后的 nb 模型文件及预测图片、字典文件等物料
cd ../ocr/assets
sh download.sh PP-OCRv5_mobile
# 3. 完成可执行文件的编译
cd ../android/shell/ppocr_demo
sh build.sh
# 4. 预测
sh run.sh PP-OCRv5_mobile
```
运行结果如下所示:
```text
The detection visualized image saved in ./test_img_result.jpg
0 纯臻营养护发素 0.998541
1 产品信息/参数 0.999094
2 (45元/每公斤100公斤起订 0.948841
3 每瓶22元1000瓶起订) 0.961245
4 【品牌】:代加工方式/OEMODM 0.970401
5 【品名】:纯臻营养护发素 0.977496
6 ODMOEM 0.955396
7 【产品编号】YM-X-3011 0.977864
8 【净含量】220ml 0.970538
9 【适用人群】:适合所有肤质 0.995907
10 【主要成分】:鲸蜡硬脂醇、燕麦β-葡聚 0.975813
11 糖、椰油酰胺丙基甜菜碱、泛醌 0.964397
12 (成品包材) 0.97298
13 【主要功能】:可紧致头发磷层,从而达到 0.989097
14 即时持久改善头发光泽的效果,给干燥的头 0.990088
15 发足够的滋养 0.998037
```
![预测结果](https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/pipeline_deploy/edge_PP-OCRv5_mobile.jpg)
## 代码介绍
```
.
├── ...
├── ocr
│ ├── ...
│ ├── android
│ │ ├── ...
│ │ └── shell
│ │ └── ppocr_demo
│ │ ├── src # 存放预测代码
│ │ | ├── cls_process.cc # 方向分类器的推理全流程,包含预处理、预测和后处理三部分
│ │ | ├── rec_process.cc # 识别模型 CRNN 的推理全流程,包含预处理、预测和后处理三部分
│ │ | ├── det_process.cc # 检测模型 CRNN 的推理全流程,包含预处理、预测和后处理三部分
│ │ | ├── det_post_process.cc # 检测模型 DB 的后处理文件
│ │ | ├── pipeline.cc # OCR 文字识别 demo 推理全流程代码
│ │ | └── MakeFile # 预测代码的 MakeFile 文件
│ │ |
│ │ ├── CMakeLists.txt # CMake 文件,约束可执行文件的编译方法
│ │ ├── README.md
│ │ ├── build.sh # 用于可执行文件的编译
│ │ └── run.sh # 用于预测
│ └── assets # 存放模型、测试图片、标签文件、config 文件
│ ├── images # 存放测试图片
│ ├── labels # 存放字典文件,更多详情可参考下文备注
│ ├── models # 存放 nb 模型
│ ├── config.txt
│ └── download.sh # 下载脚本,用于下载 paddle_lite_opt 工具优化后的模型
└── libs # 存放不同端的预测库和 OpenCV 库。
├── ...
└── download.sh # 下载脚本,用于下载 Paddle Lite 预测库和 OpenCV 库
```
**备注:**
- `PaddleX-Lite-Deploy/ocr/assets/labels/` 目录下存放了 PP-OCRv3、PP-OCRv4 模型的字典文件 `ppocr_keys_v1.txt` 以及 PP-OCRv5 模型的字典文件 `ppocr_keys_ocrv5.txt`。在实际推理过程中,会根据模型名称自动选择相应的字典文件,因此无需手动干预。
- 如果使用的 nb 模型是英文数字或其他语言的模型需要更换为对应语言的字典。PaddleOCR 仓库提供了[部分字典文件](https://github.com/PaddlePaddle/PaddleOCR/tree/release/2.3/ppocr/utils)。
```shell
# run.sh 脚本中可执行文件的参数含义:
adb shell "cd ${ppocr_demo_path} \
&& chmod +x ./ppocr_demo \
&& export LD_LIBRARY_PATH=${ppocr_demo_path}:${LD_LIBRARY_PATH} \
&& ./ppocr_demo \
\"./models/${MODEL_NAME}_det.nb\" \
\"./models/${MODEL_NAME}_rec.nb\" \
./models/${CLS_MODEL_FILE} \
./images/test.jpg \
./test_img_result.jpg \
./labels/${LABEL_FILE} \
./config.txt"
第一个参数ppocr_demo 可执行文件
第二个参数:./models/${MODEL_NAME}_det.nb 检测模型的.nb文件
第三个参数:./models/${MODEL_NAME}_rec.nb 识别模型的.nb文件
第四个参数:./models/${CLS_MODEL_FILE} 文本行方向分类模型的.nb文件默认根据模型名自动选择
第五个参数:./images/test.jpg 测试图片
第六个参数:./test_img_result.jpg 结果保存文件
第七个参数:./labels/${LABEL_FILE} label 文件,默认根据模型名自动选择
第八个参数:./config.txt 配置文件,模型的超参数配置文件,包含了检测器、分类器的超参数
```
```shell
# config.txt 具体参数 List
max_side_len 960 # 输入图像长宽大于 960 时,等比例缩放图像,使得图像最长边为 960
det_db_thresh 0.3 # 用于过滤 DB 预测的二值化图像,设置为 0.3 对结果影响不明显
det_db_box_thresh 0.5 # DB 后处理过滤 box 的阈值,如果检测存在漏框情况,可酌情减小
det_db_unclip_ratio 1.6 # 表示文本框的紧致程度,越小则文本框更靠近文本
use_direction_classify 0 # 是否使用方向分类器0 表示不使用1 表示使用
```
## 工程详解
OCR 文字识别 demo 由三个模型一起完成 OCR 文字识别功能,对输入图片先通过 `${MODEL_NAME}_det.nb` 模型做检测处理,然后通过 `ch_ppocr_mobile_v2.0_cls_slim_opt.nb` 模型做文字方向分类处理,最后通过 `${MODEL_NAME}_rec.nb` 模型完成文字识别处理。
1. `pipeline.cc` : OCR 文字识别 demo 预测全流程代码
该文件完成了三个模型串行推理的全流程控制处理,包含整个处理过程的调度处理。
- `Pipeline::Pipeline(...)` 方法完成调用三个模型类构造函数,完成模型加载和线程数、绑核处理及 predictor 创建处理
- `Pipeline::Process(...)` 方法用于完成这三个模型串行推理的全流程控制处理
2. `cls_process.cc` 方向分类器的预测文件
该文件完成了方向分类器的预处理、预测和后处理过程
- `ClsPredictor::ClsPredictor()` 方法用于完成模型加载和线程数、绑核处理及 predictor 创建处理
- `ClsPredictor::Preprocess()` 方法用于模型的预处理
- `ClsPredictor::Postprocess()` 方法用于模型的后处理
3. `rec_process.cc` 识别模型 CRNN 的预测文件
该文件完成了识别模型 CRNN 的预处理、预测和后处理过程
- `RecPredictor::RecPredictor()` 方法用于完成模型加载和线程数、绑核处理及 predictor 创建处理
- `RecPredictor::Preprocess()` 方法用于模型的预处理
- `RecPredictor::Postprocess()` 方法用于模型的后处理
4. `det_process.cc` 检测模型 DB 的预测文件
该文件完成了检测模型 DB 的预处理、预测和后处理过程
- `DetPredictor::DetPredictor()` 方法用于完成模型加载和线程数、绑核处理及 predictor 创建处理
- `DetPredictor::Preprocess()` 方法用于模型的预处理
- `DetPredictor::Postprocess()` 方法用于模型的后处理
5. `db_post_process` 检测模型 DB 的后处理函数,包含 clipper 库的调用
该文件完成了检测模型 DB 的第三方库调用和其他后处理方法实现
- `std::vector<std::vector<std::vector<int>>> BoxesFromBitmap(...)` 方法从 Bitmap 图中获取检测框
- `std::vector<std::vector<std::vector<int>>> FilterTagDetRes(...)` 方法根据识别结果获取目标框位置
## 进阶使用
如果快速开始部分无法满足你的需求,可以参考本节对 demo 进行自定义修改。
本节主要包含四部分:
- 更新预测库;
- 转换 `.nb` 模型;
- 更新模型、标签文件和预测图片;
- 更新输入/输出预处理。
### 更新预测库
本指南所使用的预测库为最新版本214rc不推荐自行更新预测库。
若有使用其他版本的需求,可参考如下步骤更新预测库:
* Paddle Lite 项目https://github.com/PaddlePaddle/Paddle-Lite
* 参考 [Paddle Lite 源码编译文档](https://www.paddlepaddle.org.cn/lite/develop/source_compile/compile_env.html),编译 Android 预测库
* 编译最终产物位于 `build.lite.xxx.xxx.xxx` 下的 `inference_lite_lib.xxx.xxx`
* 替换 c++ 库
* 头文件
将生成的 `build.lite.android.xxx.gcc/inference_lite_lib.android.xxx/cxx/include` 文件夹替换 demo 中的 `PaddleX-Lite-Deploy/libs/android/cxx/include`
* armeabi-v7a
将生成的 `build.lite.android.armv7.gcc/inference_lite_lib.android.armv7/cxx/libs/libpaddle_lite_api_shared.so` 库替换 demo 中的 `PaddleX-Lite-Deploy/libs/android/cxx/libs/armeabi-v7a/libpaddle_lite_api_shared.so`
* arm64-v8a
将生成的 `build.lite.android.armv8.gcc/inference_lite_lib.android.armv8/cxx/libs/libpaddle_lite_api_shared.so` 库替换 demo 中的 `PaddleX-Lite-Deploy/libs/android/cxx/libs/arm64-v8a/libpaddle_lite_api_shared.so`
### 转换 .nb 模型
若想使用自己训练的模型,可先参考以下流程得到 `.nb` 模型。
#### 终端命令方法支持Mac/Ubuntu
1. 进入Paddle-Lite Github仓库的[release界面](https://github.com/PaddlePaddle/Paddle-Lite/releases)选择所需版本下载对应的转化工具opt推荐使用最新版本
2. 下载 opt 工具后,执行以下命令(此处以 2.14rc 版本的 linux_x86 opt 工具转换 PP-OCRv5_mobile_det 模型为例):
```bash
./opt_linux_x86 \
--model_file=PP-OCRv5_mobile_det/inference.pdmodel \
--param_file=PP-OCRv5_mobile_det/inference.pdiparams \
--optimize_out=PP-OCRv5_mobile_det \
--valid_targets=arm
```
有关使用终端命令方法转换 `.nb` 模型的详细介绍,可参考 Paddle-Lite 仓库的[使用可执行文件 opt](https://www.paddlepaddle.org.cn/lite/v2.12/user_guides/opt/opt_bin.html)。
#### python 脚本方法支持Windows/Mac/Ubuntu
1. 安装最新版本的 paddlelite wheel 包。
```bash
pip install --pre paddlelite
```
2. 使用 python 脚本进行模型转换。以下为转换 PP-OCRv5_mobile_det 模型的示例代码:
```python
from paddlelite.lite import Opt
# 1. 创建opt实例
opt = Opt()
# 2. 指定输入模型地址
opt.set_model_file("./PP-OCRv5_mobile_det/inference.pdmodel")
opt.set_param_file("./PP-OCRv5_mobile_det/inference.pdiparams")
# 3. 指定转化类型
opt.set_valid_places("arm")
# 4. 指定输出模型地址
opt.set_optimize_out("./PP-OCRv5_mobile_det")
# 5. 执行模型优化
opt.run()
```
有关使用 python 脚本方法转换 `.nb` 模型的详细介绍,可参考 Paddle-Lite 仓库的[使用 Python 脚本 opt](https://www.paddlepaddle.org.cn/lite/v2.12/api_reference/python_api/opt.html)。
**注意**
- 有关模型优化工具 opt 的详细介绍,可参考 Paddle-Lite 仓库的[模型优化工具 opt](https://www.paddlepaddle.org.cn/lite/v2.12/user_guides/model_optimize_tool.html)
- 目前仅支持将 `.pdmodel` 格式的静态图模型转换为 `.nb` 格式。
### 更新模型、标签文件和预测图片
#### 更新模型
本指南只对 `PP-OCRv3_mobile`、`PP-OCRv4_mobile`、`PP-OCRv5_mobile` 模型进行了验证,其他模型不保证适用性。
如果你对 `PP-OCRv5_mobile` 模型进行了微调,并生成了一个名为 `PP-OCRv5_mobile_ft` 的新模型,可以按照以下步骤将原有模型替换为你的微调模型:
1. 将 `PP-OCRv5_mobile_ft` 的 nb 模型存放到目录 `PaddleX-Lite-Deploy/ocr/assets/models/` 下,最终得到的文件结构如下:
```text
.
├── ocr
│ ├── ...
│ └── assets
│ ├── models
│ │ ├── ...
│ │ ├── PP-OCRv5_mobile_ft_det.nb
│ │ └── PP-OCRv5_mobile_ft_rec.nb
│ └── ...
└── ...
```
2. 将模型名加入到 `run.sh` 脚本中的 `MODEL_LIST`。
```shell
MODEL_LIST="PP-OCRv3_mobile PP-OCRv4_mobile PP-OCRv5_mobile PP-OCRv5_mobile_ft" # 模型之间以单个空格为间隔
```
3. 运行 `run.sh` 脚本时使用模型目录名。
```shell
sh run.sh PP-OCRv5_mobile_ft
```
**注意:**
- 如果更新模型中的输入 Tensor、Shape、和 Dtype 发生更新:
- 更新文字方向分类器模型,则需要更新 `ppocr_demo/src/cls_process.cc` 中 `ClsPredictor::Preprocss` 函数
- 更新检测模型,则需要更新 `ppocr_demo/src/det_process.cc` 中 `DetPredictor::Preprocss` 函数
- 更新识别器模型,则需要更新 `ppocr_demo/src/rec_process.cc` 中 `RecPredictor::Preprocss` 函数
- 如果更新模型中的输出 Tensor 和 Dtype 发生更新:
- 更新文字方向分类器模型,则需要更新 `ppocr_demo/src/cls_process.cc` 中 `ClsPredictor::Postprocss` 函数
- 更新检测模型,则需要更新 `ppocr_demo/src/det_process.cc` 中 `DetPredictor::Postprocss` 函数
- 更新识别器模型,则需要更新 `ppocr_demo/src/rec_process.cc` 中 `RecPredictor::Postprocss` 函数
#### 更新标签文件
如果需要更新标签文件,则需要将新的标签文件存放在目录 `PaddleX-Lite-Deploy/ocr/assets/labels/` 下,并参考模型更新方法更新 `PaddleX-Lite-Deploy/ocr/android/shell/ppocr_demo/run.sh` 中执行命令;
以更新 `new_labels.txt` 为例:
```shell
# 代码文件 `PaddleX-Lite-Deploy/ocr/android/shell/ppocr_demo/run.sh`
# old
adb shell "cd ${ppocr_demo_path} \
&& chmod +x ./ppocr_demo \
&& export LD_LIBRARY_PATH=${ppocr_demo_path}:${LD_LIBRARY_PATH} \
&& ./ppocr_demo \
\"./models/${MODEL_NAME}_det.nb\" \
\"./models/${MODEL_NAME}_rec.nb\" \
./models/${CLS_MODEL_FILE} \
./images/test.jpg \
./test_img_result.jpg \
./labels/${LABEL_FILE} \
./config.txt"
# update
adb shell "cd ${ppocr_demo_path} \
&& chmod +x ./ppocr_demo \
&& export LD_LIBRARY_PATH=${ppocr_demo_path}:${LD_LIBRARY_PATH} \
&& ./ppocr_demo \
\"./models/${MODEL_NAME}_det.nb\" \
\"./models/${MODEL_NAME}_rec.nb\" \
./models/${CLS_MODEL_FILE} \
./images/test.jpg \
./test_img_result.jpg \
./labels/new_labels.txt \
./config.txt"
```
#### 更新预测图片
如果需要更新预测图片,将更新的图片存放在 `PaddleX-Lite-Deploy/ocr/assets/images/` 下,更新文件 `PaddleX-Lite-Deploy/ocr/android/shell/ppocr_demo/rush.sh` 中执行命令;
以更新 `new_pics.jpg` 为例:
```shell
# 代码文件 `PaddleX-Lite-Deploy/ocr/assets/images/run.sh`
## old
adb shell "cd ${ppocr_demo_path} \
&& chmod +x ./ppocr_demo \
&& export LD_LIBRARY_PATH=${ppocr_demo_path}:${LD_LIBRARY_PATH} \
&& ./ppocr_demo \
\"./models/${MODEL_NAME}_det.nb\" \
\"./models/${MODEL_NAME}_rec.nb\" \
./models/${CLS_MODEL_FILE} \
./images/test.jpg \
./test_img_result.jpg \
./labels/${LABEL_FILE} \
./config.txt"
# update
adb shell "cd ${ppocr_demo_path} \
&& chmod +x ./ppocr_demo \
&& export LD_LIBRARY_PATH=${ppocr_demo_path}:${LD_LIBRARY_PATH} \
&& ./ppocr_demo \
\"./models/${MODEL_NAME}_det.nb\" \
\"\"./models/${MODEL_NAME}_rec.nb\"\" \
./models/${CLS_MODEL_FILE} \
./images/new_pics.jpg \
./test_img_result.jpg \
./labels/${LABEL_FILE} \
./config.txt"
```
### 更新输入/输出预处理
- 更新输入预处理
- 更新文字方向分类器模型,则需要更新 `ppocr_demo/src/cls_process.cc` 中 `ClsPredictor::Preprocss` 函数
- 更新检测模型,则需要更新 `ppocr_demo/src/det_process.cc` 中 `DetPredictor::Preprocss` 函数
- 更新识别器模型,则需要更新 `ppocr_demo/src/rec_process.cc` 中 `RecPredictor::Preprocss` 函数
- 更新输出预处理
- 更新文字方向分类器模型,则需要更新 `ppocr_demo/src/cls_process.cc` 中 `ClsPredictor::Postprocss` 函数
- 更新检测模型,则需要更新 `ppocr_demo/src/det_process.cc` 中 `DetPredictor::Postprocss` 函数
- 更新识别器模型,则需要更新 `ppocr_demo/src/rec_process.cc` 中 `RecPredictor::Postprocss` 函数

View File

@@ -0,0 +1,11 @@
# Inference with Python or C++ Prediction Engine
Since the 2.x branch, inference with Python or C++ prediction engines has been a significant feature. This functionality allows users to load OCR-related models and perform inference without installing the wheel package.
Due to differences in pre-processing, post-processing, and concatenation details compared to the wheel package, the inference results may slightly vary, and the two cannot be directly interchanged.
For specific usage instructions, please refer to the following documents:
* [Inference with Python Prediction Engine](../../version2.x/legacy/python_infer.md)
* [Inference with C++ Prediction Engine](../../version2.x/legacy/cpp_infer.md)
* [List of Supported Models](../../version2.x/legacy/model_list_2.x.md)

View File

@@ -0,0 +1,11 @@
# 基于Python或C++预测引擎推理
自 2.x 分支以来,基于 Python 或 C++ 的预测引擎推理一直是一项重要功能。该功能允许用户在无需安装 wheel 包的情况下加载OCR相关模型并进行推理。
由于其在前后处理和串联细节上与 wheel 包存在差异,导致推理效果略有不同,两者无法直接互换使用。
有关具体使用方法,请参考以下文档:
* [基于Python预测引擎推理](../../version2.x/legacy/python_infer.md)
* [基于C++预测引擎推理](../../version2.x/legacy/cpp_infer.md)
* [支持的模型列表](../../version2.x/legacy/model_list_2.x.md)

View File

@@ -0,0 +1,92 @@
# Serving
Serving is a common deployment method in real-world production environments. By encapsulating inference capabilities as services, clients can access these services via network requests to obtain inference results. PaddleOCR recommends using [PaddleX](https://github.com/PaddlePaddle/PaddleX) for serving. Please refer to [Differences and Connections between PaddleOCR and PaddleX](../paddleocr_and_paddlex.en.md#1-Differences-and-Connections-Between-PaddleOCR-and-PaddleX) to understand the relationship between PaddleOCR and PaddleX.
PaddleX provides the following serving solutions:
- **Basic Serving**: An easy-to-use serving solution with low development costs.
- **High-Stability Serving**: Built based on [NVIDIA Triton Inference Server](https://developer.nvidia.com/triton-inference-server). Compared to the basic serving, this solution offers higher stability and allows users to adjust configurations to optimize performance.
**It is recommended to first use the basic serving solution for quick validation**, and then evaluate whether to try more complex solutions based on actual needs.
## 1. Basic Serving
### 1.1 Install Dependencies
Run the following command to install the PaddleX serving plugin via PaddleX CLI:
```bash
paddlex --install serving
```
### 1.2 Run the Server
Run the server via PaddleX CLI:
```bash
paddlex --serve --pipeline {PaddleX pipeline registration name or pipeline configuration file path} [{other command-line options}]
```
Take the general OCR pipeline as an example:
```bash
paddlex --serve --pipeline OCR
```
You should see information similar to the following:
```text
INFO: Started server process [63108]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
```
To adjust configurations (such as model path, batch size, deployment device, etc.), specify `--pipeline` as a custom configuration file. Refer to [PaddleOCR and PaddleX](../paddleocr_and_paddlex.en.md) for the mapping between PaddleOCR pipelines and PaddleX pipeline registration names, as well as how to obtain and modify PaddleX pipeline configuration files.
The command-line options related to serving are as follows:
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>--pipeline</code></td>
<td>PaddleX pipeline registration name or pipeline configuration file path.</td>
</tr>
<tr>
<td><code>--device</code></td>
<td>Deployment device for the pipeline. By default, a GPU will be used if available; otherwise, a CPU will be used."</td>
</tr>
<tr>
<td><code>--host</code></td>
<td>Hostname or IP address to which the server is bound. Defaults to <code>0.0.0.0</code>.</td>
</tr>
<tr>
<td><code>--port</code></td>
<td>Port number on which the server listens. Defaults to <code>8080</code>.</td>
</tr>
<tr>
<td><code>--use_hpip</code></td>
<td>If specified, uses high-performance inference. Refer to the High-Performance Inference documentation for more information.</td>
</tr>
<tr>
<td><code>--hpi_config</code></td>
<td>High-performance inference configuration. Refer to the High-Performance Inference documentation for more information.</td>
</tr>
</tbody>
</table>
### 1.3 Invoke the Service
The <b>"Development Integration/Deployment"</b> section in the PaddleOCR pipeline tutorial provides API references and multi-language invocation examples for the service.
## 2. High-Stability Serving
Please refer to the [PaddleX Serving Guide](https://paddlepaddle.github.io/PaddleX/3.0/en/pipeline_deploy/serving.html#2). More information about PaddleX pipeline configuration files can be found in [Using PaddleX Pipeline Configuration Files](../paddleocr_and_paddlex.en.md#3-using-paddlex-pipeline-configuration-files).
It should be noted that, due to the lack of fine-grained optimization and other reasons, the current high-stability serving deployment solution provided by PaddleOCR may not match the performance of the 2.x version based on PaddleServing. However, this new solution fully supports the PaddlePaddle 3.0 framework. We will continue to optimize it and consider introducing more performant deployment solutions in the future.

View File

@@ -0,0 +1,93 @@
# 服务化部署
服务化部署是实际生产环境中常见的一种部署形式。通过将推理功能封装为服务客户端可以通过网络请求来访问这些服务以获取推理结果。PaddleOCR 推荐用户使用 [PaddleX](https://github.com/PaddlePaddle/PaddleX) 进行服务化部署。请阅读 [PaddleOCR 与 PaddleX 的区别与联系](../paddleocr_and_paddlex.md#1-paddleocr-与-paddlex-的区别与联系) 了解 PaddleOCR 与 PaddleX 的关系。
PaddleX 提供以下服务化部署方案:
- **基础服务化部署**:简单易用的服务化部署方案,开发成本低。
- **高稳定性服务化部署**:基于 [NVIDIA Triton Inference Server](https://developer.nvidia.com/triton-inference-server) 打造。与基础服务化部署相比,该方案提供更高的稳定性,并允许用户调整配置以优化性能。
**建议首先使用基础服务化部署方案进行快速验证**,然后根据实际需要,评估是否尝试更复杂的方案。
## 1. 基础服务化部署
### 1.1 安装依赖
执行如下命令,通过 PaddleX CLI 安装 PaddleX 服务化部署插件:
```bash
paddlex --install serving
```
### 1.2 运行服务器
通过 PaddleX CLI 运行服务器:
```bash
paddlex --serve --pipeline {PaddleX 产线注册名或产线配置文件路径} [{其他命令行选项}]
```
以通用 OCR 产线为例:
```bash
paddlex --serve --pipeline OCR
```
可以看到类似以下展示的信息:
```text
INFO: Started server process [63108]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
```
如需调整配置如模型路径、batch size、部署设备等可指定 `--pipeline` 为自定义配置文件。请参考 [PaddleOCR 与 PaddleX](../paddleocr_and_paddlex.md) 了解 PaddleOCR 产线与 PaddleX 产线注册名的对应关系,以及 PaddleX 产线配置文件的获取与修改方式。
与服务化部署相关的命令行选项如下:
<table>
<thead>
<tr>
<th>名称</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>--pipeline</code></td>
<td>PaddleX 产线注册名或产线配置文件路径。</td>
</tr>
<tr>
<td><code>--device</code></td>
<td>产线部署设备。默认情况下,当 GPU 可用时,将使用 GPU否则使用 CPU。</td>
</tr>
<tr>
<td><code>--host</code></td>
<td>服务器绑定的主机名或 IP 地址。默认为 <code>0.0.0.0</code>。</td>
</tr>
<tr>
<td><code>--port</code></td>
<td>服务器监听的端口号。默认为 <code>8080</code>。</td>
</tr>
<tr>
<td><code>--use_hpip</code></td>
<td>如果指定,则使用高性能推理。请参考高性能推理文档了解更多信息。</td>
</tr>
<tr>
<td><code>--hpi_config</code></td>
<td>高性能推理配置。请参考高性能推理文档了解更多信息。</td>
</tr>
</tbody>
</table>
</table>
### 1.3 调用服务
PaddleOCR 产线使用教程中的 <b>“开发集成/部署”</b> 部分提供了服务的 API 参考与多语言调用示例。
## 2. 高稳定性服务化部署
请参考 [PaddleX 服务化部署指南](https://paddlepaddle.github.io/PaddleX/3.0/pipeline_deploy/serving.html#2)。在 [使用 PaddleX 产线配置文件](../paddleocr_and_paddlex.md#3-使用-paddlex-产线配置文件) 中,可以了解关于 PaddleX 产线配置文件的更多信息。
需要说明的是,由于缺乏细粒度优化等原因,当前 PaddleOCR 提供的高稳定性服务化部署方案在性能上可能不及 2.x 版本基于 PaddleServing 的方案;但该新方案已对飞桨 3.0 框架提供了全面支持,我们也将持续优化,后续考虑推出性能更优的部署方案。