preprocessor.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #pragma once
  15. #include "ultra_infer/vision/common/processors/transform.h"
  16. #include "ultra_infer/vision/common/result.h"
  17. namespace ultra_infer {
  18. namespace vision {
  19. namespace detection {
  20. /*! @brief Preprocessor object for YOLOv5 serials model.
  21. */
  22. class ULTRAINFER_DECL YOLOv5Preprocessor {
  23. public:
  24. /** \brief Create a preprocessor instance for YOLOv5 serials model
  25. */
  26. YOLOv5Preprocessor();
  27. /** \brief Process the input image and prepare input tensors for runtime
  28. *
  29. * \param[in] images The input image data list, all the elements are returned
  30. * by cv::imread() \param[in] outputs The output tensors which will feed in
  31. * runtime \param[in] ims_info The shape info list, record input_shape and
  32. * output_shape \return true if the preprocess successed, otherwise false
  33. */
  34. bool Run(std::vector<FDMat> *images, std::vector<FDTensor> *outputs,
  35. std::vector<std::map<std::string, std::array<float, 2>>> *ims_info);
  36. /// Set target size, tuple of (width, height), default size = {640, 640}
  37. void SetSize(const std::vector<int> &size) { size_ = size; }
  38. /// Get target size, tuple of (width, height), default size = {640, 640}
  39. std::vector<int> GetSize() const { return size_; }
  40. /// Set padding value, size should be the same as channels
  41. void SetPaddingValue(const std::vector<float> &padding_value) {
  42. padding_value_ = padding_value;
  43. }
  44. /// Get padding value, size should be the same as channels
  45. std::vector<float> GetPaddingValue() const { return padding_value_; }
  46. /// Set is_scale_up, if is_scale_up is false, the input image only
  47. /// can be zoom out, the maximum resize scale cannot exceed 1.0, default true
  48. void SetScaleUp(bool is_scale_up) { is_scale_up_ = is_scale_up; }
  49. /// Get is_scale_up, default true
  50. bool GetScaleUp() const { return is_scale_up_; }
  51. /// Set is_mini_pad, pad to the minimum rectangle
  52. /// which height and width is times of stride
  53. void SetMiniPad(bool is_mini_pad) { is_mini_pad_ = is_mini_pad; }
  54. /// Get is_mini_pad, default false
  55. bool GetMiniPad() const { return is_mini_pad_; }
  56. /// Set padding stride, only for mini_pad mode
  57. void SetStride(int stride) { stride_ = stride; }
  58. /// Get padding stride, default 32
  59. bool GetStride() const { return stride_; }
  60. protected:
  61. bool Preprocess(FDMat *mat, FDTensor *output,
  62. std::map<std::string, std::array<float, 2>> *im_info);
  63. void LetterBox(FDMat *mat);
  64. // target size, tuple of (width, height), default size = {640, 640}
  65. std::vector<int> size_;
  66. // padding value, size should be the same as channels
  67. std::vector<float> padding_value_;
  68. // only pad to the minimum rectangle which height and width is times of stride
  69. bool is_mini_pad_;
  70. // while is_mini_pad = false and is_no_pad = true,
  71. // will resize the image to the set size
  72. bool is_no_pad_;
  73. // if is_scale_up is false, the input image only can be zoom out,
  74. // the maximum resize scale cannot exceed 1.0
  75. bool is_scale_up_;
  76. // padding stride, for is_mini_pad
  77. int stride_;
  78. // for offsetting the boxes by classes when using NMS
  79. float max_wh_;
  80. };
  81. } // namespace detection
  82. } // namespace vision
  83. } // namespace ultra_infer