structurev2_table.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/structurev2_table_postprocessor.h"
  20. #include "ultra_infer/vision/ocr/ppocr/structurev2_table_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 StructureV2Table : public UltraInferModel {
  32. public:
  33. StructureV2Table();
  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. * ./en_ppstructure_mobile_v2.0_SLANet_infer/model.pdmodel. \param[in]
  38. * params_file Path of parameter file, e.g
  39. * ./en_ppstructure_mobile_v2.0_SLANet_infer/model.pdiparams, if the model
  40. * format is ONNX, this parameter will be ignored. \param[in] custom_option
  41. * RuntimeOption for inference, the default will use cpu, and choose the
  42. * backend defined in `valid_cpu_backends`. \param[in] model_format Model
  43. * format of the loaded model, default is Paddle format. \param[in] box_shape
  44. * Type of output box, default is ori.
  45. */
  46. StructureV2Table(const std::string &model_file,
  47. const std::string &params_file = "",
  48. const std::string &table_char_dict_path = "",
  49. const std::string &box_shape = "ori",
  50. const RuntimeOption &custom_option = RuntimeOption(),
  51. const ModelFormat &model_format = ModelFormat::PADDLE);
  52. /** \brief Clone a new StructureV2Table Recognizer with less memory usage when
  53. * multiple instances of the same model are created
  54. *
  55. * \return new StructureV2Table* type unique pointer
  56. */
  57. virtual std::unique_ptr<StructureV2Table> Clone() const;
  58. /// Get model's name
  59. std::string ModelName() const { return "ppocr/ocr_table"; }
  60. /** \brief Predict the input image and get OCR detection model result.
  61. *
  62. * \param[in] img The input image data, comes from cv::imread(), is a 3-D
  63. * array with layout HWC, BGR format. \param[in] boxes_result The output of
  64. * OCR detection model result will be written to this structure. \return true
  65. * if the prediction is succeeded, otherwise false.
  66. */
  67. virtual bool Predict(const cv::Mat &img,
  68. std::vector<std::array<int, 8>> *boxes_result,
  69. std::vector<std::string> *structure_result);
  70. /** \brief Predict the input image and get OCR detection model result.
  71. *
  72. * \param[in] img The input image data, comes from cv::imread(), is a 3-D
  73. * array with layout HWC, BGR format. \param[in] ocr_result The output of OCR
  74. * detection model result will be written to this structure. \return true if
  75. * the prediction is succeeded, otherwise false.
  76. */
  77. virtual bool Predict(const cv::Mat &img, vision::OCRResult *ocr_result);
  78. /** \brief BatchPredict the input image and get OCR detection model result.
  79. *
  80. * \param[in] images The list input of image data, comes from cv::imread(), is
  81. * a 3-D array with layout HWC, BGR format. \param[in] det_results The output
  82. * of OCR detection model result will be written to this structure. \return
  83. * true if the prediction is succeeded, otherwise false.
  84. */
  85. virtual bool
  86. BatchPredict(const std::vector<cv::Mat> &images,
  87. std::vector<std::vector<std::array<int, 8>>> *det_results,
  88. std::vector<std::vector<std::string>> *structure_results);
  89. /** \brief BatchPredict the input image and get OCR detection model result.
  90. *
  91. * \param[in] images The list input of image data, comes from cv::imread(), is
  92. * a 3-D array with layout HWC, BGR format. \param[in] ocr_results The output
  93. * of OCR detection model result will be written to this structure. \return
  94. * true if the prediction is succeeded, otherwise false.
  95. */
  96. virtual bool BatchPredict(const std::vector<cv::Mat> &images,
  97. std::vector<vision::OCRResult> *ocr_results);
  98. /// Get preprocessor reference of StructureV2TablePreprocessor
  99. virtual StructureV2TablePreprocessor &GetPreprocessor() {
  100. return preprocessor_;
  101. }
  102. /// Get postprocessor reference of StructureV2TablePostprocessor
  103. virtual StructureV2TablePostprocessor &GetPostprocessor() {
  104. return postprocessor_;
  105. }
  106. private:
  107. bool Initialize();
  108. StructureV2TablePreprocessor preprocessor_;
  109. StructureV2TablePostprocessor postprocessor_;
  110. };
  111. } // namespace ocr
  112. } // namespace vision
  113. } // namespace ultra_infer