det_preprocessor.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/manager.h"
  16. #include "ultra_infer/vision/common/processors/normalize_and_permute.h"
  17. #include "ultra_infer/vision/common/processors/pad.h"
  18. #include "ultra_infer/vision/common/processors/resize.h"
  19. #include "ultra_infer/vision/common/result.h"
  20. namespace ultra_infer {
  21. namespace vision {
  22. namespace ocr {
  23. /*! @brief Preprocessor object for DBDetector serials model.
  24. */
  25. class ULTRAINFER_DECL DBDetectorPreprocessor : public ProcessorManager {
  26. public:
  27. DBDetectorPreprocessor();
  28. /** \brief Process the input image and prepare input tensors for runtime
  29. *
  30. * \param[in] image_batch The input image batch
  31. * \param[in] outputs The output tensors which will feed in runtime
  32. * \return true if the preprocess succeeded, otherwise false
  33. */
  34. virtual bool Apply(FDMatBatch *image_batch, std::vector<FDTensor> *outputs);
  35. /// Set max_side_len for the detection preprocess, default is 960
  36. void SetMaxSideLen(int max_side_len) { max_side_len_ = max_side_len; }
  37. /// Get max_side_len of the detection preprocess
  38. int GetMaxSideLen() const { return max_side_len_; }
  39. /// Set preprocess normalize parameters, please call this API to customize
  40. /// the normalize parameters, otherwise it will use the default normalize
  41. /// parameters.
  42. void SetNormalize(const std::vector<float> &mean,
  43. const std::vector<float> &std, bool is_scale) {
  44. normalize_permute_op_ =
  45. std::make_shared<NormalizeAndPermute>(mean, std, is_scale);
  46. }
  47. /// Get the image info of the last batch, return a list of array
  48. /// {image width, image height, resize width, resize height}
  49. const std::vector<std::array<int, 4>> *GetBatchImgInfo() {
  50. return &batch_det_img_info_;
  51. }
  52. /// This function will disable normalize in preprocessing step.
  53. void DisableNormalize() { disable_permute_ = true; }
  54. /// This function will disable hwc2chw in preprocessing step.
  55. void DisablePermute() { disable_normalize_ = true; }
  56. /// Set det_image_shape for the detection preprocess.
  57. /// This api is usually used when you retrain the model.
  58. /// Generally, you do not need to use it.
  59. void SetDetImageShape(const std::vector<int> &det_image_shape) {
  60. det_image_shape_ = det_image_shape;
  61. }
  62. /// Get cls_image_shape for the classification preprocess
  63. std::vector<int> GetDetImageShape() const { return det_image_shape_; }
  64. /// Set static_shape_infer is true or not. When deploy PP-OCR
  65. /// on hardware which can not support dynamic input shape very well,
  66. /// like Huawei Ascned, static_shape_infer needs to to be true.
  67. void SetStaticShapeInfer(bool static_shape_infer) {
  68. static_shape_infer_ = static_shape_infer;
  69. }
  70. /// Get static_shape_infer of the recognition preprocess
  71. bool GetStaticShapeInfer() const { return static_shape_infer_; }
  72. private:
  73. bool ResizeImage(FDMat *img, int resize_w, int resize_h, int max_resize_w,
  74. int max_resize_h);
  75. // for recording the switch of hwc2chw
  76. bool disable_permute_ = false;
  77. // for recording the switch of normalize
  78. bool disable_normalize_ = false;
  79. int max_side_len_ = 960;
  80. std::vector<std::array<int, 4>> batch_det_img_info_;
  81. std::shared_ptr<Resize> resize_op_;
  82. std::shared_ptr<Pad> pad_op_;
  83. std::shared_ptr<NormalizeAndPermute> normalize_permute_op_;
  84. std::vector<int> det_image_shape_ = {3, 960, 960};
  85. bool static_shape_infer_ = false;
  86. std::array<int, 4> OcrDetectorGetInfo(FDMat *img, int max_size_len);
  87. };
  88. } // namespace ocr
  89. } // namespace vision
  90. } // namespace ultra_infer