recognizer.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "ultra_infer/vision/ocr/ppocr/recognizer.h"
  15. #include "ultra_infer/utils/perf.h"
  16. #include "ultra_infer/vision/ocr/ppocr/utils/ocr_utils.h"
  17. namespace ultra_infer {
  18. namespace vision {
  19. namespace ocr {
  20. Recognizer::Recognizer() {}
  21. Recognizer::Recognizer(const std::string &model_file,
  22. const std::string &params_file,
  23. const std::string &label_path,
  24. const RuntimeOption &custom_option,
  25. const ModelFormat &model_format)
  26. : postprocessor_(label_path) {
  27. if (model_format == ModelFormat::ONNX) {
  28. valid_cpu_backends = {Backend::ORT, Backend::OPENVINO};
  29. valid_gpu_backends = {Backend::ORT, Backend::TRT};
  30. } else {
  31. valid_cpu_backends = {Backend::PDINFER, Backend::ORT, Backend::OPENVINO,
  32. Backend::LITE};
  33. valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
  34. valid_kunlunxin_backends = {Backend::LITE};
  35. valid_ascend_backends = {Backend::LITE};
  36. valid_sophgonpu_backends = {Backend::SOPHGOTPU};
  37. valid_rknpu_backends = {Backend::RKNPU2};
  38. }
  39. runtime_option = custom_option;
  40. runtime_option.model_format = model_format;
  41. runtime_option.model_file = model_file;
  42. runtime_option.params_file = params_file;
  43. initialized = Initialize();
  44. }
  45. // Init
  46. bool Recognizer::Initialize() {
  47. if (!InitRuntime()) {
  48. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  49. return false;
  50. }
  51. return true;
  52. }
  53. std::unique_ptr<Recognizer> Recognizer::Clone() const {
  54. std::unique_ptr<Recognizer> clone_model =
  55. utils::make_unique<Recognizer>(Recognizer(*this));
  56. clone_model->SetRuntime(clone_model->CloneRuntime());
  57. return clone_model;
  58. }
  59. bool Recognizer::Predict(const cv::Mat &img, std::string *text,
  60. float *rec_score) {
  61. std::vector<std::string> texts(1);
  62. std::vector<float> rec_scores(1);
  63. bool success = BatchPredict({img}, &texts, &rec_scores);
  64. if (!success) {
  65. return success;
  66. }
  67. *text = std::move(texts[0]);
  68. *rec_score = rec_scores[0];
  69. return true;
  70. }
  71. bool Recognizer::Predict(const cv::Mat &img, vision::OCRResult *ocr_result) {
  72. ocr_result->text.resize(1);
  73. ocr_result->rec_scores.resize(1);
  74. if (!Predict(img, &(ocr_result->text[0]), &(ocr_result->rec_scores[0]))) {
  75. return false;
  76. }
  77. return true;
  78. }
  79. bool Recognizer::BatchPredict(const std::vector<cv::Mat> &images,
  80. std::vector<std::string> *texts,
  81. std::vector<float> *rec_scores) {
  82. return BatchPredict(images, texts, rec_scores, 0, images.size(), {});
  83. }
  84. bool Recognizer::BatchPredict(const std::vector<cv::Mat> &images,
  85. vision::OCRResult *ocr_result) {
  86. return BatchPredict(images, &(ocr_result->text), &(ocr_result->rec_scores));
  87. }
  88. bool Recognizer::BatchPredict(const std::vector<cv::Mat> &images,
  89. std::vector<std::string> *texts,
  90. std::vector<float> *rec_scores,
  91. size_t start_index, size_t end_index,
  92. const std::vector<int> &indices) {
  93. size_t total_size = images.size();
  94. if (indices.size() != 0 && indices.size() != total_size) {
  95. FDERROR << "indices.size() should be 0 or images.size()." << std::endl;
  96. return false;
  97. }
  98. std::vector<FDMat> fd_images = WrapMat(images);
  99. if (!preprocessor_.Run(&fd_images, &reused_input_tensors_, start_index,
  100. end_index, indices)) {
  101. FDERROR << "Failed to preprocess the input image." << std::endl;
  102. return false;
  103. }
  104. reused_input_tensors_[0].name = InputInfoOfRuntime(0).name;
  105. if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
  106. FDERROR << "Failed to inference by runtime." << std::endl;
  107. return false;
  108. }
  109. if (!postprocessor_.Run(reused_output_tensors_, texts, rec_scores,
  110. start_index, total_size, indices)) {
  111. FDERROR << "Failed to postprocess the inference cls_results by runtime."
  112. << std::endl;
  113. return false;
  114. }
  115. return true;
  116. }
  117. } // namespace ocr
  118. } // namespace vision
  119. } // namespace ultra_infer