recognizer.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/rec_postprocessor.h"
  20. #include "ultra_infer/vision/ocr/ppocr/rec_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 Recognizer object is used to load the recognition model provided by
  29. * PaddleOCR.
  30. */
  31. class ULTRAINFER_DECL Recognizer : public UltraInferModel {
  32. public:
  33. Recognizer();
  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_rec_infer/model.pdmodel. \param[in] params_file Path of
  38. * parameter file, e.g ./ch_PP-OCRv3_rec_infer/model.pdiparams, if the model
  39. * format is ONNX, this parameter will be ignored. \param[in] label_path Path
  40. * of label file used by OCR recognition model. e.g ./ppocr_keys_v1.txt
  41. * \param[in] custom_option RuntimeOption for inference, the default will use
  42. * cpu, and choose the backend defined in `valid_cpu_backends`. \param[in]
  43. * model_format Model format of the loaded model, default is Paddle format.
  44. */
  45. Recognizer(const std::string &model_file, const std::string &params_file = "",
  46. const std::string &label_path = "",
  47. const RuntimeOption &custom_option = RuntimeOption(),
  48. const ModelFormat &model_format = ModelFormat::PADDLE);
  49. /// Get model's name
  50. std::string ModelName() const { return "ppocr/ocr_rec"; }
  51. /** \brief Clone a new Recognizer with less memory usage when multiple
  52. * instances of the same model are created
  53. *
  54. * \return new Recognizer* type unique pointer
  55. */
  56. virtual std::unique_ptr<Recognizer> Clone() const;
  57. /** \brief Predict the input image and get OCR recognition model result.
  58. *
  59. * \param[in] img The input image data, comes from cv::imread(), is a 3-D
  60. * array with layout HWC, BGR format. \param[in] text The text result of rec
  61. * model will be written into this parameter. \param[in] rec_score The sccore
  62. * result of rec model will be written into this parameter. \return true if
  63. * the prediction is succeeded, otherwise false.
  64. */
  65. virtual bool Predict(const cv::Mat &img, std::string *text, float *rec_score);
  66. /** \brief Predict the input image and get OCR recognition model result.
  67. *
  68. * \param[in] img The input image data, comes from cv::imread(), is a 3-D
  69. * array with layout HWC, BGR format. \param[in] ocr_result The output of OCR
  70. * recognition model result will be written to this structure. \return true if
  71. * the prediction is succeeded, otherwise false.
  72. */
  73. virtual bool Predict(const cv::Mat &img, vision::OCRResult *ocr_result);
  74. /** \brief BatchPredict the input image and get OCR recognition model result.
  75. *
  76. * \param[in] images The list of input image data, comes from cv::imread(), is
  77. * a 3-D array with layout HWC, BGR format. \param[in] ocr_result The output
  78. * of OCR recognition model result will be written to this structure. \return
  79. * true if the prediction is succeeded, otherwise false.
  80. */
  81. virtual bool BatchPredict(const std::vector<cv::Mat> &images,
  82. vision::OCRResult *ocr_result);
  83. /** \brief BatchPredict the input image and get OCR recognition model result.
  84. *
  85. * \param[in] images The list of input image data, comes from cv::imread(), is
  86. * a 3-D array with layout HWC, BGR format. \param[in] texts The list of text
  87. * results of rec model will be written into this vector. \param[in]
  88. * rec_scores The list of sccore result of rec model will be written into this
  89. * vector. \return true if the prediction is succeeded, otherwise false.
  90. */
  91. virtual bool BatchPredict(const std::vector<cv::Mat> &images,
  92. std::vector<std::string> *texts,
  93. std::vector<float> *rec_scores);
  94. virtual bool BatchPredict(const std::vector<cv::Mat> &images,
  95. std::vector<std::string> *texts,
  96. std::vector<float> *rec_scores, size_t start_index,
  97. size_t end_index, const std::vector<int> &indices);
  98. /// Get preprocessor reference of DBDetectorPreprocessor
  99. virtual RecognizerPreprocessor &GetPreprocessor() { return preprocessor_; }
  100. /// Get postprocessor reference of DBDetectorPostprocessor
  101. virtual RecognizerPostprocessor &GetPostprocessor() { return postprocessor_; }
  102. private:
  103. bool Initialize();
  104. RecognizerPreprocessor preprocessor_;
  105. RecognizerPostprocessor postprocessor_;
  106. };
  107. } // namespace ocr
  108. } // namespace vision
  109. } // namespace ultra_infer