rec_preprocessor.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/transform.h"
  17. #include "ultra_infer/vision/common/result.h"
  18. namespace ultra_infer {
  19. namespace vision {
  20. namespace ocr {
  21. /*! @brief Preprocessor object for PaddleClas serials model.
  22. */
  23. class ULTRAINFER_DECL RecognizerPreprocessor : public ProcessorManager {
  24. public:
  25. RecognizerPreprocessor();
  26. using ProcessorManager::Run;
  27. /** \brief Process the input image and prepare input tensors for runtime
  28. *
  29. * \param[in] images The input data list, all the elements are FDMat
  30. * \param[in] outputs The output tensors which will be fed into runtime
  31. * \return true if the preprocess succeeded, otherwise false
  32. */
  33. bool Run(std::vector<FDMat> *images, std::vector<FDTensor> *outputs,
  34. size_t start_index, size_t end_index,
  35. const std::vector<int> &indices);
  36. /** \brief Implement the virtual function of ProcessorManager, Apply() is the
  37. * body of Run(). Apply() contains the main logic of preprocessing, Run() is
  38. * called by users to execute preprocessing
  39. *
  40. * \param[in] image_batch The input image batch
  41. * \param[in] outputs The output tensors which will feed in runtime
  42. * \return true if the preprocess succeeded, otherwise false
  43. */
  44. virtual bool Apply(FDMatBatch *image_batch, std::vector<FDTensor> *outputs);
  45. /// Set static_shape_infer is true or not. When deploy PP-OCR
  46. /// on hardware which can not support dynamic input shape very well,
  47. /// like Huawei Ascned, static_shape_infer needs to to be true.
  48. void SetStaticShapeInfer(bool static_shape_infer) {
  49. static_shape_infer_ = static_shape_infer;
  50. }
  51. /// Get static_shape_infer of the recognition preprocess
  52. bool GetStaticShapeInfer() const { return static_shape_infer_; }
  53. /// Set preprocess normalize parameters, please call this API to customize
  54. /// the normalize parameters, otherwise it will use the default normalize
  55. /// parameters.
  56. void SetNormalize(const std::vector<float> &mean,
  57. const std::vector<float> &std, bool is_scale) {
  58. normalize_permute_op_ =
  59. std::make_shared<NormalizeAndPermute>(mean, std, is_scale);
  60. normalize_op_ = std::make_shared<Normalize>(mean, std, is_scale);
  61. }
  62. /// Set rec_image_shape for the recognition preprocess
  63. void SetRecImageShape(const std::vector<int> &rec_image_shape) {
  64. rec_image_shape_ = rec_image_shape;
  65. }
  66. /// Get rec_image_shape for the recognition preprocess
  67. std::vector<int> GetRecImageShape() { return rec_image_shape_; }
  68. /// This function will disable normalize in preprocessing step.
  69. void DisableNormalize() { disable_permute_ = true; }
  70. /// This function will disable hwc2chw in preprocessing step.
  71. void DisablePermute() { disable_normalize_ = true; }
  72. private:
  73. void OcrRecognizerResizeImage(FDMat *mat, float max_wh_ratio,
  74. const std::vector<int> &rec_image_shape,
  75. bool static_shape_infer);
  76. // for recording the switch of hwc2chw
  77. bool disable_permute_ = false;
  78. // for recording the switch of normalize
  79. bool disable_normalize_ = false;
  80. std::vector<int> rec_image_shape_ = {3, 48, 320};
  81. bool static_shape_infer_ = false;
  82. std::shared_ptr<Resize> resize_op_;
  83. std::shared_ptr<Pad> pad_op_;
  84. std::shared_ptr<NormalizeAndPermute> normalize_permute_op_;
  85. std::shared_ptr<Normalize> normalize_op_;
  86. std::shared_ptr<HWC2CHW> hwc2chw_op_;
  87. std::shared_ptr<Cast> cast_op_;
  88. };
  89. } // namespace ocr
  90. } // namespace vision
  91. } // namespace ultra_infer