[Bug Fix]fix tracking 'LetterBoxResize' (#1711)

This commit is contained in:
ChaoII
2023-03-27 14:03:09 +08:00
committed by GitHub
parent 0c543c010e
commit d43e9011eb
2 changed files with 121 additions and 9 deletions

View File

@@ -18,7 +18,8 @@
namespace fastdeploy{
namespace vision{
bool LetterBoxResize::operator()(Mat* mat, ProcLib lib) {
bool LetterBoxResize::ImplByOpenCV(Mat* mat){
if (mat->Channels() != color_.size()) {
FDERROR << "LetterBoxResize: Require input channels equals to size of color value, "
"but now channels = "
@@ -45,14 +46,117 @@ bool LetterBoxResize::operator()(Mat* mat, ProcLib lib) {
int bottom = std::round(padh + 0.1);
int left = std::round(padw - 0.1);
int right = std::round(padw + 0.1);
Resize::Run(mat, new_shape_w, new_shape_h, -1.0, -1.0, 3, false, lib);
Pad::Run(mat, top, bottom, left, right, color_, lib);
Resize::Run(mat, new_shape_w, new_shape_h, -1.0, -1.0, 3, false);
Pad::Run(mat, top, bottom, left, right, color_);
return true;
}
bool LetterBoxResize::Run(Mat* mat, const std::vector<int>& target_size, const std::vector<float>& color, ProcLib lib) {
#ifdef ENABLE_FLYCV
bool LetterBoxResize::ImplByFlyCV(Mat* mat){
if (mat->Channels() != color_.size()) {
FDERROR << "LetterBoxResize: Require input channels equals to size of color value, "
"but now channels = "
<< mat->Channels()
<< ", the size of color values = " << color_.size() << "."
<< std::endl;
return false;
}
// generate scale_factor
int origin_w = mat->Width();
int origin_h = mat->Height();
int target_h = target_size_[0];
int target_w = target_size_[1];
float ratio_h = static_cast<float>(target_h) / static_cast<float>(origin_h);
float ratio_w = static_cast<float>(target_w) / static_cast<float>(origin_w);
float resize_scale = std::min(ratio_h, ratio_w);
// get_resized_shape
int new_shape_w = std::round(origin_w * resize_scale);
int new_shape_h = std::round(origin_h * resize_scale);
// calculate pad
float padw = (target_size_[1] - new_shape_w) / 2.;
float padh = (target_size_[0] - new_shape_h) / 2.;
int top = std::round(padh - 0.1);
int bottom = std::round(padh + 0.1);
int left = std::round(padw - 0.1);
int right = std::round(padw + 0.1);
Resize::Run(mat, new_shape_w, new_shape_h, -1.0, -1.0, 3, false, ProcLib::FLYCV);
Pad::Run(mat, top, bottom, left, right, color_, ProcLib::FLYCV);
return true;
}
#endif
#ifdef ENABLE_CVCUDA
bool LetterBoxResize::ImplByCvCuda(Mat* mat){
if (mat->Channels() != color_.size()) {
FDERROR << "LetterBoxResize: Require input channels equals to size of color value, "
"but now channels = "
<< mat->Channels()
<< ", the size of color values = " << color_.size() << "."
<< std::endl;
return false;
}
// generate scale_factor
int origin_w = mat->Width();
int origin_h = mat->Height();
int target_h = target_size_[0];
int target_w = target_size_[1];
float ratio_h = static_cast<float>(target_h) / static_cast<float>(origin_h);
float ratio_w = static_cast<float>(target_w) / static_cast<float>(origin_w);
float resize_scale = std::min(ratio_h, ratio_w);
// get_resized_shape
int new_shape_w = std::round(origin_w * resize_scale);
int new_shape_h = std::round(origin_h * resize_scale);
// calculate pad
float padw = (target_size_[1] - new_shape_w) / 2.;
float padh = (target_size_[0] - new_shape_h) / 2.;
int top = std::round(padh - 0.1);
int bottom = std::round(padh + 0.1);
int left = std::round(padw - 0.1);
int right = std::round(padw + 0.1);
Resize::Run(mat, new_shape_w, new_shape_h, -1.0, -1.0, 3, false, ProcLib::CVCUDA);
Pad::Run(mat, top, bottom, left, right, color_, ProcLib::CVCUDA);
return true;
}
#endif
#ifdef ENABLE_CUDA
bool LetterBoxResize::ImplByCuda(Mat* mat){
if (mat->Channels() != color_.size()) {
FDERROR << "LetterBoxResize: Require input channels equals to size of color value, "
"but now channels = "
<< mat->Channels()
<< ", the size of color values = " << color_.size() << "."
<< std::endl;
return false;
}
// generate scale_factor
int origin_w = mat->Width();
int origin_h = mat->Height();
int target_h = target_size_[0];
int target_w = target_size_[1];
float ratio_h = static_cast<float>(target_h) / static_cast<float>(origin_h);
float ratio_w = static_cast<float>(target_w) / static_cast<float>(origin_w);
float resize_scale = std::min(ratio_h, ratio_w);
// get_resized_shape
int new_shape_w = std::round(origin_w * resize_scale);
int new_shape_h = std::round(origin_h * resize_scale);
// calculate pad
float padw = (target_size_[1] - new_shape_w) / 2.;
float padh = (target_size_[0] - new_shape_h) / 2.;
int top = std::round(padh - 0.1);
int bottom = std::round(padh + 0.1);
int left = std::round(padw - 0.1);
int right = std::round(padw + 0.1);
Resize::Run(mat, new_shape_w, new_shape_h, -1.0, -1.0, 3, false, ProcLib::CUDA);
Pad::Run(mat, top, bottom, left, right, color_, ProcLib::CUDA);
return true;
}
#endif
bool LetterBoxResize::Run(Mat* mat, const std::vector<int>& target_size, const std::vector<float>& color,ProcLib lib) {
auto l = LetterBoxResize(target_size,color);
return l(mat, lib);
return l(mat,lib);
}
} // namespace vision

View File

@@ -27,13 +27,21 @@ class LetterBoxResize : public Processor {
color_ = color;
}
std::string Name() { return "LetterBoxResize"; }
std::string Name() override { return "LetterBoxResize"; }
bool ImplByOpenCV(Mat* mat) override;
#ifdef ENABLE_FLYCV
bool ImplByFlyCV(FDMat* mat)override;
#endif
#ifdef ENABLE_CVCUDA
virtual bool ImplByCvCuda(FDMat* mat)override;
#endif
virtual bool operator()(Mat* mat, ProcLib lib = ProcLib::DEFAULT);
#ifdef ENABLE_CUDA
virtual bool ImplByCuda(FDMat* mat);
#endif
static bool Run(Mat* mat, const std::vector<int>& target_size,
const std::vector<float>& color,
ProcLib lib = ProcLib::DEFAULT);
const std::vector<float>& color,ProcLib lib = ProcLib::DEFAULT);
private:
std::vector<int> target_size_;