init
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
[English](README.md) | 简体中文
|
||||
|
||||
# PaddleOCR 模型在CPU与GPU上的部署方案-FastDeploy
|
||||
|
||||
## 1. 说明
|
||||
PaddleOCR支持通过FastDeploy在NVIDIA GPU、X86 CPU、飞腾CPU、ARM CPU、Intel GPU(独立显卡/集成显卡)硬件上快速部署PaddleOCR系列模型
|
||||
|
||||
## 2. 支持的PaddleOCR推理模型
|
||||
|
||||
下表中的推理模型为FastDeploy测试过的模型, 下载链接由PaddleOCR模型库提供,
|
||||
更多的模型, 详见[PP-OCR系列模型列表](https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.6/doc/doc_ch/models_list.md), 欢迎用户尝试.
|
||||
|
||||
| PaddleOCR版本 | 文本框检测 | 方向分类模型 | 文字识别 |字典文件| 说明 |
|
||||
|:----|:----|:----|:----|:----|:--------|
|
||||
| ch_PP-OCRv3[推荐] |[ch_PP-OCRv3_det](https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar) | [ch_ppocr_mobile_v2.0_cls](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar) | [ch_PP-OCRv3_rec](https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar) | [ppocr_keys_v1.txt](https://bj.bcebos.com/paddlehub/fastdeploy/ppocr_keys_v1.txt) | OCRv3系列原始超轻量模型,支持中英文、多语种文本检测 |
|
||||
| en_PP-OCRv3[推荐] |[en_PP-OCRv3_det](https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar) | [ch_ppocr_mobile_v2.0_cls](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar) | [en_PP-OCRv3_rec](https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_infer.tar) | [en_dict.txt](https://bj.bcebos.com/paddlehub/fastdeploy/en_dict.txt) | OCRv3系列原始超轻量模型,支持英文与数字识别,除检测模型和识别模型的训练数据与中文模型不同以外,无其他区别 |
|
||||
| ch_PP-OCRv2 |[ch_PP-OCRv2_det](https://paddleocr.bj.bcebos.com/PP-OCRv2/chinese/ch_PP-OCRv2_det_infer.tar) | [ch_ppocr_mobile_v2.0_cls](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar) | [ch_PP-OCRv2_rec](https://paddleocr.bj.bcebos.com/PP-OCRv2/chinese/ch_PP-OCRv2_rec_infer.tar) | [ppocr_keys_v1.txt](https://bj.bcebos.com/paddlehub/fastdeploy/ppocr_keys_v1.txt) | OCRv2系列原始超轻量模型,支持中英文、多语种文本检测 |
|
||||
| ch_PP-OCRv2_mobile |[ch_ppocr_mobile_v2.0_det](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_infer.tar) | [ch_ppocr_mobile_v2.0_cls](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar) | [ch_ppocr_mobile_v2.0_rec](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_infer.tar) | [ppocr_keys_v1.txt](https://bj.bcebos.com/paddlehub/fastdeploy/ppocr_keys_v1.txt) | OCRv2系列原始超轻量模型,支持中英文、多语种文本检测,比PPOCRv2更加轻量 |
|
||||
| ch_PP-OCRv2_server |[ch_ppocr_server_v2.0_det](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_det_infer.tar) | [ch_ppocr_mobile_v2.0_cls](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar) | [ch_ppocr_server_v2.0_rec](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_rec_infer.tar) |[ppocr_keys_v1.txt](https://bj.bcebos.com/paddlehub/fastdeploy/ppocr_keys_v1.txt) | OCRv2服务器系列模型, 支持中英文、多语种文本检测,比超轻量模型更大,但效果更好|
|
||||
|
||||
|
||||
## 3. 详细部署的部署示例
|
||||
- [Python部署](python)
|
||||
- [C++部署](cpp)
|
||||
- [C部署](c)
|
||||
- [C#部署](csharp)
|
||||
@@ -0,0 +1,13 @@
|
||||
PROJECT(infer_demo C)
|
||||
CMAKE_MINIMUM_REQUIRED (VERSION 3.10)
|
||||
|
||||
# 指定下载解压后的fastdeploy库路径
|
||||
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
|
||||
|
||||
include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)
|
||||
|
||||
# 添加FastDeploy依赖头文件
|
||||
include_directories(${FASTDEPLOY_INCS})
|
||||
|
||||
add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.c)
|
||||
target_link_libraries(infer_demo ${FASTDEPLOY_LIBS})
|
||||
@@ -0,0 +1,263 @@
|
||||
[English](README.md) | 简体中文
|
||||
# PaddleOCR CPU-GPU C部署示例
|
||||
|
||||
本目录下提供`infer.c`来调用C API快速完成PP-OCRv3模型在CPU/GPU上部署的示例。
|
||||
|
||||
## 1. 说明
|
||||
PaddleOCR支持利用FastDeploy在NVIDIA GPU、X86 CPU、飞腾CPU、ARM CPU、Intel GPU(独立显卡/集成显卡)硬件上快速部署OCR模型.
|
||||
|
||||
## 2. 部署环境准备
|
||||
在部署前,需确认软硬件环境,同时下载预编译部署库,参考[FastDeploy安装文档](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install#FastDeploy预编译库安装)安装FastDeploy预编译库.
|
||||
以Linux上推理为例,在本目录执行如下命令即可完成编译测试,支持此模型需保证FastDeploy版本1.0.4以上(x.x.x>=1.0.4)
|
||||
|
||||
## 3. 部署模型准备
|
||||
在部署前, 请准备好您所需要运行的推理模型, 您可以在[FastDeploy支持的PaddleOCR模型列表](../README.md)中下载所需模型.
|
||||
|
||||
## 4.运行部署示例
|
||||
```bash
|
||||
# 下载部署示例代码
|
||||
git clone https://github.com/PaddlePaddle/FastDeploy.git
|
||||
cd FastDeploy/examples/vision/ocr/PP-OCR/cpu-gpu/c
|
||||
|
||||
# 如果您希望从PaddleOCR下载示例代码,请运行
|
||||
git clone https://github.com/PaddlePaddle/PaddleOCR.git
|
||||
# 注意:如果当前分支找不到下面的fastdeploy测试代码,请切换到dygraph分支
|
||||
git checkout dygraph
|
||||
cd PaddleOCR/deploy/fastdeploy/cpu-gpu/c
|
||||
|
||||
mkdir build
|
||||
cd build
|
||||
|
||||
# 下载FastDeploy预编译库,用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用
|
||||
wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz
|
||||
|
||||
# 编译Demo
|
||||
tar xvf fastdeploy-linux-x64-x.x.x.tgz
|
||||
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x
|
||||
make -j
|
||||
|
||||
# 下载PP-OCRv3文字检测模型
|
||||
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar
|
||||
tar -xvf ch_PP-OCRv3_det_infer.tar
|
||||
# 下载文字方向分类器模型
|
||||
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
|
||||
tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar
|
||||
# 下载PP-OCRv3文字识别模型
|
||||
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar
|
||||
tar -xvf ch_PP-OCRv3_rec_infer.tar
|
||||
|
||||
# 下载预测图片与字典文件
|
||||
wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/doc/imgs/12.jpg
|
||||
wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/ppocr/utils/ppocr_keys_v1.txt
|
||||
|
||||
# 在CPU上使用Paddle Inference推理
|
||||
./infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 0
|
||||
# 在GPU上使用Paddle Inference推理
|
||||
./infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 1
|
||||
```
|
||||
以上命令只适用于Linux或MacOS, Windows下SDK的使用方式请参考:
|
||||
- [如何在Windows中使用FastDeploy C++ SDK](../../../../../docs/cn/faq/use_sdk_on_windows.md)
|
||||
|
||||
|
||||
运行完成可视化结果如下图所示
|
||||
<img width="640" src="https://user-images.githubusercontent.com/109218879/185826024-f7593a0c-1bd2-4a60-b76c-15588484fa08.jpg">
|
||||
|
||||
|
||||
## 5. PP-OCRv3 C API接口简介
|
||||
下面提供了PP-OCRv3的C API简介
|
||||
|
||||
- 如果用户想要更换部署后端或进行其他定制化操作, 请查看[C Runtime API](https://baidu-paddle.github.io/fastdeploy-api/c/html/runtime__option_8h.html).
|
||||
- 更多 PP-OCR C API 请查看 [C PP-OCR API](https://github.com/PaddlePaddle/FastDeploy/blob/develop/c_api/fastdeploy_capi/vision/ocr/ppocr/model.h)
|
||||
|
||||
### 配置
|
||||
|
||||
```c
|
||||
FD_C_RuntimeOptionWrapper* FD_C_CreateRuntimeOptionWrapper()
|
||||
```
|
||||
|
||||
> 创建一个RuntimeOption的配置对象,并且返回操作它的指针。
|
||||
>
|
||||
> **返回**
|
||||
>
|
||||
> * **fd_c_runtime_option_wrapper**(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption对象的指针
|
||||
|
||||
|
||||
```c
|
||||
void FD_C_RuntimeOptionWrapperUseCpu(
|
||||
FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper)
|
||||
```
|
||||
|
||||
> 开启CPU推理
|
||||
>
|
||||
> **参数**
|
||||
>
|
||||
> * **fd_c_runtime_option_wrapper**(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption对象的指针
|
||||
|
||||
```c
|
||||
void FD_C_RuntimeOptionWrapperUseGpu(
|
||||
FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper,
|
||||
int gpu_id)
|
||||
```
|
||||
> 开启GPU推理
|
||||
>
|
||||
> **参数**
|
||||
>
|
||||
> * **fd_c_runtime_option_wrapper**(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption对象的指针
|
||||
> * **gpu_id**(int): 显卡号
|
||||
|
||||
|
||||
### 模型
|
||||
|
||||
```c
|
||||
FD_C_DBDetectorWrapper* FD_C_CreateDBDetectorWrapper(
|
||||
const char* model_file, const char* params_file,
|
||||
FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper,
|
||||
const FD_C_ModelFormat model_format
|
||||
)
|
||||
```
|
||||
|
||||
> 创建一个DBDetector的模型,并且返回操作它的指针。
|
||||
>
|
||||
> **参数**
|
||||
>
|
||||
> * **model_file**(const char*): 模型文件路径
|
||||
> * **params_file**(const char*): 参数文件路径
|
||||
> * **fd_c_runtime_option_wrapper**(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption的指针,表示后端推理配置
|
||||
> * **model_format**(FD_C_ModelFormat): 模型格式
|
||||
>
|
||||
> **返回**
|
||||
> * **fd_c_dbdetector_wrapper**(FD_C_DBDetectorWrapper*): 指向DBDetector模型对象的指针
|
||||
|
||||
```c
|
||||
FD_C_ClassifierWrapper* FD_C_CreateClassifierWrapper(
|
||||
const char* model_file, const char* params_file,
|
||||
FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper,
|
||||
const FD_C_ModelFormat model_format
|
||||
)
|
||||
```
|
||||
> 创建一个Classifier的模型,并且返回操作它的指针。
|
||||
>
|
||||
> **参数**
|
||||
>
|
||||
> * **model_file**(const char*): 模型文件路径
|
||||
> * **params_file**(const char*): 参数文件路径
|
||||
> * **fd_c_runtime_option_wrapper**(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption的指针,表示后端推理配置
|
||||
> * **model_format**(FD_C_ModelFormat): 模型格式
|
||||
>
|
||||
> **返回**
|
||||
>
|
||||
> * **fd_c_classifier_wrapper**(FD_C_ClassifierWrapper*): 指向Classifier模型对象的指针
|
||||
|
||||
```c
|
||||
FD_C_RecognizerWrapper* FD_C_CreateRecognizerWrapper(
|
||||
const char* model_file, const char* params_file, const char* label_path,
|
||||
FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper,
|
||||
const FD_C_ModelFormat model_format
|
||||
)
|
||||
```
|
||||
> 创建一个Recognizer的模型,并且返回操作它的指针。
|
||||
>
|
||||
> **参数**
|
||||
>
|
||||
> * **model_file**(const char*): 模型文件路径
|
||||
> * **params_file**(const char*): 参数文件路径
|
||||
> * **label_path**(const char*): 标签文件路径
|
||||
> * **fd_c_runtime_option_wrapper**(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption的指针,表示后端推理配置
|
||||
> * **model_format**(FD_C_ModelFormat): 模型格式
|
||||
>
|
||||
> **返回**
|
||||
> * **fd_c_recognizer_wrapper**(FD_C_RecognizerWrapper*): 指向Recognizer模型对象的指针
|
||||
|
||||
```c
|
||||
FD_C_PPOCRv3Wrapper* FD_C_CreatePPOCRv3Wrapper(
|
||||
FD_C_DBDetectorWrapper* det_model,
|
||||
FD_C_ClassifierWrapper* cls_model,
|
||||
FD_C_RecognizerWrapper* rec_model
|
||||
)
|
||||
```
|
||||
> 创建一个PP-OCRv3的模型,并且返回操作它的指针。
|
||||
>
|
||||
> **参数**
|
||||
>
|
||||
> * **det_model**(FD_C_DBDetectorWrapper*): DBDetector模型
|
||||
> * **cls_model**(FD_C_ClassifierWrapper*): Classifier模型
|
||||
> * **rec_model**(FD_C_RecognizerWrapper*): Recognizer模型
|
||||
>
|
||||
> **返回**
|
||||
>
|
||||
> * **fd_c_ppocrv3_wrapper**(FD_C_PPOCRv3Wrapper*): 指向PP-OCRv3模型对象的指针
|
||||
|
||||
|
||||
|
||||
### 读写图像
|
||||
|
||||
```c
|
||||
FD_C_Mat FD_C_Imread(const char* imgpath)
|
||||
```
|
||||
|
||||
> 读取一个图像,并且返回cv::Mat的指针。
|
||||
>
|
||||
> **参数**
|
||||
>
|
||||
> * **imgpath**(const char*): 图像文件路径
|
||||
>
|
||||
> **返回**
|
||||
>
|
||||
> * **imgmat**(FD_C_Mat): 指向图像数据cv::Mat的指针。
|
||||
|
||||
|
||||
```c
|
||||
FD_C_Bool FD_C_Imwrite(const char* savepath, FD_C_Mat img);
|
||||
```
|
||||
|
||||
> 将图像写入文件中。
|
||||
>
|
||||
> **参数**
|
||||
>
|
||||
> * **savepath**(const char*): 保存图像的路径
|
||||
> * **img**(FD_C_Mat): 指向图像数据的指针
|
||||
>
|
||||
> **返回**
|
||||
>
|
||||
> * **result**(FD_C_Bool): 表示操作是否成功
|
||||
|
||||
|
||||
### Predict函数
|
||||
|
||||
```c
|
||||
FD_C_Bool FD_C_PPOCRv3WrapperPredict(
|
||||
FD_C_PPOCRv3Wrapper* fd_c_ppocrv3_wrapper,
|
||||
FD_C_Mat img,
|
||||
FD_C_OCRResult* result)
|
||||
```
|
||||
>
|
||||
> 模型预测接口,输入图像直接并生成结果。
|
||||
>
|
||||
> **参数**
|
||||
> * **fd_c_ppocrv3_wrapper**(FD_C_PPOCRv3Wrapper*): 指向PP-OCRv3模型的指针
|
||||
> * **img**(FD_C_Mat): 输入图像的指针,指向cv::Mat对象,可以调用FD_C_Imread读取图像获取
|
||||
> * **result**(FD_C_OCRResult*): OCR预测结果,包括由检测模型输出的检测框位置,分类模型输出的方向分类,以及识别模型输出的识别结果, OCRResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/)
|
||||
|
||||
|
||||
### Predict结果
|
||||
|
||||
```c
|
||||
FD_C_Mat FD_C_VisOcr(FD_C_Mat im, FD_C_OCRResult* ocr_result)
|
||||
```
|
||||
>
|
||||
> 对结果进行可视化,返回可视化的图像。
|
||||
>
|
||||
> **参数**
|
||||
> * **im**(FD_C_Mat): 指向输入图像的指针
|
||||
> * **ocr_result**(FD_C_OCRResult*): 指向 FD_C_OCRResult结构的指针
|
||||
>
|
||||
> **返回**
|
||||
> * **vis_im**(FD_C_Mat): 指向可视化图像的指针
|
||||
|
||||
|
||||
## 6. 其它文档
|
||||
|
||||
- [FastDeploy部署PaddleOCR模型概览](../../)
|
||||
- [PP-OCRv3 Python部署](../python)
|
||||
- [PP-OCRv3 C++ 部署](../cpp)
|
||||
- [PP-OCRv3 C# 部署](../csharp)
|
||||
@@ -0,0 +1,249 @@
|
||||
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "fastdeploy_capi/vision.h"
|
||||
|
||||
#ifdef WIN32
|
||||
const char sep = '\\';
|
||||
#else
|
||||
const char sep = '/';
|
||||
#endif
|
||||
|
||||
void CpuInfer(const char *det_model_dir, const char *cls_model_dir,
|
||||
const char *rec_model_dir, const char *rec_label_file,
|
||||
const char *image_file) {
|
||||
char det_model_file[100];
|
||||
char det_params_file[100];
|
||||
|
||||
char cls_model_file[100];
|
||||
char cls_params_file[100];
|
||||
|
||||
char rec_model_file[100];
|
||||
char rec_params_file[100];
|
||||
|
||||
int max_size = 99;
|
||||
snprintf(det_model_file, max_size, "%s%c%s", det_model_dir, sep,
|
||||
"inference.pdmodel");
|
||||
snprintf(det_params_file, max_size, "%s%c%s", det_model_dir, sep,
|
||||
"inference.pdiparams");
|
||||
|
||||
snprintf(cls_model_file, max_size, "%s%c%s", cls_model_dir, sep,
|
||||
"inference.pdmodel");
|
||||
snprintf(cls_params_file, max_size, "%s%c%s", cls_model_dir, sep,
|
||||
"inference.pdiparams");
|
||||
|
||||
snprintf(rec_model_file, max_size, "%s%c%s", rec_model_dir, sep,
|
||||
"inference.pdmodel");
|
||||
snprintf(rec_params_file, max_size, "%s%c%s", rec_model_dir, sep,
|
||||
"inference.pdiparams");
|
||||
|
||||
FD_C_RuntimeOptionWrapper *det_option = FD_C_CreateRuntimeOptionWrapper();
|
||||
FD_C_RuntimeOptionWrapper *cls_option = FD_C_CreateRuntimeOptionWrapper();
|
||||
FD_C_RuntimeOptionWrapper *rec_option = FD_C_CreateRuntimeOptionWrapper();
|
||||
FD_C_RuntimeOptionWrapperUseCpu(det_option);
|
||||
FD_C_RuntimeOptionWrapperUseCpu(cls_option);
|
||||
FD_C_RuntimeOptionWrapperUseCpu(rec_option);
|
||||
|
||||
FD_C_DBDetectorWrapper *det_model = FD_C_CreateDBDetectorWrapper(
|
||||
det_model_file, det_params_file, det_option, FD_C_ModelFormat_PADDLE);
|
||||
FD_C_ClassifierWrapper *cls_model = FD_C_CreateClassifierWrapper(
|
||||
cls_model_file, cls_params_file, cls_option, FD_C_ModelFormat_PADDLE);
|
||||
FD_C_RecognizerWrapper *rec_model = FD_C_CreateRecognizerWrapper(
|
||||
rec_model_file, rec_params_file, rec_label_file, rec_option,
|
||||
FD_C_ModelFormat_PADDLE);
|
||||
|
||||
FD_C_PPOCRv3Wrapper *ppocr_v3 =
|
||||
FD_C_CreatePPOCRv3Wrapper(det_model, cls_model, rec_model);
|
||||
if (!FD_C_PPOCRv3WrapperInitialized(ppocr_v3)) {
|
||||
printf("Failed to initialize.\n");
|
||||
FD_C_DestroyRuntimeOptionWrapper(det_option);
|
||||
FD_C_DestroyRuntimeOptionWrapper(cls_option);
|
||||
FD_C_DestroyRuntimeOptionWrapper(rec_option);
|
||||
FD_C_DestroyClassifierWrapper(cls_model);
|
||||
FD_C_DestroyDBDetectorWrapper(det_model);
|
||||
FD_C_DestroyRecognizerWrapper(rec_model);
|
||||
FD_C_DestroyPPOCRv3Wrapper(ppocr_v3);
|
||||
return;
|
||||
}
|
||||
|
||||
FD_C_Mat im = FD_C_Imread(image_file);
|
||||
|
||||
FD_C_OCRResult *result = (FD_C_OCRResult *)malloc(sizeof(FD_C_OCRResult));
|
||||
|
||||
if (!FD_C_PPOCRv3WrapperPredict(ppocr_v3, im, result)) {
|
||||
printf("Failed to predict.\n");
|
||||
FD_C_DestroyRuntimeOptionWrapper(det_option);
|
||||
FD_C_DestroyRuntimeOptionWrapper(cls_option);
|
||||
FD_C_DestroyRuntimeOptionWrapper(rec_option);
|
||||
FD_C_DestroyClassifierWrapper(cls_model);
|
||||
FD_C_DestroyDBDetectorWrapper(det_model);
|
||||
FD_C_DestroyRecognizerWrapper(rec_model);
|
||||
FD_C_DestroyPPOCRv3Wrapper(ppocr_v3);
|
||||
FD_C_DestroyMat(im);
|
||||
free(result);
|
||||
return;
|
||||
}
|
||||
|
||||
// print res
|
||||
char res[2000];
|
||||
FD_C_OCRResultStr(result, res);
|
||||
printf("%s", res);
|
||||
FD_C_Mat vis_im = FD_C_VisOcr(im, result);
|
||||
FD_C_Imwrite("vis_result.jpg", vis_im);
|
||||
printf("Visualized result saved in ./vis_result.jpg\n");
|
||||
|
||||
FD_C_DestroyRuntimeOptionWrapper(det_option);
|
||||
FD_C_DestroyRuntimeOptionWrapper(cls_option);
|
||||
FD_C_DestroyRuntimeOptionWrapper(rec_option);
|
||||
FD_C_DestroyClassifierWrapper(cls_model);
|
||||
FD_C_DestroyDBDetectorWrapper(det_model);
|
||||
FD_C_DestroyRecognizerWrapper(rec_model);
|
||||
FD_C_DestroyPPOCRv3Wrapper(ppocr_v3);
|
||||
FD_C_DestroyOCRResult(result);
|
||||
FD_C_DestroyMat(im);
|
||||
FD_C_DestroyMat(vis_im);
|
||||
}
|
||||
|
||||
void GpuInfer(const char *det_model_dir, const char *cls_model_dir,
|
||||
const char *rec_model_dir, const char *rec_label_file,
|
||||
const char *image_file) {
|
||||
char det_model_file[100];
|
||||
char det_params_file[100];
|
||||
|
||||
char cls_model_file[100];
|
||||
char cls_params_file[100];
|
||||
|
||||
char rec_model_file[100];
|
||||
char rec_params_file[100];
|
||||
|
||||
int max_size = 99;
|
||||
snprintf(det_model_file, max_size, "%s%c%s", det_model_dir, sep,
|
||||
"inference.pdmodel");
|
||||
snprintf(det_params_file, max_size, "%s%c%s", det_model_dir, sep,
|
||||
"inference.pdiparams");
|
||||
|
||||
snprintf(cls_model_file, max_size, "%s%c%s", cls_model_dir, sep,
|
||||
"inference.pdmodel");
|
||||
snprintf(cls_params_file, max_size, "%s%c%s", cls_model_dir, sep,
|
||||
"inference.pdiparams");
|
||||
|
||||
snprintf(rec_model_file, max_size, "%s%c%s", rec_model_dir, sep,
|
||||
"inference.pdmodel");
|
||||
snprintf(rec_params_file, max_size, "%s%c%s", rec_model_dir, sep,
|
||||
"inference.pdiparams");
|
||||
|
||||
FD_C_RuntimeOptionWrapper *det_option = FD_C_CreateRuntimeOptionWrapper();
|
||||
FD_C_RuntimeOptionWrapper *cls_option = FD_C_CreateRuntimeOptionWrapper();
|
||||
FD_C_RuntimeOptionWrapper *rec_option = FD_C_CreateRuntimeOptionWrapper();
|
||||
FD_C_RuntimeOptionWrapperUseGpu(det_option, 0);
|
||||
FD_C_RuntimeOptionWrapperUseGpu(cls_option, 0);
|
||||
FD_C_RuntimeOptionWrapperUseGpu(rec_option, 0);
|
||||
|
||||
FD_C_DBDetectorWrapper *det_model = FD_C_CreateDBDetectorWrapper(
|
||||
det_model_file, det_params_file, det_option, FD_C_ModelFormat_PADDLE);
|
||||
FD_C_ClassifierWrapper *cls_model = FD_C_CreateClassifierWrapper(
|
||||
cls_model_file, cls_params_file, cls_option, FD_C_ModelFormat_PADDLE);
|
||||
FD_C_RecognizerWrapper *rec_model = FD_C_CreateRecognizerWrapper(
|
||||
rec_model_file, rec_params_file, rec_label_file, rec_option,
|
||||
FD_C_ModelFormat_PADDLE);
|
||||
|
||||
FD_C_PPOCRv3Wrapper *ppocr_v3 =
|
||||
FD_C_CreatePPOCRv3Wrapper(det_model, cls_model, rec_model);
|
||||
if (!FD_C_PPOCRv3WrapperInitialized(ppocr_v3)) {
|
||||
printf("Failed to initialize.\n");
|
||||
FD_C_DestroyRuntimeOptionWrapper(det_option);
|
||||
FD_C_DestroyRuntimeOptionWrapper(cls_option);
|
||||
FD_C_DestroyRuntimeOptionWrapper(rec_option);
|
||||
FD_C_DestroyClassifierWrapper(cls_model);
|
||||
FD_C_DestroyDBDetectorWrapper(det_model);
|
||||
FD_C_DestroyRecognizerWrapper(rec_model);
|
||||
FD_C_DestroyPPOCRv3Wrapper(ppocr_v3);
|
||||
return;
|
||||
}
|
||||
|
||||
FD_C_Mat im = FD_C_Imread(image_file);
|
||||
|
||||
FD_C_OCRResult *result = (FD_C_OCRResult *)malloc(sizeof(FD_C_OCRResult));
|
||||
|
||||
if (!FD_C_PPOCRv3WrapperPredict(ppocr_v3, im, result)) {
|
||||
printf("Failed to predict.\n");
|
||||
FD_C_DestroyRuntimeOptionWrapper(det_option);
|
||||
FD_C_DestroyRuntimeOptionWrapper(cls_option);
|
||||
FD_C_DestroyRuntimeOptionWrapper(rec_option);
|
||||
FD_C_DestroyClassifierWrapper(cls_model);
|
||||
FD_C_DestroyDBDetectorWrapper(det_model);
|
||||
FD_C_DestroyRecognizerWrapper(rec_model);
|
||||
FD_C_DestroyPPOCRv3Wrapper(ppocr_v3);
|
||||
FD_C_DestroyMat(im);
|
||||
free(result);
|
||||
return;
|
||||
}
|
||||
|
||||
// print res
|
||||
char res[2000];
|
||||
FD_C_OCRResultStr(result, res);
|
||||
printf("%s", res);
|
||||
FD_C_Mat vis_im = FD_C_VisOcr(im, result);
|
||||
FD_C_Imwrite("vis_result.jpg", vis_im);
|
||||
printf("Visualized result saved in ./vis_result.jpg\n");
|
||||
|
||||
FD_C_DestroyRuntimeOptionWrapper(det_option);
|
||||
FD_C_DestroyRuntimeOptionWrapper(cls_option);
|
||||
FD_C_DestroyRuntimeOptionWrapper(rec_option);
|
||||
FD_C_DestroyClassifierWrapper(cls_model);
|
||||
FD_C_DestroyDBDetectorWrapper(det_model);
|
||||
FD_C_DestroyRecognizerWrapper(rec_model);
|
||||
FD_C_DestroyPPOCRv3Wrapper(ppocr_v3);
|
||||
FD_C_DestroyOCRResult(result);
|
||||
FD_C_DestroyMat(im);
|
||||
FD_C_DestroyMat(vis_im);
|
||||
}
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 7) {
|
||||
printf("Usage: infer_demo path/to/det_model path/to/cls_model "
|
||||
"path/to/rec_model path/to/rec_label_file path/to/image "
|
||||
"run_option, "
|
||||
"e.g ./infer_demo ./ch_PP-OCRv3_det_infer "
|
||||
"./ch_ppocr_mobile_v3.0_cls_infer ./ch_PP-OCRv3_rec_infer "
|
||||
"./ppocr_keys_v1.txt ./12.jpg 0\n");
|
||||
printf(
|
||||
"The data type of run_option is int, 0: run with cpu; 1: run with gpu"
|
||||
"\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (atoi(argv[6]) == 0) {
|
||||
CpuInfer(argv[1], argv[2], argv[3], argv[4], argv[5]);
|
||||
} else if (atoi(argv[6]) == 1) {
|
||||
GpuInfer(argv[1], argv[2], argv[3], argv[4], argv[5]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
PROJECT(infer_demo C CXX)
|
||||
CMAKE_MINIMUM_REQUIRED (VERSION 3.10)
|
||||
|
||||
# 指定下载解压后的fastdeploy库路径
|
||||
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
|
||||
|
||||
include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)
|
||||
|
||||
# 添加FastDeploy依赖头文件
|
||||
include_directories(${FASTDEPLOY_INCS})
|
||||
|
||||
# PP-OCR
|
||||
add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc)
|
||||
# 添加FastDeploy库依赖
|
||||
target_link_libraries(infer_demo ${FASTDEPLOY_LIBS})
|
||||
|
||||
# Only Det
|
||||
add_executable(infer_det ${PROJECT_SOURCE_DIR}/infer_det.cc)
|
||||
# 添加FastDeploy库依赖
|
||||
target_link_libraries(infer_det ${FASTDEPLOY_LIBS})
|
||||
|
||||
# Only Cls
|
||||
add_executable(infer_cls ${PROJECT_SOURCE_DIR}/infer_cls.cc)
|
||||
# 添加FastDeploy库依赖
|
||||
target_link_libraries(infer_cls ${FASTDEPLOY_LIBS})
|
||||
|
||||
# Only Rec
|
||||
add_executable(infer_rec ${PROJECT_SOURCE_DIR}/infer_rec.cc)
|
||||
# 添加FastDeploy库依赖
|
||||
target_link_libraries(infer_rec ${FASTDEPLOY_LIBS})
|
||||
@@ -0,0 +1,163 @@
|
||||
[English](README.md) | 简体中文
|
||||
# PaddleOCR CPU-GPU C++部署示例
|
||||
|
||||
本目录下提供`infer.cc`快速完成PP-OCRv3在CPU/GPU,以及GPU上通过Paddle-TensorRT加速部署的示例.
|
||||
## 1. 说明
|
||||
PaddleOCR支持利用FastDeploy在NVIDIA GPU、X86 CPU、飞腾CPU、ARM CPU、Intel GPU(独立显卡/集成显卡)硬件上快速部署OCR模型.
|
||||
|
||||
## 2. 部署环境准备
|
||||
在部署前,需确认软硬件环境,同时下载预编译部署库,参考[FastDeploy安装文档](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install#FastDeploy预编译库安装)安装FastDeploy预编译库.
|
||||
|
||||
## 3. 部署模型准备
|
||||
在部署前, 请准备好您所需要运行的推理模型, 您可以在[FastDeploy支持的PaddleOCR模型列表](../README.md)中下载所需模型.
|
||||
|
||||
## 4. 运行部署示例
|
||||
以Linux上推理为例,在本目录执行如下命令即可完成编译测试,支持此模型需保证FastDeploy版本1.0.0以上(x.x.x>=1.0.0)
|
||||
|
||||
```bash
|
||||
# 下载部署示例代码
|
||||
git clone https://github.com/PaddlePaddle/FastDeploy.git
|
||||
cd FastDeploy/examples/vision/ocr/PP-OCR/cpu-gpu/cpp
|
||||
|
||||
# 如果您希望从PaddleOCR下载示例代码,请运行
|
||||
git clone https://github.com/PaddlePaddle/PaddleOCR.git
|
||||
# 注意:如果当前分支找不到下面的fastdeploy测试代码,请切换到dygraph分支
|
||||
git checkout dygraph
|
||||
cd PaddleOCR/deploy/fastdeploy/cpu-gpu/cpp
|
||||
|
||||
# 下载FastDeploy预编译库,用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用
|
||||
wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz
|
||||
tar xvf fastdeploy-linux-x64-x.x.x.tgz
|
||||
|
||||
# 编译部署示例
|
||||
mkdir build && cd build
|
||||
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x
|
||||
make -j
|
||||
|
||||
# 下载PP-OCRv3文字检测模型
|
||||
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar
|
||||
tar -xvf ch_PP-OCRv3_det_infer.tar
|
||||
# 下载文字方向分类器模型
|
||||
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
|
||||
tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar
|
||||
# 下载PP-OCRv3文字识别模型
|
||||
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar
|
||||
tar -xvf ch_PP-OCRv3_rec_infer.tar
|
||||
|
||||
# 下载预测图片与字典文件
|
||||
wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/doc/imgs/12.jpg
|
||||
wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/ppocr/utils/ppocr_keys_v1.txt
|
||||
|
||||
# 运行部署示例
|
||||
# 在CPU上使用Paddle Inference推理
|
||||
./infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 0
|
||||
# 在CPU上使用OenVINO推理
|
||||
./infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 1
|
||||
# 在CPU上使用ONNX Runtime推理
|
||||
./infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 2
|
||||
# 在CPU上使用Paddle Lite推理
|
||||
./infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 3
|
||||
# 在GPU上使用Paddle Inference推理
|
||||
./infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 4
|
||||
# 在GPU上使用Paddle TensorRT推理
|
||||
./infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 5
|
||||
# 在GPU上使用ONNX Runtime推理
|
||||
./infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 6
|
||||
# 在GPU上使用Nvidia TensorRT推理
|
||||
./infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 7
|
||||
|
||||
# 同时, FastDeploy提供文字检测,文字分类,文字识别三个模型的单独推理,
|
||||
# 有需要的用户, 请准备合适的图片, 同时根据自己的需求, 参考infer.cc来配置自定义硬件与推理后端.
|
||||
|
||||
# 在CPU上,单独使用文字检测模型部署
|
||||
./infer_det ./ch_PP-OCRv3_det_infer ./12.jpg 0
|
||||
|
||||
# 在CPU上,单独使用文字方向分类模型部署
|
||||
./infer_cls ./ch_ppocr_mobile_v2.0_cls_infer ./12.jpg 0
|
||||
|
||||
# 在CPU上,单独使用文字识别模型部署
|
||||
./infer_rec ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 0
|
||||
```
|
||||
|
||||
运行完成可视化结果如下图所示
|
||||
<div align="center">
|
||||
<img width="640" src="https://user-images.githubusercontent.com/109218879/185826024-f7593a0c-1bd2-4a60-b76c-15588484fa08.jpg">
|
||||
</div>
|
||||
|
||||
- 注意,以上命令只适用于Linux或MacOS, Windows下SDK的使用方式请参考文档: [如何在Windows中使用FastDeploy C++ SDK](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/faq/use_sdk_on_windows.md)
|
||||
- 关于如何通过FastDeploy使用更多不同的推理后端,以及如何使用不同的硬件,请参考文档:[如何切换模型推理后端引擎](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/faq/how_to_change_backend.md)
|
||||
|
||||
## 5. 部署示例选项说明
|
||||
在我们使用`infer_demo`时, 输入了6个参数, 分别为文字检测模型, 文字分类模型, 文字识别模型, 预测图片, 字典文件与最后一位的数字选项.
|
||||
现在下表将解释最后一位数字选项的含义.
|
||||
|数字选项|含义|
|
||||
|:---:|:---:|
|
||||
|0| 在CPU上使用Paddle Inference推理 |
|
||||
|1| 在CPU上使用OenVINO推理 |
|
||||
|2| 在CPU上使用ONNX Runtime推理 |
|
||||
|3| 在CPU上使用Paddle Lite推理 |
|
||||
|4| 在GPU上使用Paddle Inference推理 |
|
||||
|5| 在GPU上使用Paddle TensorRT推理 |
|
||||
|6| 在GPU上使用ONNX Runtime推理 |
|
||||
|7| 在GPU上使用Nvidia TensorRT推理 |
|
||||
|
||||
关于如何通过FastDeploy使用更多不同的推理后端,以及如何使用不同的硬件,请参考文档:[如何切换模型推理后端引擎](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/faq/how_to_change_backend.md)
|
||||
|
||||
## 6. 更多指南
|
||||
|
||||
### 6.1 如何使用C++部署PP-OCRv2系列模型.
|
||||
本目录下的`infer.cc`代码是以PP-OCRv3模型为例, 如果用户有使用PP-OCRv2的需求, 只需要按照下面所示的方式, 来创建PP-OCRv2并使用.
|
||||
|
||||
```cpp
|
||||
// 此行为创建PP-OCRv3模型的代码
|
||||
auto ppocr_v3 = fastdeploy::pipeline::PPOCRv3(&det_model, &cls_model, &rec_model);
|
||||
// 只需要将PPOCRv3改为PPOCRv2,即可创造PPOCRv2模型, 同时, 后续的接口均使用ppocr_v2来调用
|
||||
auto ppocr_v2 = fastdeploy::pipeline::PPOCRv2(&det_model, &cls_model, &rec_model);
|
||||
|
||||
// 如果用户在部署PP-OCRv2时, 需要使用TensorRT推理, 还需要改动Rec模型的TensorRT的输入shape.
|
||||
// 建议如下修改, 需要把 H 维度改为32, W 纬度按需修改.
|
||||
rec_option.SetTrtInputShape("x", {1, 3, 32, 10}, {rec_batch_size, 3, 32, 320},
|
||||
{rec_batch_size, 3, 32, 2304});
|
||||
```
|
||||
### 6.2 如何在PP-OCRv2/v3系列模型中, 关闭文字方向分类器的使用.
|
||||
|
||||
在PP-OCRv3/v2中, 文字方向分类器是可选的, 用户可以按照以下方式, 来决定自己是否使用方向分类器.
|
||||
```cpp
|
||||
// 使用 Cls 模型
|
||||
auto ppocr_v3 = fastdeploy::pipeline::PPOCRv3(&det_model, &cls_model, &rec_model);
|
||||
|
||||
// 不使用 Cls 模型
|
||||
auto ppocr_v3 = fastdeploy::pipeline::PPOCRv3(&det_model, &rec_model);
|
||||
|
||||
// 当不使用Cls模型时, 请删掉或者注释掉相关代码
|
||||
```
|
||||
|
||||
### 6.3 如何修改前后处理超参数.
|
||||
在示例代码中, 我们展示出了修改前后处理超参数的接口,并设置为默认值,其中, FastDeploy提供的超参数的含义与文档[PaddleOCR推理模型参数解释](https://github.com/PaddlePaddle/PaddleOCR/blob/dygraph/doc/doc_ch/inference_args.md)是相同的. 如果用户想要进行更多定制化的开发, 请阅读[PP-OCR系列 C++ API查阅](https://www.paddlepaddle.org.cn/fastdeploy-api-doc/cpp/html/namespacefastdeploy_1_1vision_1_1ocr.html)
|
||||
|
||||
```cpp
|
||||
// 设置检测模型的max_side_len
|
||||
det_model.GetPreprocessor().SetMaxSideLen(960);
|
||||
// 其他...
|
||||
```
|
||||
|
||||
### 6.4 其他指南
|
||||
- [FastDeploy部署PaddleOCR模型概览](../../)
|
||||
- [PP-OCRv3 Python部署](../python)
|
||||
- [PP-OCRv3 C 部署](../c)
|
||||
- [PP-OCRv3 C# 部署](../csharp)
|
||||
|
||||
## 7. 常见问题
|
||||
- PaddleOCR能在FastDeploy支持的多种后端上推理,支持情况如下表所示, 如何切换后端, 详见文档[如何切换模型推理后端引擎](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/faq/how_to_change_backend.md)
|
||||
|
||||
|硬件类型|支持的后端|
|
||||
|:---:|:---:|
|
||||
|X86 CPU| Paddle Inference, ONNX Runtime, OpenVINO |
|
||||
|ARM CPU| Paddle Lite |
|
||||
|飞腾 CPU| ONNX Runtime |
|
||||
|NVIDIA GPU| Paddle Inference, ONNX Runtime, TensorRT |
|
||||
|
||||
- [Intel GPU(独立显卡/集成显卡)的使用](https://github.com/PaddlePaddle/FastDeploy/blob/develop/tutorials/intel_gpu/README.md)
|
||||
- [编译CPU部署库](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/cpu.md)
|
||||
- [编译GPU部署库](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/gpu.md)
|
||||
- [编译Jetson部署库](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/jetson.md)
|
||||
@@ -0,0 +1,174 @@
|
||||
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "fastdeploy/vision.h"
|
||||
#ifdef WIN32
|
||||
const char sep = '\\';
|
||||
#else
|
||||
const char sep = '/';
|
||||
#endif
|
||||
|
||||
void InitAndInfer(const std::string &det_model_dir,
|
||||
const std::string &cls_model_dir,
|
||||
const std::string &rec_model_dir,
|
||||
const std::string &rec_label_file,
|
||||
const std::string &image_file,
|
||||
const fastdeploy::RuntimeOption &option) {
|
||||
auto det_model_file = det_model_dir + sep + "inference.pdmodel";
|
||||
auto det_params_file = det_model_dir + sep + "inference.pdiparams";
|
||||
|
||||
auto cls_model_file = cls_model_dir + sep + "inference.pdmodel";
|
||||
auto cls_params_file = cls_model_dir + sep + "inference.pdiparams";
|
||||
|
||||
auto rec_model_file = rec_model_dir + sep + "inference.pdmodel";
|
||||
auto rec_params_file = rec_model_dir + sep + "inference.pdiparams";
|
||||
|
||||
auto det_option = option;
|
||||
auto cls_option = option;
|
||||
auto rec_option = option;
|
||||
|
||||
// The cls and rec model can inference a batch of images now.
|
||||
// User could initialize the inference batch size and set them after create
|
||||
// PP-OCR model.
|
||||
int cls_batch_size = 1;
|
||||
int rec_batch_size = 6;
|
||||
|
||||
// If use TRT backend, the dynamic shape will be set as follow.
|
||||
// We recommend that users set the length and height of the detection model to
|
||||
// a multiple of 32.
|
||||
// We also recommend that users set the Trt input shape as follow.
|
||||
det_option.SetTrtInputShape("x", {1, 3, 64, 64}, {1, 3, 640, 640},
|
||||
{1, 3, 960, 960});
|
||||
cls_option.SetTrtInputShape("x", {1, 3, 48, 10}, {cls_batch_size, 3, 48, 320},
|
||||
{cls_batch_size, 3, 48, 1024});
|
||||
rec_option.SetTrtInputShape("x", {1, 3, 48, 10}, {rec_batch_size, 3, 48, 320},
|
||||
{rec_batch_size, 3, 48, 2304});
|
||||
|
||||
// Users could save TRT cache file to disk as follow.
|
||||
// det_option.SetTrtCacheFile(det_model_dir + sep + "det_trt_cache.trt");
|
||||
// cls_option.SetTrtCacheFile(cls_model_dir + sep + "cls_trt_cache.trt");
|
||||
// rec_option.SetTrtCacheFile(rec_model_dir + sep + "rec_trt_cache.trt");
|
||||
|
||||
auto det_model = fastdeploy::vision::ocr::DBDetector(
|
||||
det_model_file, det_params_file, det_option);
|
||||
auto cls_model = fastdeploy::vision::ocr::Classifier(
|
||||
cls_model_file, cls_params_file, cls_option);
|
||||
auto rec_model = fastdeploy::vision::ocr::Recognizer(
|
||||
rec_model_file, rec_params_file, rec_label_file, rec_option);
|
||||
|
||||
assert(det_model.Initialized());
|
||||
assert(cls_model.Initialized());
|
||||
assert(rec_model.Initialized());
|
||||
|
||||
// Parameters settings for pre and post processing of Det/Cls/Rec Models.
|
||||
// All parameters are set to default values.
|
||||
det_model.GetPreprocessor().SetMaxSideLen(960);
|
||||
det_model.GetPostprocessor().SetDetDBThresh(0.3);
|
||||
det_model.GetPostprocessor().SetDetDBBoxThresh(0.6);
|
||||
det_model.GetPostprocessor().SetDetDBUnclipRatio(1.5);
|
||||
det_model.GetPostprocessor().SetDetDBScoreMode("slow");
|
||||
det_model.GetPostprocessor().SetUseDilation(0);
|
||||
cls_model.GetPostprocessor().SetClsThresh(0.9);
|
||||
|
||||
// The classification model is optional, so the PP-OCR can also be connected
|
||||
// in series as follows
|
||||
// auto ppocr_v3 = fastdeploy::pipeline::PPOCRv3(&det_model, &rec_model);
|
||||
auto ppocr_v3 =
|
||||
fastdeploy::pipeline::PPOCRv3(&det_model, &cls_model, &rec_model);
|
||||
|
||||
// Set inference batch size for cls model and rec model, the value could be -1
|
||||
// and 1 to positive infinity.
|
||||
// When inference batch size is set to -1, it means that the inference batch
|
||||
// size
|
||||
// of the cls and rec models will be the same as the number of boxes detected
|
||||
// by the det model.
|
||||
ppocr_v3.SetClsBatchSize(cls_batch_size);
|
||||
ppocr_v3.SetRecBatchSize(rec_batch_size);
|
||||
|
||||
if (!ppocr_v3.Initialized()) {
|
||||
std::cerr << "Failed to initialize PP-OCR." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
auto im_bak = im.clone();
|
||||
|
||||
fastdeploy::vision::OCRResult result;
|
||||
if (!ppocr_v3.Predict(&im, &result)) {
|
||||
std::cerr << "Failed to predict." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << result.Str() << std::endl;
|
||||
|
||||
auto vis_im = fastdeploy::vision::VisOcr(im_bak, result);
|
||||
cv::imwrite("vis_result.jpg", vis_im);
|
||||
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 7) {
|
||||
std::cout << "Usage: infer_demo path/to/det_model path/to/cls_model "
|
||||
"path/to/rec_model path/to/rec_label_file path/to/image "
|
||||
"run_option, "
|
||||
"e.g ./infer_demo ./ch_PP-OCRv3_det_infer "
|
||||
"./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer "
|
||||
"./ppocr_keys_v1.txt ./12.jpg 0"
|
||||
<< std::endl;
|
||||
std::cout << "The data type of run_option is int, e.g. 0: run with paddle "
|
||||
"inference on cpu;"
|
||||
<< std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
fastdeploy::RuntimeOption option;
|
||||
int flag = std::atoi(argv[6]);
|
||||
|
||||
if (flag == 0) {
|
||||
option.UseCpu();
|
||||
option.UsePaddleBackend(); // Paddle Inference
|
||||
} else if (flag == 1) {
|
||||
option.UseCpu();
|
||||
option.UseOpenVINOBackend(); // OpenVINO
|
||||
} else if (flag == 2) {
|
||||
option.UseCpu();
|
||||
option.UseOrtBackend(); // ONNX Runtime
|
||||
} else if (flag == 3) {
|
||||
option.UseCpu();
|
||||
option.UseLiteBackend(); // Paddle Lite
|
||||
} else if (flag == 4) {
|
||||
option.UseGpu();
|
||||
option.UsePaddleBackend(); // Paddle Inference
|
||||
} else if (flag == 5) {
|
||||
option.UseGpu();
|
||||
option.UsePaddleInferBackend();
|
||||
option.paddle_infer_option.collect_trt_shape = true;
|
||||
option.paddle_infer_option.enable_trt = true; // Paddle-TensorRT
|
||||
} else if (flag == 6) {
|
||||
option.UseGpu();
|
||||
option.UseOrtBackend(); // ONNX Runtime
|
||||
} else if (flag == 7) {
|
||||
option.UseGpu();
|
||||
option.UseTrtBackend(); // TensorRT
|
||||
}
|
||||
|
||||
std::string det_model_dir = argv[1];
|
||||
std::string cls_model_dir = argv[2];
|
||||
std::string rec_model_dir = argv[3];
|
||||
std::string rec_label_file = argv[4];
|
||||
std::string test_image = argv[5];
|
||||
InitAndInfer(det_model_dir, cls_model_dir, rec_model_dir, rec_label_file,
|
||||
test_image, option);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "fastdeploy/vision.h"
|
||||
#ifdef WIN32
|
||||
const char sep = '\\';
|
||||
#else
|
||||
const char sep = '/';
|
||||
#endif
|
||||
|
||||
void InitAndInfer(const std::string &cls_model_dir,
|
||||
const std::string &image_file,
|
||||
const fastdeploy::RuntimeOption &option) {
|
||||
auto cls_model_file = cls_model_dir + sep + "inference.pdmodel";
|
||||
auto cls_params_file = cls_model_dir + sep + "inference.pdiparams";
|
||||
auto cls_option = option;
|
||||
|
||||
auto cls_model = fastdeploy::vision::ocr::Classifier(
|
||||
cls_model_file, cls_params_file, cls_option);
|
||||
assert(cls_model.Initialized());
|
||||
|
||||
// Parameters settings for pre and post processing of Cls Model.
|
||||
cls_model.GetPostprocessor().SetClsThresh(0.9);
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
auto im_bak = im.clone();
|
||||
|
||||
fastdeploy::vision::OCRResult result;
|
||||
if (!cls_model.Predict(im, &result)) {
|
||||
std::cerr << "Failed to predict." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// User can infer a batch of images by following code.
|
||||
// if (!cls_model.BatchPredict({im}, &result)) {
|
||||
// std::cerr << "Failed to predict." << std::endl;
|
||||
// return;
|
||||
// }
|
||||
|
||||
std::cout << result.Str() << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 4) {
|
||||
std::cout << "Usage: infer_demo path/to/cls_model path/to/image "
|
||||
"run_option, "
|
||||
"e.g ./infer_demo ./ch_ppocr_mobile_v2.0_cls_infer ./12.jpg 0"
|
||||
<< std::endl;
|
||||
std::cout << "The data type of run_option is int, 0: run with cpu; 1: run "
|
||||
"with gpu;."
|
||||
<< std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
fastdeploy::RuntimeOption option;
|
||||
int flag = std::atoi(argv[3]);
|
||||
|
||||
if (flag == 0) {
|
||||
option.UseCpu();
|
||||
} else if (flag == 1) {
|
||||
option.UseGpu();
|
||||
}
|
||||
|
||||
std::string cls_model_dir = argv[1];
|
||||
std::string test_image = argv[2];
|
||||
InitAndInfer(cls_model_dir, test_image, option);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "fastdeploy/vision.h"
|
||||
#ifdef WIN32
|
||||
const char sep = '\\';
|
||||
#else
|
||||
const char sep = '/';
|
||||
#endif
|
||||
|
||||
void InitAndInfer(const std::string &det_model_dir,
|
||||
const std::string &image_file,
|
||||
const fastdeploy::RuntimeOption &option) {
|
||||
auto det_model_file = det_model_dir + sep + "inference.pdmodel";
|
||||
auto det_params_file = det_model_dir + sep + "inference.pdiparams";
|
||||
auto det_option = option;
|
||||
|
||||
auto det_model = fastdeploy::vision::ocr::DBDetector(
|
||||
det_model_file, det_params_file, det_option);
|
||||
assert(det_model.Initialized());
|
||||
|
||||
// Parameters settings for pre and post processing of Det Model.
|
||||
det_model.GetPreprocessor().SetMaxSideLen(960);
|
||||
det_model.GetPostprocessor().SetDetDBThresh(0.3);
|
||||
det_model.GetPostprocessor().SetDetDBBoxThresh(0.6);
|
||||
det_model.GetPostprocessor().SetDetDBUnclipRatio(1.5);
|
||||
det_model.GetPostprocessor().SetDetDBScoreMode("slow");
|
||||
det_model.GetPostprocessor().SetUseDilation(0);
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
auto im_bak = im.clone();
|
||||
|
||||
fastdeploy::vision::OCRResult result;
|
||||
if (!det_model.Predict(im, &result)) {
|
||||
std::cerr << "Failed to predict." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << result.Str() << std::endl;
|
||||
|
||||
auto vis_im = fastdeploy::vision::VisOcr(im_bak, result);
|
||||
cv::imwrite("vis_result.jpg", vis_im);
|
||||
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 4) {
|
||||
std::cout << "Usage: infer_demo path/to/det_model path/to/image "
|
||||
"run_option, "
|
||||
"e.g ./infer_demo ./ch_PP-OCRv3_det_infer ./12.jpg 0"
|
||||
<< std::endl;
|
||||
std::cout << "The data type of run_option is int, 0: run with cpu; 1: run "
|
||||
"with gpu;."
|
||||
<< std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
fastdeploy::RuntimeOption option;
|
||||
int flag = std::atoi(argv[3]);
|
||||
|
||||
if (flag == 0) {
|
||||
option.UseCpu();
|
||||
} else if (flag == 1) {
|
||||
option.UseGpu();
|
||||
}
|
||||
|
||||
std::string det_model_dir = argv[1];
|
||||
std::string test_image = argv[2];
|
||||
InitAndInfer(det_model_dir, test_image, option);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "fastdeploy/vision.h"
|
||||
#ifdef WIN32
|
||||
const char sep = '\\';
|
||||
#else
|
||||
const char sep = '/';
|
||||
#endif
|
||||
|
||||
void InitAndInfer(const std::string &rec_model_dir,
|
||||
const std::string &rec_label_file,
|
||||
const std::string &image_file,
|
||||
const fastdeploy::RuntimeOption &option) {
|
||||
auto rec_model_file = rec_model_dir + sep + "inference.pdmodel";
|
||||
auto rec_params_file = rec_model_dir + sep + "inference.pdiparams";
|
||||
auto rec_option = option;
|
||||
|
||||
auto rec_model = fastdeploy::vision::ocr::Recognizer(
|
||||
rec_model_file, rec_params_file, rec_label_file, rec_option);
|
||||
|
||||
assert(rec_model.Initialized());
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
auto im_bak = im.clone();
|
||||
|
||||
fastdeploy::vision::OCRResult result;
|
||||
|
||||
if (!rec_model.Predict(im, &result)) {
|
||||
std::cerr << "Failed to predict." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// User can infer a batch of images by following code.
|
||||
// if (!rec_model.BatchPredict({im}, &result)) {
|
||||
// std::cerr << "Failed to predict." << std::endl;
|
||||
// return;
|
||||
// }
|
||||
|
||||
std::cout << result.Str() << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 5) {
|
||||
std::cout << "Usage: infer_demo"
|
||||
"path/to/rec_model path/to/rec_label_file path/to/image "
|
||||
"run_option, "
|
||||
"e.g ./infer_demo "
|
||||
"./ch_PP-OCRv3_rec_infer "
|
||||
"./ppocr_keys_v1.txt ./12.jpg 0"
|
||||
<< std::endl;
|
||||
std::cout << "The data type of run_option is int, 0: run with cpu; 1: run "
|
||||
"with gpu;"
|
||||
<< std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
fastdeploy::RuntimeOption option;
|
||||
int flag = std::atoi(argv[4]);
|
||||
|
||||
if (flag == 0) {
|
||||
option.UseCpu();
|
||||
} else if (flag == 1) {
|
||||
option.UseGpu();
|
||||
}
|
||||
|
||||
std::string rec_model_dir = argv[1];
|
||||
std::string rec_label_file = argv[2];
|
||||
std::string test_image = argv[3];
|
||||
InitAndInfer(rec_model_dir, rec_label_file, test_image, option);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
PROJECT(infer_demo CSharp)
|
||||
CMAKE_MINIMUM_REQUIRED (VERSION 3.10)
|
||||
|
||||
# Set the C# language version (defaults to 3.0 if not set).
|
||||
set(CMAKE_CSharp_FLAGS "/langversion:10")
|
||||
set(CMAKE_DOTNET_TARGET_FRAMEWORK "net6.0")
|
||||
set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk")
|
||||
|
||||
# 指定下载解压后的fastdeploy库路径
|
||||
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
|
||||
|
||||
include(${FASTDEPLOY_INSTALL_DIR}/FastDeployCSharp.cmake)
|
||||
|
||||
|
||||
add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cs)
|
||||
|
||||
set_property(TARGET infer_demo PROPERTY VS_DOTNET_REFERENCES
|
||||
${FASTDEPLOY_DOTNET_REFERENCES}
|
||||
)
|
||||
|
||||
set_property(TARGET infer_demo
|
||||
PROPERTY VS_PACKAGE_REFERENCES ${FASTDEPLOY_PACKAGE_REFERENCES})
|
||||
@@ -0,0 +1,173 @@
|
||||
[English](README.md) | 简体中文
|
||||
# PaddleOCR CPU-GPU C#部署示例
|
||||
|
||||
本目录下提供`infer.cs`来调用C# API快速完成PPOCRv3模型在CPU/GPU上部署的示例。
|
||||
|
||||
## 1. 说明
|
||||
PaddleOCR支持利用FastDeploy在NVIDIA GPU、X86 CPU、飞腾CPU、ARM CPU、Intel GPU(独立显卡/集成显卡)硬件上快速部署OCR模型.
|
||||
|
||||
## 2. 部署环境准备
|
||||
在部署前,需确认软硬件环境,同时下载预编译部署库,参考[FastDeploy安装文档](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install#FastDeploy预编译库安装)安装FastDeploy预编译库. 在本目录执行如下命令即可在Windows完成编译测试,支持此模型需保证FastDeploy版本1.0.4以上(x.x.x>=1.0.4)
|
||||
|
||||
## 3. 部署模型准备
|
||||
在部署前, 请准备好您所需要运行的推理模型, 您可以在[FastDeploy支持的PaddleOCR模型列表](../README.md)中下载所需模型.
|
||||
|
||||
## 4. 部署示例
|
||||
|
||||
### 4.1 下载C#包管理程序nuget客户端
|
||||
> https://dist.nuget.org/win-x86-commandline/v6.4.0/nuget.exe
|
||||
下载完成后将该程序添加到环境变量**PATH**中
|
||||
|
||||
### 4.2. 下载模型文件和测试图片
|
||||
> https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar # (下载后解压缩)
|
||||
> https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
|
||||
> https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar
|
||||
> https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/doc/imgs/12.jpg
|
||||
> https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/ppocr/utils/ppocr_keys_v1.txt
|
||||
|
||||
### 4.3 编译示例代码
|
||||
|
||||
本文档编译的示例代码的编译工具依赖VS 2019,**Windows打开x64 Native Tools Command Prompt for VS 2019命令工具**,通过如下命令开始编译
|
||||
|
||||
|
||||
```shell
|
||||
## 下载FastDeploy预编译库,用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用
|
||||
https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz
|
||||
|
||||
# 下载部署示例代码
|
||||
git clone https://github.com/PaddlePaddle/FastDeploy.git
|
||||
cd D:\FastDeploy\examples\vision\ocr\PP-OCR\cpu-gpu\csharp
|
||||
|
||||
# 如果您希望从PaddleOCR下载示例代码,请运行
|
||||
git clone https://github.com/PaddlePaddle/PaddleOCR.git
|
||||
# 注意:如果当前分支找不到下面的fastdeploy测试代码,请切换到dygraph分支
|
||||
git checkout dygraph
|
||||
cd D:\PaddleOCR\deploy\fastdeploy\cpu-gpu\csharp
|
||||
|
||||
mkdir build && cd build
|
||||
cmake .. -G "Visual Studio 16 2019" -A x64 -DFASTDEPLOY_INSTALL_DIR=D:\fastdeploy-win-x64-gpu-x.x.x -DCUDA_DIRECTORY="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.2"
|
||||
|
||||
nuget restore
|
||||
msbuild infer_demo.sln /m:4 /p:Configuration=Release /p:Platform=x64
|
||||
```
|
||||
|
||||
关于使用Visual Studio 2019创建sln工程,或者CMake工程等方式编译的更详细信息,可参考如下文档
|
||||
- [在 Windows 使用 FastDeploy C++ SDK](https://github.com/PaddlePaddle/FastDeploy/tree/develop/docs/cn/faq/use_sdk_on_windows.md)
|
||||
- [FastDeploy C++库在Windows上的多种使用方式](https://github.com/PaddlePaddle/FastDeploy/tree/develop/docs/cn/faq/use_sdk_on_windows_build.md)
|
||||
|
||||
### 4.4 运行可执行程序
|
||||
|
||||
注意Windows上运行时,需要将FastDeploy依赖的库拷贝至可执行程序所在目录, 或者配置环境变量。FastDeploy提供了工具帮助我们快速将所有依赖库拷贝至可执行程序所在目录,通过如下命令将所有依赖的dll文件拷贝至可执行程序所在的目录(可能生成的可执行文件在Release下还有一层目录,这里假设生成的可执行文件在Release处)
|
||||
```shell
|
||||
cd D:\fastdeploy-win-x64-gpu-x.x.x
|
||||
|
||||
fastdeploy_init.bat install %cd% D:\PaddleOCR\deploy\fastdeploy\cpu-gpu\csharp\build\Release
|
||||
```
|
||||
|
||||
将dll拷贝到当前路径后,准备好模型和图片,使用如下命令运行可执行程序即可
|
||||
```shell
|
||||
cd Release
|
||||
# CPU推理
|
||||
infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v3.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 0
|
||||
# GPU推理
|
||||
infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v3.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 1
|
||||
```
|
||||
|
||||
## 5. PP-OCRv3 C# API接口简介
|
||||
下面提供了PP-OCRv3的C# API简介
|
||||
|
||||
- 如果用户想要更换部署后端或进行其他定制化操作, 请查看[C# Runtime API](https://baidu-paddle.github.io/fastdeploy-api/csharp/html/classfastdeploy_1_1RuntimeOption.html).
|
||||
- 更多 PP-OCR C# API 请查看 [C# PP-OCR API](https://github.com/PaddlePaddle/FastDeploy/blob/develop/csharp/fastdeploy/vision/ocr/model.cs)
|
||||
|
||||
### 模型
|
||||
|
||||
```c#
|
||||
fastdeploy.vision.ocr.DBDetector(
|
||||
string model_file,
|
||||
string params_file,
|
||||
fastdeploy.RuntimeOption runtime_option = null,
|
||||
fastdeploy.ModelFormat model_format = ModelFormat.PADDLE)
|
||||
```
|
||||
|
||||
> DBDetector模型加载和初始化。
|
||||
|
||||
> **参数**
|
||||
|
||||
>> * **model_file**(str): 模型文件路径
|
||||
>> * **params_file**(str): 参数文件路径
|
||||
>> * **runtime_option**(RuntimeOption): 后端推理配置,默认为null,即采用默认配置
|
||||
>> * **model_format**(ModelFormat): 模型格式,默认为PADDLE格式
|
||||
|
||||
```c#
|
||||
fastdeploy.vision.ocr.Classifier(
|
||||
string model_file,
|
||||
string params_file,
|
||||
fastdeploy.RuntimeOption runtime_option = null,
|
||||
fastdeploy.ModelFormat model_format = ModelFormat.PADDLE)
|
||||
```
|
||||
|
||||
> Classifier模型加载和初始化。
|
||||
|
||||
> **参数**
|
||||
|
||||
>> * **model_file**(str): 模型文件路径
|
||||
>> * **params_file**(str): 参数文件路径
|
||||
>> * **runtime_option**(RuntimeOption): 后端推理配置,默认为null,即采用默认配置
|
||||
>> * **model_format**(ModelFormat): 模型格式,默认为PADDLE格式
|
||||
|
||||
```c#
|
||||
fastdeploy.vision.ocr.Recognizer(
|
||||
string model_file,
|
||||
string params_file,
|
||||
string label_path,
|
||||
fastdeploy.RuntimeOption runtime_option = null,
|
||||
fastdeploy.ModelFormat model_format = ModelFormat.PADDLE)
|
||||
```
|
||||
|
||||
> Recognizer模型加载和初始化。
|
||||
|
||||
> **参数**
|
||||
|
||||
>> * **model_file**(str): 模型文件路径
|
||||
>> * **params_file**(str): 参数文件路径
|
||||
>> * **label_path**(str): 标签文件路径
|
||||
>> * **runtime_option**(RuntimeOption): 后端推理配置,默认为null,即采用默认配置
|
||||
>> * **model_format**(ModelFormat): 模型格式,默认为PADDLE格式
|
||||
|
||||
```c#
|
||||
fastdeploy.pipeline.PPOCRv3Model(
|
||||
DBDetector dbdetector,
|
||||
Classifier classifier,
|
||||
Recognizer recognizer)
|
||||
```
|
||||
|
||||
> PP-OCRv3Model模型加载和初始化。
|
||||
|
||||
> **参数**
|
||||
|
||||
>> * **det_model**(FD_C_DBDetectorWrapper*): DBDetector模型
|
||||
>> * **cls_model**(FD_C_ClassifierWrapper*): Classifier模型
|
||||
>> * **rec_model**(FD_C_RecognizerWrapper*): Recognizer模型文件
|
||||
|
||||
#### Predict函数
|
||||
|
||||
```c#
|
||||
fastdeploy.OCRResult Predict(OpenCvSharp.Mat im)
|
||||
```
|
||||
|
||||
> 模型预测接口,输入图像直接输出结果。
|
||||
>
|
||||
> **参数**
|
||||
>
|
||||
>> * **im**(Mat): 输入图像,注意需为HWC,BGR格式
|
||||
>>
|
||||
> **返回值**
|
||||
>
|
||||
>> * **result**: OCR预测结果,包括由检测模型输出的检测框位置,分类模型输出的方向分类,以及识别模型输出的识别结果, OCRResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/)
|
||||
|
||||
|
||||
## 6. 其它文档
|
||||
- [FastDeploy部署PaddleOCR模型概览](../../)
|
||||
- [PP-OCRv3 Python部署](../python)
|
||||
- [PP-OCRv3 C++ 部署](../cpp)
|
||||
- [PP-OCRv3 C 部署](../c)
|
||||
@@ -0,0 +1,79 @@
|
||||
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using OpenCvSharp;
|
||||
using fastdeploy;
|
||||
|
||||
namespace Test
|
||||
{
|
||||
public class TestPPOCRv3
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
if (args.Length < 6) {
|
||||
Console.WriteLine(
|
||||
"Usage: infer_demo path/to/det_model path/to/cls_model " +
|
||||
"path/to/rec_model path/to/rec_label_file path/to/image " +
|
||||
"run_option, " +
|
||||
"e.g ./infer_demo ./ch_PP-OCRv2_det_infer " +
|
||||
"./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv2_rec_infer " +
|
||||
"./ppocr_keys_v1.txt ./12.jpg 0"
|
||||
);
|
||||
Console.WriteLine( "The data type of run_option is int, 0: run with cpu; 1: run with gpu");
|
||||
return;
|
||||
}
|
||||
string det_model_dir = args[0];
|
||||
string cls_model_dir = args[1];
|
||||
string rec_model_dir = args[2];
|
||||
string rec_label_file = args[3];
|
||||
string image_path = args[4];
|
||||
RuntimeOption runtimeoption = new RuntimeOption();
|
||||
int device_option = Int32.Parse(args[5]);
|
||||
if(device_option==0){
|
||||
runtimeoption.UseCpu();
|
||||
}else{
|
||||
runtimeoption.UseGpu();
|
||||
}
|
||||
string sep = "\\";
|
||||
string det_model_file = det_model_dir + sep + "inference.pdmodel";
|
||||
string det_params_file = det_model_dir + sep + "inference.pdiparams";
|
||||
|
||||
string cls_model_file = cls_model_dir + sep + "inference.pdmodel";
|
||||
string cls_params_file = cls_model_dir + sep + "inference.pdiparams";
|
||||
|
||||
string rec_model_file = rec_model_dir + sep + "inference.pdmodel";
|
||||
string rec_params_file = rec_model_dir + sep + "inference.pdiparams";
|
||||
|
||||
fastdeploy.vision.ocr.DBDetector dbdetector = new fastdeploy.vision.ocr.DBDetector(det_model_file, det_params_file, runtimeoption, ModelFormat.PADDLE);
|
||||
fastdeploy.vision.ocr.Classifier classifier = new fastdeploy.vision.ocr.Classifier(cls_model_file, cls_params_file, runtimeoption, ModelFormat.PADDLE);
|
||||
fastdeploy.vision.ocr.Recognizer recognizer = new fastdeploy.vision.ocr.Recognizer(rec_model_file, rec_params_file, rec_label_file, runtimeoption, ModelFormat.PADDLE);
|
||||
fastdeploy.pipeline.PPOCRv3 model = new fastdeploy.pipeline.PPOCRv3(dbdetector, classifier, recognizer);
|
||||
if(!model.Initialized()){
|
||||
Console.WriteLine("Failed to initialize.\n");
|
||||
}
|
||||
Mat image = Cv2.ImRead(image_path);
|
||||
fastdeploy.vision.OCRResult res = model.Predict(image);
|
||||
Console.WriteLine(res.ToString());
|
||||
Mat res_img = fastdeploy.vision.Visualize.VisOcr(image, res);
|
||||
Cv2.ImShow("result.png", res_img);
|
||||
Cv2.ImWrite("result.png", res_img);
|
||||
Cv2.WaitKey(0);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
[English](README.md) | 简体中文
|
||||
# PaddleOCR CPU-GPU Python部署示例
|
||||
本目录下提供`infer.py`快速完成PP-OCRv3在CPU/GPU,以及GPU上通过Paddle-TensorRT加速部署的示例.
|
||||
|
||||
## 1. 说明
|
||||
PaddleOCR支持利用FastDeploy在NVIDIA GPU、X86 CPU、飞腾CPU、ARM CPU、Intel GPU(独立显卡/集成显卡)硬件上快速部署OCR模型
|
||||
|
||||
## 2. 部署环境准备
|
||||
在部署前,需确认软硬件环境,同时下载预编译部署库,参考[FastDeploy安装文档](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install#FastDeploy预编译库安装)安装FastDeploy预编译库.
|
||||
|
||||
## 3. 部署模型准备
|
||||
在部署前, 请准备好您所需要运行的推理模型, 您可以在[FastDeploy支持的PaddleOCR模型列表](../README.md)中下载所需模型.
|
||||
|
||||
## 4. 运行部署示例
|
||||
```bash
|
||||
# 安装FastDpeloy python包(详细文档请参考`部署环境准备`)
|
||||
pip install fastdeploy-gpu-python -f https://www.paddlepaddle.org.cn/whl/fastdeploy.html
|
||||
conda config --add channels conda-forge && conda install cudatoolkit=11.2 cudnn=8.2
|
||||
|
||||
# 下载部署示例代码
|
||||
git clone https://github.com/PaddlePaddle/FastDeploy.git
|
||||
cd FastDeploy/examples/vision/ocr/PP-OCR/cpu-gpu/python
|
||||
|
||||
# 如果您希望从PaddleOCR下载示例代码,请运行
|
||||
git clone https://github.com/PaddlePaddle/PaddleOCR.git
|
||||
# 注意:如果当前分支找不到下面的fastdeploy测试代码,请切换到dygraph分支
|
||||
git checkout dygraph
|
||||
cd PaddleOCR/deploy/fastdeploy/cpu-gpu/python
|
||||
|
||||
# 下载PP-OCRv3文字检测模型
|
||||
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar
|
||||
tar -xvf ch_PP-OCRv3_det_infer.tar
|
||||
# 下载文字方向分类器模型
|
||||
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
|
||||
tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar
|
||||
# 下载PP-OCRv3文字识别模型
|
||||
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar
|
||||
tar -xvf ch_PP-OCRv3_rec_infer.tar
|
||||
|
||||
# 下载预测图片与字典文件
|
||||
wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/doc/imgs/12.jpg
|
||||
wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/ppocr/utils/ppocr_keys_v1.txt
|
||||
|
||||
# 运行部署示例
|
||||
# 在CPU上使用Paddle Inference推理
|
||||
python infer.py --det_model ch_PP-OCRv3_det_infer --cls_model ch_ppocr_mobile_v2.0_cls_infer --rec_model ch_PP-OCRv3_rec_infer --rec_label_file ppocr_keys_v1.txt --image 12.jpg --device cpu --backend paddle
|
||||
# 在CPU上使用OenVINO推理
|
||||
python infer.py --det_model ch_PP-OCRv3_det_infer --cls_model ch_ppocr_mobile_v2.0_cls_infer --rec_model ch_PP-OCRv3_rec_infer --rec_label_file ppocr_keys_v1.txt --image 12.jpg --device cpu --backend openvino
|
||||
# 在CPU上使用ONNX Runtime推理
|
||||
python infer.py --det_model ch_PP-OCRv3_det_infer --cls_model ch_ppocr_mobile_v2.0_cls_infer --rec_model ch_PP-OCRv3_rec_infer --rec_label_file ppocr_keys_v1.txt --image 12.jpg --device cpu --backend ort
|
||||
# 在CPU上使用Paddle Lite推理
|
||||
python infer.py --det_model ch_PP-OCRv3_det_infer --cls_model ch_ppocr_mobile_v2.0_cls_infer --rec_model ch_PP-OCRv3_rec_infer --rec_label_file ppocr_keys_v1.txt --image 12.jpg --device cpu --backend pplite
|
||||
# 在GPU上使用Paddle Inference推理
|
||||
python infer.py --det_model ch_PP-OCRv3_det_infer --cls_model ch_ppocr_mobile_v2.0_cls_infer --rec_model ch_PP-OCRv3_rec_infer --rec_label_file ppocr_keys_v1.txt --image 12.jpg --device gpu --backend paddle
|
||||
# 在GPU上使用Paddle TensorRT推理
|
||||
python infer.py --det_model ch_PP-OCRv3_det_infer --cls_model ch_ppocr_mobile_v2.0_cls_infer --rec_model ch_PP-OCRv3_rec_infer --rec_label_file ppocr_keys_v1.txt --image 12.jpg --device gpu --backend pptrt
|
||||
# 在GPU上使用ONNX Runtime推理
|
||||
python infer.py --det_model ch_PP-OCRv3_det_infer --cls_model ch_ppocr_mobile_v2.0_cls_infer --rec_model ch_PP-OCRv3_rec_infer --rec_label_file ppocr_keys_v1.txt --image 12.jpg --device gpu --backend ort
|
||||
# 在GPU上使用Nvidia TensorRT推理
|
||||
python infer.py --det_model ch_PP-OCRv3_det_infer --cls_model ch_ppocr_mobile_v2.0_cls_infer --rec_model ch_PP-OCRv3_rec_infer --rec_label_file ppocr_keys_v1.txt --image 12.jpg --device gpu --backend trt
|
||||
|
||||
# 同时, FastDeploy提供文字检测,文字分类,文字识别三个模型的单独推理,
|
||||
# 有需要的用户, 请准备合适的图片, 同时根据自己的需求, 参考infer.py来配置自定义硬件与推理后端.
|
||||
|
||||
# 在CPU上,单独使用文字检测模型部署
|
||||
python infer_det.py --det_model ch_PP-OCRv3_det_infer --image 12.jpg --device cpu
|
||||
|
||||
# 在CPU上,单独使用文字方向分类模型部署
|
||||
python infer_cls.py --cls_model ch_ppocr_mobile_v2.0_cls_infer --image 12.jpg --device cpu
|
||||
|
||||
# 在CPU上,单独使用文字识别模型部署
|
||||
python infer_rec.py --rec_model ch_PP-OCRv3_rec_infer --rec_label_file ppocr_keys_v1.txt --image 12.jpg --device cpu
|
||||
|
||||
```
|
||||
|
||||
运行完成可视化结果如下图所示
|
||||
<div align="center">
|
||||
<img width="640" src="https://user-images.githubusercontent.com/109218879/185826024-f7593a0c-1bd2-4a60-b76c-15588484fa08.jpg">
|
||||
</div>
|
||||
|
||||
## 5. 部署示例选项说明
|
||||
|
||||
|参数|含义|默认值
|
||||
|---|---|---|
|
||||
|--det_model|指定检测模型文件夹所在的路径|None|
|
||||
|--cls_model|指定分类模型文件夹所在的路径|None|
|
||||
|--rec_model|指定识别模型文件夹所在的路径|None|
|
||||
|--rec_label_file|识别模型所需label所在的路径|None|
|
||||
|--image|指定测试图片所在的路径|None|
|
||||
|--device|指定即将运行的硬件类型,支持的值为`[cpu, gpu]`,当设置为cpu时,可运行在x86 cpu/arm cpu等cpu上|cpu|
|
||||
|--device_id|使用gpu时, 指定设备号|0|
|
||||
|--backend|部署模型时使用的后端, 支持的值为`[paddle,pptrt,pplite,ort,openvino,trt]` |paddle|
|
||||
|
||||
关于如何通过FastDeploy使用更多不同的推理后端,以及如何使用不同的硬件,请参考文档:[如何切换模型推理后端引擎](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/faq/how_to_change_backend.md)
|
||||
|
||||
## 6. 更多指南
|
||||
|
||||
### 6.1 如何使用Python部署PP-OCRv2系列模型.
|
||||
本目录下的`infer.py`代码是以PP-OCRv3模型为例, 如果用户有使用PP-OCRv2的需求, 只需要按照下面所示的方式, 来创建PP-OCRv2并使用.
|
||||
|
||||
```python
|
||||
# 此行为创建PP-OCRv3模型的代码
|
||||
ppocr_v3 = fd.vision.ocr.PPOCRv3(det_model=det_model, cls_model=cls_model, rec_model=rec_model)
|
||||
# 只需要将PPOCRv3改为PPOCRv2,即可创造PPOCRv2模型, 同时, 后续的接口均使用ppocr_v2来调用
|
||||
ppocr_v2 = fd.vision.ocr.PPOCRv2(det_model=det_model, cls_model=cls_model, rec_model=rec_model)
|
||||
|
||||
# 如果用户在部署PP-OCRv2时, 需要使用TensorRT推理, 还需要改动Rec模型的TensorRT的输入shape.
|
||||
# 建议如下修改, 需要把 H 维度改为32, W 纬度按需修改.
|
||||
rec_option.set_trt_input_shape("x", [1, 3, 32, 10],
|
||||
[args.rec_bs, 3, 32, 320],
|
||||
[args.rec_bs, 3, 32, 2304])
|
||||
```
|
||||
|
||||
### 6.2 如何在PP-OCRv2/v3系列模型中, 关闭文字方向分类器的使用.
|
||||
|
||||
在PP-OCRv3/v2中, 文字方向分类器是可选的, 用户可以按照以下方式, 来决定自己是否使用方向分类器.
|
||||
```python
|
||||
# 使用 Cls 模型
|
||||
ppocr_v3 = fd.vision.ocr.PPOCRv3(det_model=det_model, cls_model=cls_model, rec_model=rec_model)
|
||||
|
||||
# 不使用 Cls 模型
|
||||
ppocr_v3 = fd.vision.ocr.PPOCRv3(det_model=det_model, cls_model=None, rec_model=rec_model)
|
||||
```
|
||||
### 6.3 如何修改前后处理超参数.
|
||||
在示例代码中, 我们展示出了修改前后处理超参数的接口,并设置为默认值,其中, FastDeploy提供的超参数的含义与文档[PaddleOCR推理模型参数解释](https://github.com/PaddlePaddle/PaddleOCR/blob/dygraph/doc/doc_ch/inference_args.md)是相同的. 如果用户想要进行更多定制化的开发, 请阅读[PP-OCR系列 Python API查阅](https://www.paddlepaddle.org.cn/fastdeploy-api-doc/python/html/ocr.html)
|
||||
|
||||
```python
|
||||
# 设置检测模型的max_side_len
|
||||
det_model.preprocessor.max_side_len = 960
|
||||
# 其他...
|
||||
```
|
||||
|
||||
### 6.4 其他指南
|
||||
- [FastDeploy部署PaddleOCR模型概览](../../)
|
||||
- [PP-OCRv3 C++部署](../cpp)
|
||||
- [PP-OCRv3 C 部署](../c)
|
||||
- [PP-OCRv3 C# 部署](../csharp)
|
||||
|
||||
## 7. 常见问题
|
||||
- PaddleOCR能在FastDeploy支持的多种后端上推理,支持情况如下表所示, 如何切换后端, 详见文档[如何切换模型推理后端引擎](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/faq/how_to_change_backend.md)
|
||||
|
||||
|硬件类型|支持的后端|
|
||||
|:---:|:---:|
|
||||
|X86 CPU| Paddle Inference, ONNX Runtime, OpenVINO |
|
||||
|ARM CPU| Paddle Lite |
|
||||
|飞腾 CPU| ONNX Runtime |
|
||||
|NVIDIA GPU| Paddle Inference, ONNX Runtime, TensorRT |
|
||||
|
||||
- [如何将模型预测结果转为numpy格式](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/faq/vision_result_related_problems.md)
|
||||
- [Intel GPU(独立显卡/集成显卡)的使用](https://github.com/PaddlePaddle/FastDeploy/blob/develop/tutorials/intel_gpu/README.md)
|
||||
- [编译CPU部署库](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/cpu.md)
|
||||
- [编译GPU部署库](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/gpu.md)
|
||||
- [编译Jetson部署库](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/jetson.md)
|
||||
@@ -0,0 +1,218 @@
|
||||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import fastdeploy as fd
|
||||
import cv2
|
||||
import os
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
import argparse
|
||||
import ast
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--det_model", required=True, help="Path of Detection model of PPOCR.")
|
||||
parser.add_argument(
|
||||
"--cls_model",
|
||||
required=True,
|
||||
help="Path of Classification model of PPOCR.")
|
||||
parser.add_argument(
|
||||
"--rec_model",
|
||||
required=True,
|
||||
help="Path of Recognization model of PPOCR.")
|
||||
parser.add_argument(
|
||||
"--rec_label_file",
|
||||
required=True,
|
||||
help="Path of Recognization model of PPOCR.")
|
||||
parser.add_argument(
|
||||
"--image", type=str, required=True, help="Path of test image file.")
|
||||
parser.add_argument(
|
||||
"--device",
|
||||
type=str,
|
||||
default='cpu',
|
||||
help="Type of inference device, support 'cpu' or 'gpu'.")
|
||||
parser.add_argument(
|
||||
"--device_id",
|
||||
type=int,
|
||||
default=0,
|
||||
help="Define which GPU card used to run model.")
|
||||
parser.add_argument(
|
||||
"--cls_bs",
|
||||
type=int,
|
||||
default=1,
|
||||
help="Classification model inference batch size.")
|
||||
parser.add_argument(
|
||||
"--rec_bs",
|
||||
type=int,
|
||||
default=6,
|
||||
help="Recognition model inference batch size")
|
||||
parser.add_argument(
|
||||
"--backend",
|
||||
type=str,
|
||||
default="default",
|
||||
help="Type of inference backend, support ort/trt/paddle/openvino, default 'openvino' for cpu, 'tensorrt' for gpu"
|
||||
)
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def build_option(args):
|
||||
|
||||
det_option = fd.RuntimeOption()
|
||||
cls_option = fd.RuntimeOption()
|
||||
rec_option = fd.RuntimeOption()
|
||||
|
||||
if args.device.lower() == "gpu":
|
||||
det_option.use_gpu(args.device_id)
|
||||
cls_option.use_gpu(args.device_id)
|
||||
rec_option.use_gpu(args.device_id)
|
||||
|
||||
if args.backend.lower() == "trt":
|
||||
assert args.device.lower(
|
||||
) == "gpu", "TensorRT backend require inference on device GPU."
|
||||
det_option.use_trt_backend()
|
||||
cls_option.use_trt_backend()
|
||||
rec_option.use_trt_backend()
|
||||
|
||||
# If use TRT backend, the dynamic shape will be set as follow.
|
||||
# We recommend that users set the length and height of the detection model to a multiple of 32.
|
||||
# We also recommend that users set the Trt input shape as follow.
|
||||
det_option.set_trt_input_shape("x", [1, 3, 64, 64], [1, 3, 640, 640],
|
||||
[1, 3, 960, 960])
|
||||
cls_option.set_trt_input_shape("x", [1, 3, 48, 10],
|
||||
[args.cls_bs, 3, 48, 320],
|
||||
[args.cls_bs, 3, 48, 1024])
|
||||
rec_option.set_trt_input_shape("x", [1, 3, 48, 10],
|
||||
[args.rec_bs, 3, 48, 320],
|
||||
[args.rec_bs, 3, 48, 2304])
|
||||
|
||||
# Users could save TRT cache file to disk as follow.
|
||||
det_option.set_trt_cache_file(args.det_model + "/det_trt_cache.trt")
|
||||
cls_option.set_trt_cache_file(args.cls_model + "/cls_trt_cache.trt")
|
||||
rec_option.set_trt_cache_file(args.rec_model + "/rec_trt_cache.trt")
|
||||
|
||||
elif args.backend.lower() == "pptrt":
|
||||
assert args.device.lower(
|
||||
) == "gpu", "Paddle-TensorRT backend require inference on device GPU."
|
||||
det_option.use_paddle_infer_backend()
|
||||
det_option.paddle_infer_option.collect_trt_shape = True
|
||||
det_option.paddle_infer_option.enable_trt = True
|
||||
|
||||
cls_option.use_paddle_infer_backend()
|
||||
cls_option.paddle_infer_option.collect_trt_shape = True
|
||||
cls_option.paddle_infer_option.enable_trt = True
|
||||
|
||||
rec_option.use_paddle_infer_backend()
|
||||
rec_option.paddle_infer_option.collect_trt_shape = True
|
||||
rec_option.paddle_infer_option.enable_trt = True
|
||||
|
||||
# If use TRT backend, the dynamic shape will be set as follow.
|
||||
# We recommend that users set the length and height of the detection model to a multiple of 32.
|
||||
# We also recommend that users set the Trt input shape as follow.
|
||||
det_option.set_trt_input_shape("x", [1, 3, 64, 64], [1, 3, 640, 640],
|
||||
[1, 3, 960, 960])
|
||||
cls_option.set_trt_input_shape("x", [1, 3, 48, 10],
|
||||
[args.cls_bs, 3, 48, 320],
|
||||
[args.cls_bs, 3, 48, 1024])
|
||||
rec_option.set_trt_input_shape("x", [1, 3, 48, 10],
|
||||
[args.rec_bs, 3, 48, 320],
|
||||
[args.rec_bs, 3, 48, 2304])
|
||||
|
||||
# Users could save TRT cache file to disk as follow.
|
||||
det_option.set_trt_cache_file(args.det_model)
|
||||
cls_option.set_trt_cache_file(args.cls_model)
|
||||
rec_option.set_trt_cache_file(args.rec_model)
|
||||
|
||||
elif args.backend.lower() == "ort":
|
||||
det_option.use_ort_backend()
|
||||
cls_option.use_ort_backend()
|
||||
rec_option.use_ort_backend()
|
||||
|
||||
elif args.backend.lower() == "paddle":
|
||||
det_option.use_paddle_infer_backend()
|
||||
cls_option.use_paddle_infer_backend()
|
||||
rec_option.use_paddle_infer_backend()
|
||||
|
||||
elif args.backend.lower() == "openvino":
|
||||
assert args.device.lower(
|
||||
) == "cpu", "OpenVINO backend require inference on device CPU."
|
||||
det_option.use_openvino_backend()
|
||||
cls_option.use_openvino_backend()
|
||||
rec_option.use_openvino_backend()
|
||||
|
||||
elif args.backend.lower() == "pplite":
|
||||
assert args.device.lower(
|
||||
) == "cpu", "Paddle Lite backend require inference on device CPU."
|
||||
det_option.use_lite_backend()
|
||||
cls_option.use_lite_backend()
|
||||
rec_option.use_lite_backend()
|
||||
|
||||
return det_option, cls_option, rec_option
|
||||
|
||||
|
||||
args = parse_arguments()
|
||||
|
||||
det_model_file = os.path.join(args.det_model, "inference.pdmodel")
|
||||
det_params_file = os.path.join(args.det_model, "inference.pdiparams")
|
||||
|
||||
cls_model_file = os.path.join(args.cls_model, "inference.pdmodel")
|
||||
cls_params_file = os.path.join(args.cls_model, "inference.pdiparams")
|
||||
|
||||
rec_model_file = os.path.join(args.rec_model, "inference.pdmodel")
|
||||
rec_params_file = os.path.join(args.rec_model, "inference.pdiparams")
|
||||
rec_label_file = args.rec_label_file
|
||||
|
||||
det_option, cls_option, rec_option = build_option(args)
|
||||
|
||||
det_model = fd.vision.ocr.DBDetector(
|
||||
det_model_file, det_params_file, runtime_option=det_option)
|
||||
|
||||
cls_model = fd.vision.ocr.Classifier(
|
||||
cls_model_file, cls_params_file, runtime_option=cls_option)
|
||||
|
||||
rec_model = fd.vision.ocr.Recognizer(
|
||||
rec_model_file, rec_params_file, rec_label_file, runtime_option=rec_option)
|
||||
|
||||
# Parameters settings for pre and post processing of Det/Cls/Rec Models.
|
||||
# All parameters are set to default values.
|
||||
det_model.preprocessor.max_side_len = 960
|
||||
det_model.postprocessor.det_db_thresh = 0.3
|
||||
det_model.postprocessor.det_db_box_thresh = 0.6
|
||||
det_model.postprocessor.det_db_unclip_ratio = 1.5
|
||||
det_model.postprocessor.det_db_score_mode = "slow"
|
||||
det_model.postprocessor.use_dilation = False
|
||||
cls_model.postprocessor.cls_thresh = 0.9
|
||||
|
||||
# Create PP-OCRv3, if cls_model is not needed, just set cls_model=None .
|
||||
ppocr_v3 = fd.vision.ocr.PPOCRv3(
|
||||
det_model=det_model, cls_model=cls_model, rec_model=rec_model)
|
||||
|
||||
# Set inference batch size for cls model and rec model, the value could be -1 and 1 to positive infinity.
|
||||
# When inference batch size is set to -1, it means that the inference batch size
|
||||
# of the cls and rec models will be the same as the number of boxes detected by the det model.
|
||||
ppocr_v3.cls_batch_size = args.cls_bs
|
||||
ppocr_v3.rec_batch_size = args.rec_bs
|
||||
|
||||
# Read the input image
|
||||
im = cv2.imread(args.image)
|
||||
|
||||
# Predict and reutrn the results
|
||||
result = ppocr_v3.predict(im)
|
||||
|
||||
print(result)
|
||||
|
||||
# Visuliaze the results.
|
||||
vis_im = fd.vision.vis_ppocr(im, result)
|
||||
cv2.imwrite("visualized_result.jpg", vis_im)
|
||||
print("Visualized result save in ./visualized_result.jpg")
|
||||
@@ -0,0 +1,77 @@
|
||||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import fastdeploy as fd
|
||||
import cv2
|
||||
import os
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
import argparse
|
||||
import ast
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--cls_model",
|
||||
required=True,
|
||||
help="Path of Classification model of PPOCR.")
|
||||
parser.add_argument(
|
||||
"--image", type=str, required=True, help="Path of test image file.")
|
||||
parser.add_argument(
|
||||
"--device",
|
||||
type=str,
|
||||
default='cpu',
|
||||
help="Type of inference device, support 'cpu', 'kunlunxin' or 'gpu'.")
|
||||
parser.add_argument(
|
||||
"--device_id",
|
||||
type=int,
|
||||
default=0,
|
||||
help="Define which GPU card used to run model.")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def build_option(args):
|
||||
|
||||
cls_option = fd.RuntimeOption()
|
||||
|
||||
if args.device.lower() == "gpu":
|
||||
cls_option.use_gpu(args.device_id)
|
||||
|
||||
return cls_option
|
||||
|
||||
|
||||
args = parse_arguments()
|
||||
|
||||
cls_model_file = os.path.join(args.cls_model, "inference.pdmodel")
|
||||
cls_params_file = os.path.join(args.cls_model, "inference.pdiparams")
|
||||
|
||||
# Set the runtime option
|
||||
cls_option = build_option(args)
|
||||
|
||||
# Create the cls_model
|
||||
cls_model = fd.vision.ocr.Classifier(
|
||||
cls_model_file, cls_params_file, runtime_option=cls_option)
|
||||
|
||||
# Set the postprocessing parameters
|
||||
cls_model.postprocessor.cls_thresh = 0.9
|
||||
|
||||
# Read the image
|
||||
im = cv2.imread(args.image)
|
||||
|
||||
# Predict and return the results
|
||||
result = cls_model.predict(im)
|
||||
|
||||
# User can infer a batch of images by following code.
|
||||
# result = cls_model.batch_predict([im])
|
||||
|
||||
print(result)
|
||||
@@ -0,0 +1,82 @@
|
||||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import fastdeploy as fd
|
||||
import cv2
|
||||
import os
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
import argparse
|
||||
import ast
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--det_model", required=True, help="Path of Detection model of PPOCR.")
|
||||
parser.add_argument(
|
||||
"--image", type=str, required=True, help="Path of test image file.")
|
||||
parser.add_argument(
|
||||
"--device",
|
||||
type=str,
|
||||
default='cpu',
|
||||
help="Type of inference device, support 'cpu', 'kunlunxin' or 'gpu'.")
|
||||
parser.add_argument(
|
||||
"--device_id",
|
||||
type=int,
|
||||
default=0,
|
||||
help="Define which GPU card used to run model.")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def build_option(args):
|
||||
|
||||
det_option = fd.RuntimeOption()
|
||||
|
||||
if args.device.lower() == "gpu":
|
||||
det_option.use_gpu(args.device_id)
|
||||
|
||||
return det_option
|
||||
|
||||
|
||||
args = parse_arguments()
|
||||
|
||||
det_model_file = os.path.join(args.det_model, "inference.pdmodel")
|
||||
det_params_file = os.path.join(args.det_model, "inference.pdiparams")
|
||||
|
||||
# Set the runtime option
|
||||
det_option = build_option(args)
|
||||
|
||||
# Create the det_model
|
||||
det_model = fd.vision.ocr.DBDetector(
|
||||
det_model_file, det_params_file, runtime_option=det_option)
|
||||
|
||||
# Set the preporcessing parameters
|
||||
det_model.preprocessor.max_side_len = 960
|
||||
det_model.postprocessor.det_db_thresh = 0.3
|
||||
det_model.postprocessor.det_db_box_thresh = 0.6
|
||||
det_model.postprocessor.det_db_unclip_ratio = 1.5
|
||||
det_model.postprocessor.det_db_score_mode = "slow"
|
||||
det_model.postprocessor.use_dilation = False
|
||||
|
||||
# Read the image
|
||||
im = cv2.imread(args.image)
|
||||
|
||||
# Predict and return the results
|
||||
result = det_model.predict(im)
|
||||
|
||||
print(result)
|
||||
|
||||
# Visualize the results
|
||||
vis_im = fd.vision.vis_ppocr(im, result)
|
||||
cv2.imwrite("visualized_result.jpg", vis_im)
|
||||
print("Visualized result save in ./visualized_result.jpg")
|
||||
@@ -0,0 +1,79 @@
|
||||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import fastdeploy as fd
|
||||
import cv2
|
||||
import os
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
import argparse
|
||||
import ast
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--rec_model",
|
||||
required=True,
|
||||
help="Path of Recognization model of PPOCR.")
|
||||
parser.add_argument(
|
||||
"--rec_label_file",
|
||||
required=True,
|
||||
help="Path of Recognization model of PPOCR.")
|
||||
parser.add_argument(
|
||||
"--image", type=str, required=True, help="Path of test image file.")
|
||||
parser.add_argument(
|
||||
"--device",
|
||||
type=str,
|
||||
default='cpu',
|
||||
help="Type of inference device, support 'cpu', 'kunlunxin' or 'gpu'.")
|
||||
parser.add_argument(
|
||||
"--device_id",
|
||||
type=int,
|
||||
default=0,
|
||||
help="Define which GPU card used to run model.")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def build_option(args):
|
||||
|
||||
rec_option = fd.RuntimeOption()
|
||||
|
||||
if args.device.lower() == "gpu":
|
||||
rec_option.use_gpu(args.device_id)
|
||||
|
||||
return rec_option
|
||||
|
||||
|
||||
args = parse_arguments()
|
||||
|
||||
rec_model_file = os.path.join(args.rec_model, "inference.pdmodel")
|
||||
rec_params_file = os.path.join(args.rec_model, "inference.pdiparams")
|
||||
rec_label_file = args.rec_label_file
|
||||
|
||||
# Set the runtime option
|
||||
rec_option = build_option(args)
|
||||
|
||||
# Create the rec_model
|
||||
rec_model = fd.vision.ocr.Recognizer(
|
||||
rec_model_file, rec_params_file, rec_label_file, runtime_option=rec_option)
|
||||
|
||||
# Read the image
|
||||
im = cv2.imread(args.image)
|
||||
|
||||
# Predict and return the result
|
||||
result = rec_model.predict(im)
|
||||
|
||||
# User can infer a batch of images by following code.
|
||||
# result = rec_model.batch_predict([im])
|
||||
|
||||
print(result)
|
||||
Reference in New Issue
Block a user