ppocr_v3.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/vision/ocr/ppocr/ppocr_v2.h"
  16. namespace ultra_infer {
  17. /** \brief This pipeline can launch detection model, classification model and
  18. * recognition model sequentially. All OCR pipeline APIs are defined inside this
  19. * namespace.
  20. *
  21. */
  22. namespace pipeline {
  23. /*! @brief PPOCRv3 is used to load PP-OCRv3 series models provided by PaddleOCR.
  24. */
  25. class ULTRAINFER_DECL PPOCRv3 : public PPOCRv2 {
  26. public:
  27. /** \brief Set up the detection model path, classification model path and
  28. * recognition model path respectively.
  29. *
  30. * \param[in] det_model Path of detection model, e.g ./ch_PP-OCRv3_det_infer
  31. * \param[in] cls_model Path of classification model, e.g
  32. * ./ch_ppocr_mobile_v2.0_cls_infer \param[in] rec_model Path of recognition
  33. * model, e.g ./ch_PP-OCRv3_rec_infer
  34. */
  35. PPOCRv3(ultra_infer::vision::ocr::DBDetector *det_model,
  36. ultra_infer::vision::ocr::Classifier *cls_model,
  37. ultra_infer::vision::ocr::Recognizer *rec_model)
  38. : PPOCRv2(det_model, cls_model, rec_model) {
  39. // The only difference between v2 and v3
  40. auto preprocess_shape = recognizer_->GetPreprocessor().GetRecImageShape();
  41. preprocess_shape[1] = 48;
  42. recognizer_->GetPreprocessor().SetRecImageShape(preprocess_shape);
  43. }
  44. /** \brief Classification model is optional, so this function is set up the
  45. * detection model path and recognition model path respectively.
  46. *
  47. * \param[in] det_model Path of detection model, e.g ./ch_PP-OCRv3_det_infer
  48. * \param[in] rec_model Path of recognition model, e.g ./ch_PP-OCRv3_rec_infer
  49. */
  50. PPOCRv3(ultra_infer::vision::ocr::DBDetector *det_model,
  51. ultra_infer::vision::ocr::Recognizer *rec_model)
  52. : PPOCRv2(det_model, rec_model) {
  53. // The only difference between v2 and v3
  54. auto preprocess_shape = recognizer_->GetPreprocessor().GetRecImageShape();
  55. preprocess_shape[1] = 48;
  56. recognizer_->GetPreprocessor().SetRecImageShape(preprocess_shape);
  57. }
  58. /** \brief Clone a new PPOCRv3 with less memory usage when multiple instances
  59. * of the same model are created
  60. *
  61. * \return new PPOCRv3* type unique pointer
  62. */
  63. std::unique_ptr<PPOCRv3> Clone() const {
  64. std::unique_ptr<PPOCRv3> clone_model =
  65. utils::make_unique<PPOCRv3>(PPOCRv3(*this));
  66. clone_model->detector_ = detector_->Clone().release();
  67. if (classifier_ != nullptr) {
  68. clone_model->classifier_ = classifier_->Clone().release();
  69. }
  70. clone_model->recognizer_ = recognizer_->Clone().release();
  71. return clone_model;
  72. }
  73. };
  74. } // namespace pipeline
  75. namespace application {
  76. namespace ocrsystem {
  77. typedef pipeline::PPOCRv3 PPOCRSystemv3;
  78. } // namespace ocrsystem
  79. } // namespace application
  80. } // namespace ultra_infer