init
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user