dbcurvedetector.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_curve.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 DBCURVEDetector object is used to load the detection model provided
  29. * by PaddleOCR.
  30. */
  31. class ULTRAINFER_DECL DBCURVEDetector : public UltraInferModel {
  32. public:
  33. DBCURVEDetector();
  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. DBCURVEDetector(const std::string &model_file,
  45. const std::string &params_file = "",
  46. const RuntimeOption &custom_option = RuntimeOption(),
  47. const ModelFormat &model_format = ModelFormat::PADDLE);
  48. /** \brief Clone a new DBCURVEDetector with less memory usage when multiple
  49. * instances of the same model are created
  50. *
  51. * \return new DBCURVEDetector* type unique pointer
  52. */
  53. virtual std::unique_ptr<DBCURVEDetector> Clone() const;
  54. /// Get model's name
  55. std::string ModelName() const { return "ppocr/ocr_det"; }
  56. /** \brief Predict the input image and get OCR detection model result.
  57. *
  58. * \param[in] img The input image data, comes from cv::imread(), is a 3-D
  59. * array with layout HWC, BGR format. \param[in] boxes_result The output of
  60. * OCR detection model result will be written to this structure. \return true
  61. * if the prediction is succeeded, otherwise false.
  62. */
  63. virtual bool Predict(const cv::Mat &img,
  64. std::vector<std::vector<int>> *boxes_result);
  65. /** \brief Predict the input image and get OCR detection model result.
  66. *
  67. * \param[in] img The input image data, comes from cv::imread(), is a 3-D
  68. * array with layout HWC, BGR format. \param[in] ocr_result The output of OCR
  69. * detection model result will be written to this structure. \return true if
  70. * the prediction is succeeded, otherwise false.
  71. */
  72. virtual bool Predict(const cv::Mat &img, vision::OCRCURVEResult *ocr_result);
  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] det_results The output
  77. * of OCR detection model result will be written to this structure. \return
  78. * true if the prediction is succeeded, otherwise false.
  79. */
  80. virtual bool
  81. BatchPredict(const std::vector<cv::Mat> &images,
  82. std::vector<std::vector<std::vector<int>>> *det_results);
  83. /** \brief BatchPredict the input image and get OCR detection model result.
  84. *
  85. * \param[in] images The list input of image data, comes from cv::imread(), is
  86. * a 3-D array with layout HWC, BGR format. \param[in] ocr_results The output
  87. * of OCR detection model result will be written to this structure. \return
  88. * true if the prediction is succeeded, otherwise false.
  89. */
  90. virtual bool BatchPredict(const std::vector<cv::Mat> &images,
  91. std::vector<vision::OCRCURVEResult> *ocr_results);
  92. /// Get preprocessor reference of DBCURVEDetectorPreprocessor
  93. virtual DBDetectorPreprocessor &GetPreprocessor() { return preprocessor_; }
  94. /// Get postprocessor reference of DBCURVEDetectorPostprocessor
  95. virtual DBCURVEDetectorPostprocessor &GetPostprocessor() {
  96. return postprocessor_;
  97. }
  98. private:
  99. bool Initialize();
  100. DBDetectorPreprocessor preprocessor_;
  101. DBCURVEDetectorPostprocessor postprocessor_;
  102. };
  103. } // namespace ocr
  104. } // namespace vision
  105. } // namespace ultra_infer