FastDeploy  latest
Fast & Easy to Deploy!
preprocessor.h
1 // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 #include "fastdeploy/vision/common/processors/transform.h"
17 #include "fastdeploy/vision/common/result.h"
18 
19 namespace fastdeploy {
20 namespace vision {
21 
22 namespace detection {
25 class FASTDEPLOY_DECL YOLOv5Preprocessor {
26  public:
30 
38  bool Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs,
39  std::vector<std::map<std::string, std::array<float, 2>>>* ims_info);
40 
42  void SetSize(const std::vector<int>& size) { size_ = size; }
43 
45  std::vector<int> GetSize() const { return size_; }
46 
48  void SetPaddingValue(const std::vector<float>& padding_value) {
49  padding_value_ = padding_value;
50  }
51 
53  std::vector<float> GetPaddingValue() const { return padding_value_; }
54 
57  void SetScaleUp(bool is_scale_up) {
58  is_scale_up_ = is_scale_up;
59  }
60 
62  bool GetScaleUp() const { return is_scale_up_; }
63 
66  void SetMiniPad(bool is_mini_pad) {
67  is_mini_pad_ = is_mini_pad;
68  }
69 
71  bool GetMiniPad() const { return is_mini_pad_; }
72 
74  void SetStride(int stride) {
75  stride_ = stride;
76  }
77 
79  bool GetStride() const { return stride_; }
80 
81  protected:
82  bool Preprocess(FDMat* mat, FDTensor* output,
83  std::map<std::string, std::array<float, 2>>* im_info);
84 
85  void LetterBox(FDMat* mat);
86 
87  // target size, tuple of (width, height), default size = {640, 640}
88  std::vector<int> size_;
89 
90  // padding value, size should be the same as channels
91  std::vector<float> padding_value_;
92 
93  // only pad to the minimum rectange which height and width is times of stride
94  bool is_mini_pad_;
95 
96  // while is_mini_pad = false and is_no_pad = true,
97  // will resize the image to the set size
98  bool is_no_pad_;
99 
100  // if is_scale_up is false, the input image only can be zoom out,
101  // the maximum resize scale cannot exceed 1.0
102  bool is_scale_up_;
103 
104  // padding stride, for is_mini_pad
105  int stride_;
106 
107  // for offseting the boxes by classes when using NMS
108  float max_wh_;
109 };
110 
111 } // namespace detection
112 } // namespace vision
113 } // namespace fastdeploy
void SetScaleUp(bool is_scale_up)
Definition: preprocessor.h:57
std::vector< float > GetPaddingValue() const
Get padding value, size should be the same as channels.
Definition: preprocessor.h:53
bool GetMiniPad() const
Get is_mini_pad, default false.
Definition: preprocessor.h:71
void SetStride(int stride)
Set padding stride, only for mini_pad mode.
Definition: preprocessor.h:74
FDTensor object used to represend data matrix.
Definition: fd_tensor.h:31
bool GetScaleUp() const
Get is_scale_up, default true.
Definition: preprocessor.h:62
void SetMiniPad(bool is_mini_pad)
Definition: preprocessor.h:66
std::vector< int > GetSize() const
Get target size, tuple of (width, height), default size = {640, 640}.
Definition: preprocessor.h:45
bool GetStride() const
Get padding stride, default 32.
Definition: preprocessor.h:79
FDMat is a structure for replace cv::Mat.
Definition: mat.h:34
void SetPaddingValue(const std::vector< float > &padding_value)
Set padding value, size should be the same as channels.
Definition: preprocessor.h:48
Preprocessor object for YOLOv5 serials model.
Definition: preprocessor.h:25
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16
void SetSize(const std::vector< int > &size)
Set target size, tuple of (width, height), default size = {640, 640}.
Definition: preprocessor.h:42