ppocr_v2.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/classifier.h"
  21. #include "ultra_infer/vision/ocr/ppocr/dbdetector.h"
  22. #include "ultra_infer/vision/ocr/ppocr/recognizer.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 PPOCRv2 is used to load PP-OCRv2 series models provided by PaddleOCR.
  32. */
  33. class ULTRAINFER_DECL PPOCRv2 : public UltraInferModel {
  34. public:
  35. /** \brief Set up the detection model path, classification model path and
  36. * recognition model path respectively.
  37. *
  38. * \param[in] det_model Path of detection model, e.g ./ch_PP-OCRv2_det_infer
  39. * \param[in] cls_model Path of classification model, e.g
  40. * ./ch_ppocr_mobile_v2.0_cls_infer \param[in] rec_model Path of recognition
  41. * model, e.g ./ch_PP-OCRv2_rec_infer
  42. */
  43. PPOCRv2(ultra_infer::vision::ocr::DBDetector *det_model,
  44. ultra_infer::vision::ocr::Classifier *cls_model,
  45. ultra_infer::vision::ocr::Recognizer *rec_model);
  46. /** \brief Classification model is optional, so this function is set up the
  47. * detection model path and recognition model path respectively.
  48. *
  49. * \param[in] det_model Path of detection model, e.g ./ch_PP-OCRv2_det_infer
  50. * \param[in] rec_model Path of recognition model, e.g ./ch_PP-OCRv2_rec_infer
  51. */
  52. PPOCRv2(ultra_infer::vision::ocr::DBDetector *det_model,
  53. ultra_infer::vision::ocr::Recognizer *rec_model);
  54. /** \brief Clone a new PPOCRv2 with less memory usage when multiple instances
  55. * of the same model are created
  56. *
  57. * \return new PPOCRv2* type unique pointer
  58. */
  59. std::unique_ptr<PPOCRv2> Clone() const;
  60. /** \brief Predict the input image and get OCR result.
  61. *
  62. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  63. * with layout HWC, BGR format. \param[in] result The output OCR result will
  64. * be written to this structure. \return true if the prediction succeeded,
  65. * otherwise false.
  66. */
  67. virtual bool Predict(cv::Mat *img, ultra_infer::vision::OCRResult *result);
  68. virtual bool Predict(const cv::Mat &img,
  69. ultra_infer::vision::OCRResult *result);
  70. /** \brief BatchPredict the input image and get OCR result.
  71. *
  72. * \param[in] images The list of input image data, comes from cv::imread(), is
  73. * a 3-D array with layout HWC, BGR format. \param[in] batch_result The output
  74. * list of OCR result will be written to this structure. \return true if the
  75. * prediction succeeded, otherwise false.
  76. */
  77. virtual bool
  78. BatchPredict(const std::vector<cv::Mat> &images,
  79. std::vector<ultra_infer::vision::OCRResult> *batch_result);
  80. bool Initialized() const override;
  81. bool SetClsBatchSize(int cls_batch_size);
  82. int GetClsBatchSize();
  83. bool SetRecBatchSize(int rec_batch_size);
  84. int GetRecBatchSize();
  85. protected:
  86. ultra_infer::vision::ocr::DBDetector *detector_ = nullptr;
  87. ultra_infer::vision::ocr::Classifier *classifier_ = nullptr;
  88. ultra_infer::vision::ocr::Recognizer *recognizer_ = nullptr;
  89. private:
  90. int cls_batch_size_ = 1;
  91. int rec_batch_size_ = 6;
  92. };
  93. namespace application {
  94. namespace ocrsystem {
  95. typedef pipeline::PPOCRv2 PPOCRSystemv2;
  96. } // namespace ocrsystem
  97. } // namespace application
  98. } // namespace pipeline
  99. } // namespace ultra_infer