Skip to content

Commit

Permalink
Add PP-ModNet and PP-HumanMatting Support (PaddlePaddle#240)
Browse files Browse the repository at this point in the history
* first commit for yolov7

* pybind for yolov7

* CPP README.md

* CPP README.md

* modified yolov7.cc

* README.md

* python file modify

* delete license in fastdeploy/

* repush the conflict part

* README.md modified

* README.md modified

* file path modified

* file path modified

* file path modified

* file path modified

* file path modified

* README modified

* README modified

* move some helpers to private

* add examples for yolov7

* api.md modified

* api.md modified

* api.md modified

* YOLOv7

* yolov7 release link

* yolov7 release link

* yolov7 release link

* copyright

* change some helpers to private

* change variables to const and fix documents.

* gitignore

* Transfer some funtions to private member of class

* Transfer some funtions to private member of class

* Merge from develop (#9)

* Fix compile problem in different python version (#26)

* fix some usage problem in linux

* Fix compile problem

Co-authored-by: root <[email protected]>

* Add PaddleDetetion/PPYOLOE model support (#22)

* add ppdet/ppyoloe

* Add demo code and documents

* add convert processor to vision (#27)

* update .gitignore

* Added checking for cmake include dir

* fixed missing trt_backend option bug when init from trt

* remove un-need data layout and add pre-check for dtype

* changed RGB2BRG to BGR2RGB in ppcls model

* add model_zoo yolov6 c++/python demo

* fixed CMakeLists.txt typos

* update yolov6 cpp/README.md

* add yolox c++/pybind and model_zoo demo

* move some helpers to private

* fixed CMakeLists.txt typos

* add normalize with alpha and beta

* add version notes for yolov5/yolov6/yolox

* add copyright to yolov5.cc

* revert normalize

* fixed some bugs in yolox

* fixed examples/CMakeLists.txt to avoid conflicts

* add convert processor to vision

* format examples/CMakeLists summary

* Fix bug while the inference result is empty with YOLOv5 (#29)

* Add multi-label function for yolov5

* Update README.md

Update doc

* Update fastdeploy_runtime.cc

fix variable option.trt_max_shape wrong name

* Update runtime_option.md

Update resnet model dynamic shape setting name from images to x

* Fix bug when inference result boxes are empty

* Delete detection.py

Co-authored-by: Jason <[email protected]>
Co-authored-by: root <[email protected]>
Co-authored-by: DefTruth <[email protected]>
Co-authored-by: huangjianhui <[email protected]>

* first commit for yolor

* for merge

* Develop (#11)

* Fix compile problem in different python version (#26)

* fix some usage problem in linux

* Fix compile problem

Co-authored-by: root <[email protected]>

* Add PaddleDetetion/PPYOLOE model support (#22)

* add ppdet/ppyoloe

* Add demo code and documents

* add convert processor to vision (#27)

* update .gitignore

* Added checking for cmake include dir

* fixed missing trt_backend option bug when init from trt

* remove un-need data layout and add pre-check for dtype

* changed RGB2BRG to BGR2RGB in ppcls model

* add model_zoo yolov6 c++/python demo

* fixed CMakeLists.txt typos

* update yolov6 cpp/README.md

* add yolox c++/pybind and model_zoo demo

* move some helpers to private

* fixed CMakeLists.txt typos

* add normalize with alpha and beta

* add version notes for yolov5/yolov6/yolox

* add copyright to yolov5.cc

* revert normalize

* fixed some bugs in yolox

* fixed examples/CMakeLists.txt to avoid conflicts

* add convert processor to vision

* format examples/CMakeLists summary

* Fix bug while the inference result is empty with YOLOv5 (#29)

* Add multi-label function for yolov5

* Update README.md

Update doc

* Update fastdeploy_runtime.cc

fix variable option.trt_max_shape wrong name

* Update runtime_option.md

Update resnet model dynamic shape setting name from images to x

* Fix bug when inference result boxes are empty

* Delete detection.py

Co-authored-by: Jason <[email protected]>
Co-authored-by: root <[email protected]>
Co-authored-by: DefTruth <[email protected]>
Co-authored-by: huangjianhui <[email protected]>

* Yolor (#16)

* Develop (#11) (#12)

* Fix compile problem in different python version (#26)

* fix some usage problem in linux

* Fix compile problem

Co-authored-by: root <[email protected]>

* Add PaddleDetetion/PPYOLOE model support (#22)

* add ppdet/ppyoloe

* Add demo code and documents

* add convert processor to vision (#27)

* update .gitignore

* Added checking for cmake include dir

* fixed missing trt_backend option bug when init from trt

* remove un-need data layout and add pre-check for dtype

* changed RGB2BRG to BGR2RGB in ppcls model

* add model_zoo yolov6 c++/python demo

* fixed CMakeLists.txt typos

* update yolov6 cpp/README.md

* add yolox c++/pybind and model_zoo demo

* move some helpers to private

* fixed CMakeLists.txt typos

* add normalize with alpha and beta

* add version notes for yolov5/yolov6/yolox

* add copyright to yolov5.cc

* revert normalize

* fixed some bugs in yolox

* fixed examples/CMakeLists.txt to avoid conflicts

* add convert processor to vision

* format examples/CMakeLists summary

* Fix bug while the inference result is empty with YOLOv5 (#29)

* Add multi-label function for yolov5

* Update README.md

Update doc

* Update fastdeploy_runtime.cc

fix variable option.trt_max_shape wrong name

* Update runtime_option.md

Update resnet model dynamic shape setting name from images to x

* Fix bug when inference result boxes are empty

* Delete detection.py

Co-authored-by: Jason <[email protected]>
Co-authored-by: root <[email protected]>
Co-authored-by: DefTruth <[email protected]>
Co-authored-by: huangjianhui <[email protected]>

Co-authored-by: Jason <[email protected]>
Co-authored-by: root <[email protected]>
Co-authored-by: DefTruth <[email protected]>
Co-authored-by: huangjianhui <[email protected]>

* Develop (#13)

* Fix compile problem in different python version (#26)

* fix some usage problem in linux

* Fix compile problem

Co-authored-by: root <[email protected]>

* Add PaddleDetetion/PPYOLOE model support (#22)

* add ppdet/ppyoloe

* Add demo code and documents

* add convert processor to vision (#27)

* update .gitignore

* Added checking for cmake include dir

* fixed missing trt_backend option bug when init from trt

* remove un-need data layout and add pre-check for dtype

* changed RGB2BRG to BGR2RGB in ppcls model

* add model_zoo yolov6 c++/python demo

* fixed CMakeLists.txt typos

* update yolov6 cpp/README.md

* add yolox c++/pybind and model_zoo demo

* move some helpers to private

* fixed CMakeLists.txt typos

* add normalize with alpha and beta

* add version notes for yolov5/yolov6/yolox

* add copyright to yolov5.cc

* revert normalize

* fixed some bugs in yolox

* fixed examples/CMakeLists.txt to avoid conflicts

* add convert processor to vision

* format examples/CMakeLists summary

* Fix bug while the inference result is empty with YOLOv5 (#29)

* Add multi-label function for yolov5

* Update README.md

Update doc

* Update fastdeploy_runtime.cc

fix variable option.trt_max_shape wrong name

* Update runtime_option.md

Update resnet model dynamic shape setting name from images to x

* Fix bug when inference result boxes are empty

* Delete detection.py

Co-authored-by: Jason <[email protected]>
Co-authored-by: root <[email protected]>
Co-authored-by: DefTruth <[email protected]>
Co-authored-by: huangjianhui <[email protected]>

* documents

* documents

* documents

* documents

* documents

* documents

* documents

* documents

* documents

* documents

* documents

* documents

* Develop (#14)

* Fix compile problem in different python version (#26)

* fix some usage problem in linux

* Fix compile problem

Co-authored-by: root <[email protected]>

* Add PaddleDetetion/PPYOLOE model support (#22)

* add ppdet/ppyoloe

* Add demo code and documents

* add convert processor to vision (#27)

* update .gitignore

* Added checking for cmake include dir

* fixed missing trt_backend option bug when init from trt

* remove un-need data layout and add pre-check for dtype

* changed RGB2BRG to BGR2RGB in ppcls model

* add model_zoo yolov6 c++/python demo

* fixed CMakeLists.txt typos

* update yolov6 cpp/README.md

* add yolox c++/pybind and model_zoo demo

* move some helpers to private

* fixed CMakeLists.txt typos

* add normalize with alpha and beta

* add version notes for yolov5/yolov6/yolox

* add copyright to yolov5.cc

* revert normalize

* fixed some bugs in yolox

* fixed examples/CMakeLists.txt to avoid conflicts

* add convert processor to vision

* format examples/CMakeLists summary

* Fix bug while the inference result is empty with YOLOv5 (#29)

* Add multi-label function for yolov5

* Update README.md

Update doc

* Update fastdeploy_runtime.cc

fix variable option.trt_max_shape wrong name

* Update runtime_option.md

Update resnet model dynamic shape setting name from images to x

* Fix bug when inference result boxes are empty

* Delete detection.py

Co-authored-by: root <[email protected]>
Co-authored-by: DefTruth <[email protected]>
Co-authored-by: huangjianhui <[email protected]>

Co-authored-by: Jason <[email protected]>
Co-authored-by: root <[email protected]>
Co-authored-by: DefTruth <[email protected]>
Co-authored-by: huangjianhui <[email protected]>
Co-authored-by: Jason <[email protected]>

* add is_dynamic for YOLO series (#22)

* modify ppmatting backend and docs

* modify ppmatting docs

* fix the PPMatting size problem

* fix LimitShort's log

* retrigger ci

* modify PPMatting docs

* modify the way  for dealing with  LimitShort

* add pphumanmatting and modnet series

* docs of PPMatting series

* add explanation of newly added processors and fix processors

* Modify LimitShort function and ppmatting.cc

* modify ResizeByShort and ppmatting.cc

* change resize_to_int_mult to limit_by_stride and delete resize_by_input_shape

* retrigger ci

* retrigger ci

* fix problem produced by ResizeByShort

* Update eigen.cmake

* Delete eigen.cmake

* refine code

* add test file for ppmatting series

* add squeeze for fd_tensor and modify ppmatting.cc

Co-authored-by: Jason <[email protected]>
Co-authored-by: root <[email protected]>
Co-authored-by: DefTruth <[email protected]>
Co-authored-by: huangjianhui <[email protected]>
Co-authored-by: Jason <[email protected]>
  • Loading branch information
6 people authored Oct 7, 2022
1 parent 1005a09 commit 0692dcc
Show file tree
Hide file tree
Showing 21 changed files with 524 additions and 133 deletions.
2 changes: 2 additions & 0 deletions examples/vision/matting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ FastDeploy目前支持如下抠图模型部署
| :--- | :--- | :------- | :--- |
| [ZHKKKe/MODNet](./modnet) | MODNet 系列模型 | ONNX | [CommitID:28165a4](https://github.com/ZHKKKe/MODNet/commit/28165a4) |
| [PaddleSeg/PPMatting](./ppmatting) | PPMatting 系列模型 | Paddle | [Release/2.6](https://github.com/PaddlePaddle/PaddleSeg/tree/release/2.6/Matting) |
| [PaddleSeg/PPHumanMatting](./ppmatting) | PPHumanMatting 系列模型 | Paddle | [Release/2.6](https://github.com/PaddlePaddle/PaddleSeg/tree/release/2.6/Matting) |
| [PaddleSeg/ModNet](./ppmatting) | ModNet 系列模型 | Paddle | [Release/2.6](https://github.com/PaddlePaddle/PaddleSeg/tree/release/2.6/Matting) |
12 changes: 9 additions & 3 deletions examples/vision/matting/ppmatting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
目前FastDeploy支持如下模型的部署

- [PPMatting系列模型](https://github.com/PaddlePaddle/PaddleSeg/tree/release/2.6/Matting)
- [PPHumanMatting系列模型](https://github.com/PaddlePaddle/PaddleSeg/tree/release/2.6/Matting)
- [ModNet系列模型](https://github.com/PaddlePaddle/PaddleSeg/tree/release/2.6/Matting)


## 导出部署模型

在部署前,需要先将PPMatting导出成部署模型,导出步骤参考文档[导出模型](https://github.com/PaddlePaddle/PaddleSeg/tree/release/2.6/Matting)
在部署前,需要先将PPMatting导出成部署模型,导出步骤参考文档[导出模型](https://github.com/PaddlePaddle/PaddleSeg/tree/release/2.6/Matting)(Tips:导出PPMatting系列模型和PPHumanMatting系列模型需要设置导出脚本的`--input_shape`参数)


## 下载预训练模型
Expand All @@ -25,8 +27,12 @@

| 模型 | 参数大小 | 精度 | 备注 |
|:---------------------------------------------------------------- |:----- |:----- | :------ |
| [PPMatting-512](https://bj.bcebos.com/paddlehub/fastdeploy/PP-Matting-512.tgz) | 87MB | - |
| [PPMatting-1024](https://bj.bcebos.com/paddlehub/fastdeploy/PP-Matting-1024.tgz) | 87MB | - |
| [PPMatting-512](https://bj.bcebos.com/paddlehub/fastdeploy/PP-Matting-512.tgz) | 106MB | - |
| [PPMatting-1024](https://bj.bcebos.com/paddlehub/fastdeploy/PP-Matting-1024.tgz) | 106MB | - |
| [PPHumanMatting](https://bj.bcebos.com/paddlehub/fastdeploy/PPHumanMatting.tgz) | 247MB | - |
| [Modnet_ResNet50_vd](https://bj.bcebos.com/paddlehub/fastdeploy/PPModnet_ResNet50_vd.tgz) | 355MB | - |
| [Modnet_MobileNetV2](https://bj.bcebos.com/paddlehub/fastdeploy/PPModnet_MobileNetV2.tgz) | 28MB | - |
| [Modnet_HRNet_w18](https://bj.bcebos.com/paddlehub/fastdeploy/PPModnet_HRNet_w18.tgz) | 51MB | - |



Expand Down
2 changes: 1 addition & 1 deletion examples/vision/matting/ppmatting/cpp/infer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void GpuInfer(const std::string& model_dir, const std::string& image_file,
cv::imwrite("visualized_result.jpg", vis_im_with_bg);
cv::imwrite("visualized_result_fg.jpg", vis_im);
std::cout << "Visualized result save in ./visualized_result_replaced_bg.jpg "
"and ./visualized_result_fg.jpgg"
"and ./visualized_result_fg.jpg"
<< std::endl;
}

Expand Down
7 changes: 7 additions & 0 deletions fastdeploy/core/fd_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ void FDTensor::ExpandDim(int64_t axis) {
shape.insert(shape.begin() + axis, 1);
}

void FDTensor::Squeeze(int64_t axis) {
size_t ndim = shape.size();
FDASSERT(axis >= 0 && axis < ndim,
"The allowed 'axis' must be in range of (0, %lu)!", ndim);
shape.erase(shape.begin() + axis);
}

void FDTensor::Allocate(const std::vector<int64_t>& new_shape,
const FDDataType& data_type,
const std::string& tensor_name,
Expand Down
4 changes: 4 additions & 0 deletions fastdeploy/core/fd_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ struct FASTDEPLOY_DECL FDTensor {
// at the `axis` position in the expanded Tensor shape.
void ExpandDim(int64_t axis = 0);

// Squeeze the shape of a Tensor. Erase the axis that will appear
// at the `axis` position in the squeezed Tensor shape.
void Squeeze(int64_t axis = 0);

// Initialize Tensor
// Include setting attribute for tensor
// and allocate cpu memory buffer
Expand Down
65 changes: 65 additions & 0 deletions fastdeploy/vision/common/processors/crop.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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/common/processors/crop.h"

namespace fastdeploy {
namespace vision {

bool Crop::CpuRun(Mat* mat) {
cv::Mat* im = mat->GetCpuMat();
int height = static_cast<int>(im->rows);
int width = static_cast<int>(im->cols);
if (height < height_ + offset_h_ || width < width_ + offset_w_) {
FDERROR << "[Crop] Cannot crop [" << height_ << ", " << width_
<< "] from the input image [" << height << ", " << width
<< "], with offset [" << offset_h_ << ", " << offset_w_ << "]."
<< std::endl;
return false;
}
cv::Rect crop_roi(offset_w_, offset_h_, width_, height_);
*im = (*im)(crop_roi);
mat->SetWidth(width_);
mat->SetHeight(height_);
return true;
}

#ifdef ENABLE_OPENCV_CUDA
bool Crop::GpuRun(Mat* mat) {
cv::cuda::GpuMat* im = mat->GetGpuMat();
int height = static_cast<int>(im->rows);
int width = static_cast<int>(im->cols);
if (height < height_ + offset_h_ || width < width_ + offset_w_) {
FDERROR << "[Crop] Cannot crop [" << height_ << ", " << width_
<< "] from the input image [" << height << ", " << width
<< "], with offset [" << offset_h_ << ", " << offset_w_ << "]."
<< std::endl;
return false;
}
cv::Rect crop_roi(offset_w_, offset_h_, width_, height_);
*im = (*im)(crop_roi);
mat->SetWidth(width_);
mat->SetHeight(height_);
return true;
}
#endif

bool Crop::Run(Mat* mat, int offset_w, int offset_h, int width, int height,
ProcLib lib) {
auto c = Crop(offset_w, offset_h, width, height);
return c(mat, lib);
}

} // namespace vision
} // namespace fastdeploy
47 changes: 47 additions & 0 deletions fastdeploy/vision/common/processors/crop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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.

#pragma once

#include "fastdeploy/vision/common/processors/base.h"

namespace fastdeploy {
namespace vision {

class Crop : public Processor {
public:
Crop(int offset_w, int offset_h, int width, int height) {
offset_w_ = offset_w;
offset_h_ = offset_h;
width_ = width;
height_ = height;
}
bool CpuRun(Mat* mat);
#ifdef ENABLE_OPENCV_CUDA
bool GpuRun(Mat* mat);
#endif
std::string Name() { return "Crop"; }

static bool Run(Mat* mat, int offset_w, int offset_h, int width, int height,
ProcLib lib = ProcLib::OPENCV_CPU);

private:
int offset_w_;
int offset_h_;
int height_;
int width_;
};

} // namespace vision
} // namespace fastdeploy
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "fastdeploy/vision/common/processors/resize_to_int_mult.h"
#include "fastdeploy/vision/common/processors/limit_by_stride.h"

namespace fastdeploy {
namespace vision {

bool ResizeToIntMult::CpuRun(Mat* mat) {
bool LimitByStride::CpuRun(Mat* mat) {
cv::Mat* im = mat->GetCpuMat();
int origin_w = im->cols;
int origin_h = im->rows;
int rw = origin_w - origin_w % mult_int_;
int rh = origin_h - origin_h % mult_int_;
int rw = origin_w - origin_w % stride_;
int rh = origin_h - origin_h % stride_;
if (rw != origin_w || rh != origin_w) {
cv::resize(*im, *im, cv::Size(rw, rh), 0, 0, interp_);
mat->SetWidth(im->cols);
Expand All @@ -32,13 +32,13 @@ bool ResizeToIntMult::CpuRun(Mat* mat) {
}

#ifdef ENABLE_OPENCV_CUDA
bool ResizeToIntMult::GpuRun(Mat* mat) {
bool LimitByStride::GpuRun(Mat* mat) {
cv::cuda::GpuMat* im = mat->GetGpuMat();
int origin_w = im->cols;
int origin_h = im->rows;
im->convertTo(*im, CV_32FC(im->channels()));
int rw = origin_w - origin_w % mult_int_;
int rh = origin_h - origin_h % mult_int_;
int rw = origin_w - origin_w % stride_;
int rh = origin_h - origin_h % stride_;
if (rw != origin_w || rh != origin_w) {
cv::cuda::resize(*im, *im, cv::Size(rw, rh), 0, 0, interp_);
mat->SetWidth(im->cols);
Expand All @@ -48,8 +48,8 @@ bool ResizeToIntMult::GpuRun(Mat* mat) {
}
#endif

bool ResizeToIntMult::Run(Mat* mat, int mult_int, int interp, ProcLib lib) {
auto r = ResizeToIntMult(mult_int, interp);
bool LimitByStride::Run(Mat* mat, int stride, int interp, ProcLib lib) {
auto r = LimitByStride(stride, interp);
return r(mat, lib);
}
} // namespace vision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,27 @@
namespace fastdeploy {
namespace vision {

class ResizeToIntMult : public Processor {
class LimitByStride : public Processor {
public:
explicit ResizeToIntMult(int mult_int = 32, int interp = 1) {
mult_int_ = mult_int;
explicit LimitByStride(int stride = 32, int interp = 1) {
stride_ = stride;
interp_ = interp;
}

// Resize Mat* mat to make the size divisible by stride_.

bool CpuRun(Mat* mat);
#ifdef ENABLE_OPENCV_CUDA
bool GpuRun(Mat* mat);
#endif
std::string Name() { return "ResizeToIntMult"; }
std::string Name() { return "LimitByStride"; }

static bool Run(Mat* mat, int mult_int = 32, int interp = 1,
static bool Run(Mat* mat, int stride = 32, int interp = 1,
ProcLib lib = ProcLib::OPENCV_CPU);

private:
int interp_;
int mult_int_;
int stride_;
};
} // namespace vision
} // namespace fastdeploy
70 changes: 70 additions & 0 deletions fastdeploy/vision/common/processors/limit_long.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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/common/processors/limit_long.h"

namespace fastdeploy {
namespace vision {

bool LimitLong::CpuRun(Mat* mat) {
cv::Mat* im = mat->GetCpuMat();
int origin_w = im->cols;
int origin_h = im->rows;
int im_size_max = std::max(origin_w, origin_h);
int target = im_size_max;
if (max_long_ > 0 && im_size_max > max_long_) {
target = max_long_;
} else if (min_long_ > 0 && im_size_max < min_long_) {
target = min_long_;
}
if (target != im_size_max) {
double scale =
static_cast<double>(target) / static_cast<double>(im_size_max);
cv::resize(*im, *im, cv::Size(), scale, scale, interp_);
mat->SetWidth(im->cols);
mat->SetHeight(im->rows);
}
return true;
}

#ifdef ENABLE_OPENCV_CUDA
bool LimitLong::GpuRun(Mat* mat) {
cv::cuda::GpuMat* im = mat->GetGpuMat();
int origin_w = im->cols;
int origin_h = im->rows;
im->convertTo(*im, CV_32FC(im->channels()));
int im_size_max = std::max(origin_w, origin_h);
int target = im_size_max;
if (max_long_ > 0 && im_size_max > max_long_) {
target = max_long_;
} else if (min_long_ > 0 && im_size_max < min_long_) {
target = min_long_;
}
if (target != im_size_max) {
double scale =
static_cast<double>(target) / static_cast<double>(im_size_max);
cv::cuda::resize(*im, *im, cv::Size(), scale, scale, interp_);
mat->SetWidth(im->cols);
mat->SetHeight(im->rows);
}
return true;
}
#endif

bool LimitLong::Run(Mat* mat, int max_long, int min_long, ProcLib lib) {
auto l = LimitLong(max_long, min_long);
return l(mat, lib);
}
} // namespace vision
} // namespace fastdeploy
51 changes: 51 additions & 0 deletions fastdeploy/vision/common/processors/limit_long.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.

#pragma once

#include "fastdeploy/vision/common/processors/base.h"

namespace fastdeploy {
namespace vision {

class LimitLong : public Processor {
public:
explicit LimitLong(int max_long = -1, int min_long = -1, int interp = 1) {
max_long_ = max_long;
min_long_ = min_long;
interp_ = interp;
}

// Limit the long edge of image.
// If the long edge is larger than max_long_, resize the long edge
// to max_long_, while scale the short edge proportionally.
// If the long edge is smaller than min_long_, resize the long edge
// to min_long_, while scale the short edge proportionally.
bool CpuRun(Mat* mat);
#ifdef ENABLE_OPENCV_CUDA
bool GpuRun(Mat* mat);
#endif
std::string Name() { return "LimitLong"; }

static bool Run(Mat* mat, int max_long = -1, int min_long = -1,
ProcLib lib = ProcLib::OPENCV_CPU);
int GetMaxLong() const { return max_long_; }

private:
int max_long_;
int min_long_;
int interp_;
};
} // namespace vision
} // namespace fastdeploy
Loading

0 comments on commit 0692dcc

Please sign in to comment.