mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-06 00:57:33 +08:00
[CVCUDA] Update CV-CUDA to v0.2.1, add vision processor C++ tutorial (#1678)
* update cvcuda 0.2.0 -> 0.2.1 * add cpp tutorials demo * fix reviewed problem
This commit is contained in:
11
tutorials/vision_processor/cpp/CMakeLists.txt
Normal file
11
tutorials/vision_processor/cpp/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
PROJECT(preprocessor_demo C CXX)
|
||||
CMAKE_MINIMUM_REQUIRED (VERSION 3.10)
|
||||
|
||||
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
|
||||
|
||||
include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)
|
||||
|
||||
include_directories(${FASTDEPLOY_INCS})
|
||||
|
||||
add_executable(preprocessor_demo ${PROJECT_SOURCE_DIR}/main.cc)
|
||||
target_link_libraries(preprocessor_demo ${FASTDEPLOY_LIBS})
|
27
tutorials/vision_processor/cpp/README.md
Normal file
27
tutorials/vision_processor/cpp/README.md
Normal file
@@ -0,0 +1,27 @@
|
||||
English | [中文](README_CN.md)
|
||||
|
||||
# Preprocessor Python Demo
|
||||
|
||||
1. Compile FastDeploy and open CV-CUDA option
|
||||
> [Compile FastDeploy](../../../docs/cn/build_and_install/gpu.md)
|
||||
> [Open CV-CUDA option](../../../docs/cn/faq/use_cv_cuda.md)
|
||||
|
||||
2. Run the demo
|
||||
```bash
|
||||
# Download the test image
|
||||
wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg
|
||||
|
||||
# Compile the Demo
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/../../../../build/compiled_fastdeploy_sdk/ # if build sdk in `FastDeploy/build/compiled_fastdeploy_sdk`
|
||||
make -j
|
||||
|
||||
# Run the demo
|
||||
|
||||
# Use OpenCV
|
||||
./preprocessor_demo ILSVRC2012_val_00000010.jpeg 0
|
||||
|
||||
# Use CV-CUDA
|
||||
./preprocessor_demo ILSVRC2012_val_00000010.jpeg 1
|
||||
```
|
27
tutorials/vision_processor/cpp/README_CN.md
Normal file
27
tutorials/vision_processor/cpp/README_CN.md
Normal file
@@ -0,0 +1,27 @@
|
||||
中文 | [English](README.md)
|
||||
|
||||
# Preprocessor Python 示例代码
|
||||
|
||||
1. 编译FastDeploy并开启CV-CUDA选项
|
||||
> [编译FastDeploy](../../../docs/cn/build_and_install/gpu.md)
|
||||
> [开启CV-CUDA选项](../../../docs/cn/faq/use_cv_cuda.md)
|
||||
|
||||
2. 运行示例代码
|
||||
```bash
|
||||
# 下载测试图片
|
||||
wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg
|
||||
|
||||
# 编译示例代码
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/../../../../build/compiled_fastdeploy_sdk/ # 若编译FastDeploy在其他文件夹,请替换为相应的sdk路径
|
||||
make -j
|
||||
|
||||
# 运行示例代码
|
||||
|
||||
# 使用OpenCV处理图片
|
||||
./preprocessor_demo ILSVRC2012_val_00000010.jpeg 0
|
||||
|
||||
# 使用CV-CUDA处理图片
|
||||
./preprocessor_demo ILSVRC2012_val_00000010.jpeg 1
|
||||
```
|
78
tutorials/vision_processor/cpp/main.cc
Normal file
78
tutorials/vision_processor/cpp/main.cc
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "fastdeploy/vision.h"
|
||||
#include "fastdeploy/vision/common/processors/manager.h"
|
||||
#include "fastdeploy/vision/common/processors/transform.h"
|
||||
|
||||
namespace fd = fastdeploy;
|
||||
|
||||
// Define our custom processor
|
||||
class CustomPreprocessor : public fd::vision::ProcessorManager {
|
||||
public:
|
||||
explicit CustomPreprocessor(){};
|
||||
~CustomPreprocessor(){};
|
||||
|
||||
virtual bool Apply(fd::vision::FDMatBatch* image_batch,
|
||||
std::vector<fd::FDTensor>* outputs);
|
||||
|
||||
private:
|
||||
// Create op
|
||||
int width = 160;
|
||||
int height = 160;
|
||||
std::shared_ptr<fd::vision::Resize> resize_op =
|
||||
std::make_shared<fd::vision::Resize>(width, height, -1.0, -1.0, 1, false);
|
||||
std::shared_ptr<fd::vision::CenterCrop> crop =
|
||||
std::make_shared<fd::vision::CenterCrop>(50, 50);
|
||||
std::vector<float> mean = {0.485f, 0.456f, 0.406f};
|
||||
std::vector<float> std = {0.229f, 0.224f, 0.225f};
|
||||
std::shared_ptr<fd::vision::Normalize> normalize =
|
||||
std::make_shared<fd::vision::Normalize>(mean, std);
|
||||
};
|
||||
|
||||
// Implement our custom processor's Apply() method
|
||||
bool CustomPreprocessor::Apply(fd::vision::FDMatBatch* image_batch,
|
||||
std::vector<fd::FDTensor>* outputs) {
|
||||
// Use op to transform the images
|
||||
bool resize_ret = (*resize_op)(&(image_batch->mats->at(0)));
|
||||
bool crop_ret = (*crop)(image_batch);
|
||||
bool normalize_ret = (*normalize)(image_batch);
|
||||
|
||||
outputs->resize(1);
|
||||
fd::FDTensor* tensor = image_batch->Tensor();
|
||||
(*outputs)[0].SetExternalData(tensor->Shape(), tensor->Dtype(),
|
||||
tensor->Data(), tensor->device,
|
||||
tensor->device_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cout << "Usage: ./preprocessor_demo path/to/image run_option, "
|
||||
"e.g ././preprocessor_demo ./test.jpeg 0"
|
||||
<< std::endl;
|
||||
std::cout << "Run_option 0: OpenCV; 1: CV-CUDA " << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Prepare input images
|
||||
auto im = cv::imread(argv[1]);
|
||||
std::vector<cv::Mat> images = {im, im};
|
||||
std::vector<fd::vision::FDMat> mats = fd::vision::WrapMat(images);
|
||||
std::vector<fd::FDTensor> outputs;
|
||||
|
||||
// CustomPreprocessor processor;
|
||||
CustomPreprocessor processor = CustomPreprocessor();
|
||||
|
||||
// Use CV-CUDA if parameter passed and detected
|
||||
if (std::atoi(argv[2]) == 1) {
|
||||
processor.UseCuda(true, 0);
|
||||
}
|
||||
|
||||
// Run the processor
|
||||
bool ret = processor.Run(&mats, &outputs);
|
||||
|
||||
// Print output
|
||||
for (int i = 0; i < outputs.size(); i++) {
|
||||
outputs[i].PrintInfo("out");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user