preprocessor.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 RKYOLOPreprocessor {
  23. public:
  24. /** \brief Create a preprocessor instance for YOLOv5 serials model
  25. */
  26. RKYOLOPreprocessor();
  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. /// Set target size, tuple of (width, height), default size = {640, 640}
  36. void SetSize(const std::vector<int> &size) { size_ = size; }
  37. /// Get target size, tuple of (width, height), default size = {640, 640}
  38. std::vector<int> GetSize() const { return size_; }
  39. /// Set padding value, size should be the same as channels
  40. void SetPaddingValue(const std::vector<float> &padding_value) {
  41. padding_value_ = padding_value;
  42. }
  43. /// Get padding value, size should be the same as channels
  44. std::vector<float> GetPaddingValue() const { return padding_value_; }
  45. /// Set is_scale_up, if is_scale_up is false, the input image only
  46. /// can be zoom out, the maximum resize scale cannot exceed 1.0, default true
  47. void SetScaleUp(bool is_scale_up) { is_scale_up_ = is_scale_up; }
  48. /// Get is_scale_up, default true
  49. bool GetScaleUp() const { return is_scale_up_; }
  50. std::vector<std::vector<int>> GetPadHWValues() const {
  51. return pad_hw_values_;
  52. }
  53. std::vector<float> GetScale() const { return scale_; }
  54. protected:
  55. bool Preprocess(FDMat *mat, FDTensor *output);
  56. void LetterBox(FDMat *mat);
  57. // target size, tuple of (width, height), default size = {640, 640}
  58. std::vector<int> size_;
  59. // padding value, size should be the same as channels
  60. std::vector<float> padding_value_;
  61. // only pad to the minimum rectangle which height and width is times of stride
  62. bool is_mini_pad_;
  63. // while is_mini_pad = false and is_no_pad = true,
  64. // will resize the image to the set size
  65. bool is_no_pad_;
  66. // if is_scale_up is false, the input image only can be zoom out,
  67. // the maximum resize scale cannot exceed 1.0
  68. bool is_scale_up_;
  69. // padding stride, for is_mini_pad
  70. int stride_;
  71. // for offsetting the boxes by classes when using NMS
  72. float max_wh_;
  73. std::vector<std::vector<int>> pad_hw_values_;
  74. std::vector<float> scale_;
  75. };
  76. } // namespace detection
  77. } // namespace vision
  78. } // namespace ultra_infer