dbdetector.h 4.8 KB

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