Skip to content

Commit d43e901

Browse files
authored
[Bug Fix]fix tracking 'LetterBoxResize' (PaddlePaddle#1711)
1 parent 0c543c0 commit d43e901

File tree

2 files changed

+122
-10
lines changed

2 files changed

+122
-10
lines changed

fastdeploy/vision/tracking/pptracking/letter_box_resize.cc

+109-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
namespace fastdeploy{
1919
namespace vision{
2020

21-
bool LetterBoxResize::operator()(Mat* mat, ProcLib lib) {
21+
22+
bool LetterBoxResize::ImplByOpenCV(Mat* mat){
2223
if (mat->Channels() != color_.size()) {
2324
FDERROR << "LetterBoxResize: Require input channels equals to size of color value, "
2425
"but now channels = "
@@ -45,14 +46,117 @@ bool LetterBoxResize::operator()(Mat* mat, ProcLib lib) {
4546
int bottom = std::round(padh + 0.1);
4647
int left = std::round(padw - 0.1);
4748
int right = std::round(padw + 0.1);
48-
Resize::Run(mat, new_shape_w, new_shape_h, -1.0, -1.0, 3, false, lib);
49-
Pad::Run(mat, top, bottom, left, right, color_, lib);
49+
Resize::Run(mat, new_shape_w, new_shape_h, -1.0, -1.0, 3, false);
50+
Pad::Run(mat, top, bottom, left, right, color_);
5051
return true;
5152
}
5253

53-
bool LetterBoxResize::Run(Mat* mat, const std::vector<int>& target_size, const std::vector<float>& color, ProcLib lib) {
54+
#ifdef ENABLE_FLYCV
55+
bool LetterBoxResize::ImplByFlyCV(Mat* mat){
56+
if (mat->Channels() != color_.size()) {
57+
FDERROR << "LetterBoxResize: Require input channels equals to size of color value, "
58+
"but now channels = "
59+
<< mat->Channels()
60+
<< ", the size of color values = " << color_.size() << "."
61+
<< std::endl;
62+
return false;
63+
}
64+
// generate scale_factor
65+
int origin_w = mat->Width();
66+
int origin_h = mat->Height();
67+
int target_h = target_size_[0];
68+
int target_w = target_size_[1];
69+
float ratio_h = static_cast<float>(target_h) / static_cast<float>(origin_h);
70+
float ratio_w = static_cast<float>(target_w) / static_cast<float>(origin_w);
71+
float resize_scale = std::min(ratio_h, ratio_w);
72+
// get_resized_shape
73+
int new_shape_w = std::round(origin_w * resize_scale);
74+
int new_shape_h = std::round(origin_h * resize_scale);
75+
// calculate pad
76+
float padw = (target_size_[1] - new_shape_w) / 2.;
77+
float padh = (target_size_[0] - new_shape_h) / 2.;
78+
int top = std::round(padh - 0.1);
79+
int bottom = std::round(padh + 0.1);
80+
int left = std::round(padw - 0.1);
81+
int right = std::round(padw + 0.1);
82+
Resize::Run(mat, new_shape_w, new_shape_h, -1.0, -1.0, 3, false, ProcLib::FLYCV);
83+
Pad::Run(mat, top, bottom, left, right, color_, ProcLib::FLYCV);
84+
return true;
85+
}
86+
#endif
87+
88+
#ifdef ENABLE_CVCUDA
89+
bool LetterBoxResize::ImplByCvCuda(Mat* mat){
90+
if (mat->Channels() != color_.size()) {
91+
FDERROR << "LetterBoxResize: Require input channels equals to size of color value, "
92+
"but now channels = "
93+
<< mat->Channels()
94+
<< ", the size of color values = " << color_.size() << "."
95+
<< std::endl;
96+
return false;
97+
}
98+
// generate scale_factor
99+
int origin_w = mat->Width();
100+
int origin_h = mat->Height();
101+
int target_h = target_size_[0];
102+
int target_w = target_size_[1];
103+
float ratio_h = static_cast<float>(target_h) / static_cast<float>(origin_h);
104+
float ratio_w = static_cast<float>(target_w) / static_cast<float>(origin_w);
105+
float resize_scale = std::min(ratio_h, ratio_w);
106+
// get_resized_shape
107+
int new_shape_w = std::round(origin_w * resize_scale);
108+
int new_shape_h = std::round(origin_h * resize_scale);
109+
// calculate pad
110+
float padw = (target_size_[1] - new_shape_w) / 2.;
111+
float padh = (target_size_[0] - new_shape_h) / 2.;
112+
int top = std::round(padh - 0.1);
113+
int bottom = std::round(padh + 0.1);
114+
int left = std::round(padw - 0.1);
115+
int right = std::round(padw + 0.1);
116+
Resize::Run(mat, new_shape_w, new_shape_h, -1.0, -1.0, 3, false, ProcLib::CVCUDA);
117+
Pad::Run(mat, top, bottom, left, right, color_, ProcLib::CVCUDA);
118+
return true;
119+
}
120+
#endif
121+
122+
#ifdef ENABLE_CUDA
123+
bool LetterBoxResize::ImplByCuda(Mat* mat){
124+
if (mat->Channels() != color_.size()) {
125+
FDERROR << "LetterBoxResize: Require input channels equals to size of color value, "
126+
"but now channels = "
127+
<< mat->Channels()
128+
<< ", the size of color values = " << color_.size() << "."
129+
<< std::endl;
130+
return false;
131+
}
132+
// generate scale_factor
133+
int origin_w = mat->Width();
134+
int origin_h = mat->Height();
135+
int target_h = target_size_[0];
136+
int target_w = target_size_[1];
137+
float ratio_h = static_cast<float>(target_h) / static_cast<float>(origin_h);
138+
float ratio_w = static_cast<float>(target_w) / static_cast<float>(origin_w);
139+
float resize_scale = std::min(ratio_h, ratio_w);
140+
// get_resized_shape
141+
int new_shape_w = std::round(origin_w * resize_scale);
142+
int new_shape_h = std::round(origin_h * resize_scale);
143+
// calculate pad
144+
float padw = (target_size_[1] - new_shape_w) / 2.;
145+
float padh = (target_size_[0] - new_shape_h) / 2.;
146+
int top = std::round(padh - 0.1);
147+
int bottom = std::round(padh + 0.1);
148+
int left = std::round(padw - 0.1);
149+
int right = std::round(padw + 0.1);
150+
Resize::Run(mat, new_shape_w, new_shape_h, -1.0, -1.0, 3, false, ProcLib::CUDA);
151+
Pad::Run(mat, top, bottom, left, right, color_, ProcLib::CUDA);
152+
return true;
153+
}
154+
#endif
155+
156+
157+
bool LetterBoxResize::Run(Mat* mat, const std::vector<int>& target_size, const std::vector<float>& color,ProcLib lib) {
54158
auto l = LetterBoxResize(target_size,color);
55-
return l(mat, lib);
159+
return l(mat,lib);
56160
}
57161

58162
} // namespace vision

fastdeploy/vision/tracking/pptracking/letter_box_resize.h

+13-5
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,21 @@ class LetterBoxResize : public Processor {
2727
color_ = color;
2828
}
2929

30-
std::string Name() { return "LetterBoxResize"; }
31-
32-
virtual bool operator()(Mat* mat, ProcLib lib = ProcLib::DEFAULT);
30+
std::string Name() override { return "LetterBoxResize"; }
31+
bool ImplByOpenCV(Mat* mat) override;
32+
#ifdef ENABLE_FLYCV
33+
bool ImplByFlyCV(FDMat* mat)override;
34+
#endif
35+
#ifdef ENABLE_CVCUDA
36+
virtual bool ImplByCvCuda(FDMat* mat)override;
37+
#endif
38+
39+
#ifdef ENABLE_CUDA
40+
virtual bool ImplByCuda(FDMat* mat);
41+
#endif
3342

3443
static bool Run(Mat* mat, const std::vector<int>& target_size,
35-
const std::vector<float>& color,
36-
ProcLib lib = ProcLib::DEFAULT);
44+
const std::vector<float>& color,ProcLib lib = ProcLib::DEFAULT);
3745

3846
private:
3947
std::vector<int> target_size_;

0 commit comments

Comments
 (0)