uvdocwarpper.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/ultra_infer_model.h"
  16. #include "ultra_infer/utils/unique_ptr.h"
  17. #include "ultra_infer/vision/common/processors/transform.h"
  18. #include "ultra_infer/vision/ocr/ppocr/utils/ocr_postprocess_op.h"
  19. #include "ultra_infer/vision/ocr/ppocr/uvdoc_postprocessor.h"
  20. #include "ultra_infer/vision/ocr/ppocr/uvdoc_preprocessor.h"
  21. namespace ultra_infer {
  22. namespace vision {
  23. /** \brief All OCR series model APIs are defined inside this namespace
  24. *
  25. */
  26. namespace ocr {
  27. /*! @brief UVDocWarpper object is used to load the detection model provided by
  28. * PaddleOCR.
  29. */
  30. class ULTRAINFER_DECL UVDocWarpper : public UltraInferModel {
  31. public:
  32. UVDocWarpper();
  33. /** \brief Set path of model file, and the configuration of runtime
  34. *
  35. * \param[in] model_file Path of model file, e.g
  36. * ./ch_PP-OCRv3_det_infer/model.pdmodel. \param[in] params_file Path of
  37. * parameter file, e.g ./ch_PP-OCRv3_det_infer/model.pdiparams, if the model
  38. * format is ONNX, this parameter will be ignored. \param[in] custom_option
  39. * RuntimeOption for inference, the default will use cpu, and choose the
  40. * backend defined in `valid_cpu_backends`. \param[in] model_format Model
  41. * format of the loaded model, default is Paddle format.
  42. */
  43. UVDocWarpper(const std::string &model_file,
  44. const std::string &params_file = "",
  45. const RuntimeOption &custom_option = RuntimeOption(),
  46. const ModelFormat &model_format = ModelFormat::PADDLE);
  47. /** \brief Clone a new UVDocWarpper with less memory usage when multiple
  48. * instances of the same model are created
  49. *
  50. * \return new UVDocWarpper* type unique pointer
  51. */
  52. virtual std::unique_ptr<UVDocWarpper> Clone() const;
  53. /// Get model's name
  54. std::string ModelName() const { return "pp-uvdoc"; }
  55. /** \brief Predict the input image and get OCR detection model result.
  56. *
  57. * \param[in] img The input image data, comes from cv::imread(), is a 3-D
  58. * array with layout HWC, BGR format. \param[in] result The output of OCR
  59. * detection model result will be written to this structure. \return true if
  60. * the prediction is succeeded, otherwise false.
  61. */
  62. virtual bool Predict(cv::Mat *img, FDTensor *result);
  63. virtual bool Predict(const cv::Mat &img, FDTensor *result);
  64. /** \brief Predict the input image and get OCR detection model result.
  65. *
  66. * \param[in] img The input image data, comes from cv::imread(), is a 3-D
  67. * array with layout HWC, BGR format. \param[in] result The output of OCR
  68. * detection model result will be written to this structure. \return true if
  69. * the prediction is succeeded, otherwise false.
  70. */
  71. virtual bool BatchPredict(const std::vector<cv::Mat> &images,
  72. std::vector<FDTensor> *results);
  73. /** \brief BatchPredict the input image and get OCR detection model result.
  74. *
  75. * \param[in] images The list input of image data, comes from cv::imread(), is
  76. * a 3-D array with layout HWC, BGR format. \param[in] results The output of
  77. * OCR detection model result will be written to this structure. \return true
  78. * if the prediction is succeeded, otherwise false.
  79. */
  80. /// Get preprocessor reference of UVDocWarpperPreprocessor
  81. virtual UVDocPreprocessor &GetPreprocessor() { return preprocessor_; }
  82. // / Get postprocessor reference of UVDocWarpperPostprocessor
  83. virtual UVDocPostprocessor &GetPostprocessor() { return postprocessor_; }
  84. private:
  85. bool Initialize();
  86. UVDocPreprocessor preprocessor_;
  87. UVDocPostprocessor postprocessor_;
  88. };
  89. } // namespace ocr
  90. } // namespace vision
  91. } // namespace ultra_infer