ppstructurev2_table.h 3.8 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 <vector>
  16. #include "ultra_infer/ultra_infer_model.h"
  17. #include "ultra_infer/vision/common/processors/transform.h"
  18. #include "ultra_infer/vision/common/result.h"
  19. #include "ultra_infer/utils/unique_ptr.h"
  20. #include "ultra_infer/vision/ocr/ppocr/dbdetector.h"
  21. #include "ultra_infer/vision/ocr/ppocr/recognizer.h"
  22. #include "ultra_infer/vision/ocr/ppocr/structurev2_table.h"
  23. #include "ultra_infer/vision/ocr/ppocr/utils/ocr_postprocess_op.h"
  24. namespace ultra_infer {
  25. /** \brief This pipeline can launch detection model, classification model and
  26. * recognition model sequentially. All OCR pipeline APIs are defined inside this
  27. * namespace.
  28. *
  29. */
  30. namespace pipeline {
  31. /*! @brief PPStructureV2Table is used to load PP-OCRv2 series models provided by
  32. * PaddleOCR.
  33. */
  34. class ULTRAINFER_DECL PPStructureV2Table : public UltraInferModel {
  35. public:
  36. /** \brief Set up the detection model path, recognition model path and table
  37. * model path respectively.
  38. *
  39. * \param[in] det_model Path of detection model, e.g ./ch_PP-OCRv2_det_infer
  40. * \param[in] rec_model Path of recognition model, e.g ./ch_PP-OCRv2_rec_infer
  41. * \param[in] table_model Path of table recognition model, e.g
  42. * ./en_ppstructure_mobile_v2.0_SLANet_infer
  43. */
  44. PPStructureV2Table(ultra_infer::vision::ocr::DBDetector *det_model,
  45. ultra_infer::vision::ocr::Recognizer *rec_model,
  46. ultra_infer::vision::ocr::StructureV2Table *table_model);
  47. /** \brief Clone a new PPStructureV2Table with less memory usage when multiple
  48. * instances of the same model are created
  49. *
  50. * \return new PPStructureV2Table* type unique pointer
  51. */
  52. std::unique_ptr<PPStructureV2Table> Clone() const;
  53. /** \brief Predict the input image and get OCR result.
  54. *
  55. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  56. * with layout HWC, BGR format. \param[in] result The output OCR result will
  57. * be written to this structure. \return true if the prediction succeeded,
  58. * otherwise false.
  59. */
  60. virtual bool Predict(cv::Mat *img, ultra_infer::vision::OCRResult *result);
  61. virtual bool Predict(const cv::Mat &img,
  62. ultra_infer::vision::OCRResult *result);
  63. /** \brief BatchPredict the input image and get OCR result.
  64. *
  65. * \param[in] images The list of input image data, comes from cv::imread(), is
  66. * a 3-D array with layout HWC, BGR format. \param[in] batch_result The output
  67. * list of OCR result will be written to this structure. \return true if the
  68. * prediction succeeded, otherwise false.
  69. */
  70. virtual bool
  71. BatchPredict(const std::vector<cv::Mat> &images,
  72. std::vector<ultra_infer::vision::OCRResult> *batch_result);
  73. bool Initialized() const override;
  74. bool SetRecBatchSize(int rec_batch_size);
  75. int GetRecBatchSize();
  76. protected:
  77. ultra_infer::vision::ocr::DBDetector *detector_ = nullptr;
  78. ultra_infer::vision::ocr::Recognizer *recognizer_ = nullptr;
  79. ultra_infer::vision::ocr::StructureV2Table *table_ = nullptr;
  80. private:
  81. int rec_batch_size_ = 6;
  82. };
  83. namespace application {
  84. namespace ocrsystem {
  85. typedef pipeline::PPStructureV2Table PPStructureV2TableSystem;
  86. } // namespace ocrsystem
  87. } // namespace application
  88. } // namespace pipeline
  89. } // namespace ultra_infer