mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-12-24 13:28:13 +08:00
[Model] Move letter box resize code (#502)
* Remove letter box resize code * Remove letter box resize code * Add model test for mobilenetv2
This commit is contained in:
60
fastdeploy/vision/tracking/pptracking/letter_box_resize.cc
Normal file
60
fastdeploy/vision/tracking/pptracking/letter_box_resize.cc
Normal file
@@ -0,0 +1,60 @@
|
||||
// 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/tracking/pptracking/letter_box_resize.h"
|
||||
#include "fastdeploy/vision/common/processors/transform.h"
|
||||
|
||||
namespace fastdeploy{
|
||||
namespace vision{
|
||||
|
||||
bool LetterBoxResize::operator()(Mat* mat, ProcLib lib) {
|
||||
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;
|
||||
}
|
||||
cv::Mat* im = mat->GetOpenCVMat();
|
||||
// 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, lib);
|
||||
Pad::Run(mat, top, bottom, left, right, color_, lib);
|
||||
return true;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
} // namespace vision
|
||||
} // namespace fastdeploy
|
||||
43
fastdeploy/vision/tracking/pptracking/letter_box_resize.h
Normal file
43
fastdeploy/vision/tracking/pptracking/letter_box_resize.h
Normal file
@@ -0,0 +1,43 @@
|
||||
// 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 LetterBoxResize : public Processor {
|
||||
public:
|
||||
LetterBoxResize(const std::vector<int>& target_size,
|
||||
const std::vector<float>& color) {
|
||||
target_size_ = target_size;
|
||||
color_ = color;
|
||||
}
|
||||
|
||||
std::string Name() { return "LetterBoxResize"; }
|
||||
|
||||
virtual bool operator()(Mat* mat, ProcLib lib = ProcLib::DEFAULT);
|
||||
|
||||
static bool Run(Mat* mat, const std::vector<int>& target_size,
|
||||
const std::vector<float>& color,
|
||||
ProcLib lib = ProcLib::DEFAULT);
|
||||
|
||||
private:
|
||||
std::vector<int> target_size_;
|
||||
std::vector<float> color_;
|
||||
};
|
||||
} // namespace vision
|
||||
} // namespace fastdeploy
|
||||
@@ -13,6 +13,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
#include "fastdeploy/vision/tracking/pptracking/model.h"
|
||||
#include "fastdeploy/vision/tracking/pptracking/letter_box_resize.h"
|
||||
#include "yaml-cpp/yaml.h"
|
||||
|
||||
namespace fastdeploy {
|
||||
|
||||
Reference in New Issue
Block a user