[nvJPEG] Integrate nvJPEG decoder (#1288)

* nvjpeg cmake

* add common decoder, nvjpeg decoder and add image name predict api

* ppclas support nvjpeg decoder

* remove useless comments

* image decoder support opencv

* nvjpeg decode fallback to opencv

* fdtensor add nbytes_allocated

* single image decode api

* fix bug

* add pybind

* ignore nvjpeg on jetson

* fix cmake in

* predict on fdmat

* remove image names predict api, add image decoder tutorial

* Update __init__.py

* fix pybind
This commit is contained in:
Wang Xinyu
2023-02-17 10:27:05 +08:00
committed by GitHub
parent e3a7ab4c14
commit efa46563f3
25 changed files with 875 additions and 44 deletions

View File

@@ -0,0 +1,16 @@
English | [中文](README_CN.md)
# Image Decoder
Currently, we support below image decoder libs
- OpenCV
- nvJPEG (Needs NVIDIA GPU, doesn't support Jetson)
## Example
- [C++ Example](cpp)
- Python API(WIP)
## nvJPEG vs. OpenCV performance benchmark
Refer to: https://github.com/PaddlePaddle/FastDeploy/pull/1288#issuecomment-1427749772

View File

@@ -0,0 +1,16 @@
简体中文 | [English](README.md)
# Image Decoder
图片解码库,目前支持以下图片解码库:
- OpenCV
- nvJPEG (依赖NVIDIA GPU不支持Jetson)
## 示例代码
- [C++示例](cpp)
- Python API仍在开发中...
## nvJPEG和OpenCV性能对比数据
参见https://github.com/PaddlePaddle/FastDeploy/pull/1288#issuecomment-1427749772

View File

@@ -0,0 +1,11 @@
PROJECT(image_decoder 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(image_decoder ${PROJECT_SOURCE_DIR}/main.cc)
target_link_libraries(image_decoder ${FASTDEPLOY_LIBS})

View File

@@ -0,0 +1,22 @@
English | [中文](README_CN.md)
# Image Decoder C++ Example
1. [Build FastDeploy](../docs/cn/build_and_install) or download [FastDeploy prebuilt library](../docs/cn/build_and_install/download_prebuilt_libraries.md)
2. Build example
```bash
mkdir build
cd build
# [PATH-TO-FASTDEPLOY] is the install directory of FastDeploy
cmake .. -DFASTDEPLOY_INSTALL_DIR=[PATH-TO-FASTDEPLOY]
make -j
# Download the test image
wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg
# OpenCV decoder
./image_decoder ILSVRC2012_val_00000010.jpeg 0
# nvJPEG
./image_decoder ILSVRC2012_val_00000010.jpeg 1

View File

@@ -0,0 +1,22 @@
简体中文 | [English](README.md)
# Image Decoder C++示例
1. [编译FastDeploy](../docs/cn/build_and_install), 或直接下载[FastDeploy预编译库](../docs/cn/build_and_install/download_prebuilt_libraries.md)
2. 编译示例
```bash
mkdir build
cd build
# [PATH-TO-FASTDEPLOY]需替换为FastDeploy的安装路径
cmake .. -DFASTDEPLOY_INSTALL_DIR=[PATH-TO-FASTDEPLOY]
make -j
# 下载测试图片
wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg
# OpenCV解码
./image_decoder ILSVRC2012_val_00000010.jpeg 0
# nvJPEG
./image_decoder ILSVRC2012_val_00000010.jpeg 1

View File

@@ -0,0 +1,57 @@
#include "fastdeploy/vision/common/image_decoder/image_decoder.h"
namespace fdvis = fastdeploy::vision;
namespace fd = fastdeploy;
void OpenCVImageDecode(const std::string& img_name) {
fdvis::FDMat mat;
auto img_decoder = new fdvis::ImageDecoder();
img_decoder->Decode(img_name, &mat);
mat.PrintInfo("");
delete img_decoder;
}
void NvJpegImageDecode(const std::string& img_name) {
std::vector<fdvis::FDMat> mats(1);
std::vector<fastdeploy::FDTensor> caches(1);
cudaStream_t stream;
cudaStreamCreate(&stream);
// For nvJPEG decoder, we need set stream and output cache for the FDMat
for (size_t i = 0; i < mats.size(); i++) {
mats[i].output_cache = &caches[i];
mats[i].SetStream(stream);
}
auto img_decoder = new fdvis::ImageDecoder(fdvis::ImageDecoderLib::NVJPEG);
// This is batch decode API, for single image decode API,
// please refer to OpenCVImageDecode()
img_decoder->BatchDecode({img_name}, &mats);
for (size_t i = 0; i < mats.size(); i++) {
std::cout << "Mat type: " << mats[i].mat_type << ", "
<< "DataType=" << mats[i].Type() << ", "
<< "Channel=" << mats[i].Channels() << ", "
<< "Height=" << mats[i].Height() << ", "
<< "Width=" << mats[i].Width() << std::endl;
}
cudaStreamDestroy(stream);
}
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cout << "Usage: image_decoder path/to/image run_option, "
"e.g ./image_decoder ./test.jpeg 0"
<< std::endl;
std::cout << "Run_option 0: OpenCV; 1: nvJPEG " << std::endl;
return -1;
}
if (std::atoi(argv[2]) == 0) {
OpenCVImageDecode(argv[1]);
} else if (std::atoi(argv[2]) == 1) {
NvJpegImageDecode(argv[1]);
}
return 0;
}