dbdetector.cc 4.0 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. #include "ultra_infer/vision/ocr/ppocr/dbdetector.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. DBDetector::DBDetector() {}
  21. DBDetector::DBDetector(const std::string &model_file,
  22. const std::string &params_file,
  23. const RuntimeOption &custom_option,
  24. const ModelFormat &model_format) {
  25. if (model_format == ModelFormat::ONNX) {
  26. valid_cpu_backends = {Backend::ORT, Backend::OPENVINO};
  27. valid_gpu_backends = {Backend::ORT, Backend::TRT};
  28. } else {
  29. valid_cpu_backends = {Backend::PDINFER, Backend::ORT, Backend::OPENVINO,
  30. Backend::LITE};
  31. valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
  32. valid_kunlunxin_backends = {Backend::LITE};
  33. valid_ascend_backends = {Backend::LITE};
  34. valid_sophgonpu_backends = {Backend::SOPHGOTPU};
  35. valid_rknpu_backends = {Backend::RKNPU2};
  36. }
  37. runtime_option = custom_option;
  38. runtime_option.model_format = model_format;
  39. runtime_option.model_file = model_file;
  40. runtime_option.params_file = params_file;
  41. initialized = Initialize();
  42. }
  43. // Init
  44. bool DBDetector::Initialize() {
  45. if (!InitRuntime()) {
  46. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  47. return false;
  48. }
  49. return true;
  50. }
  51. std::unique_ptr<DBDetector> DBDetector::Clone() const {
  52. std::unique_ptr<DBDetector> clone_model =
  53. utils::make_unique<DBDetector>(DBDetector(*this));
  54. clone_model->SetRuntime(clone_model->CloneRuntime());
  55. return clone_model;
  56. }
  57. bool DBDetector::Predict(const cv::Mat &img,
  58. std::vector<std::array<int, 8>> *boxes_result) {
  59. std::vector<std::vector<std::array<int, 8>>> det_results;
  60. if (!BatchPredict({img}, &det_results)) {
  61. return false;
  62. }
  63. *boxes_result = std::move(det_results[0]);
  64. return true;
  65. }
  66. bool DBDetector::Predict(const cv::Mat &img, vision::OCRResult *ocr_result) {
  67. if (!Predict(img, &(ocr_result->boxes))) {
  68. return false;
  69. }
  70. return true;
  71. }
  72. bool DBDetector::BatchPredict(const std::vector<cv::Mat> &images,
  73. std::vector<vision::OCRResult> *ocr_results) {
  74. std::vector<std::vector<std::array<int, 8>>> det_results;
  75. if (!BatchPredict(images, &det_results)) {
  76. return false;
  77. }
  78. ocr_results->resize(det_results.size());
  79. for (int i = 0; i < det_results.size(); i++) {
  80. (*ocr_results)[i].boxes = std::move(det_results[i]);
  81. }
  82. return true;
  83. }
  84. bool DBDetector::BatchPredict(
  85. const std::vector<cv::Mat> &images,
  86. std::vector<std::vector<std::array<int, 8>>> *det_results) {
  87. std::vector<FDMat> fd_images = WrapMat(images);
  88. if (!preprocessor_.Run(&fd_images, &reused_input_tensors_)) {
  89. FDERROR << "Failed to preprocess input image." << std::endl;
  90. return false;
  91. }
  92. auto batch_det_img_info = preprocessor_.GetBatchImgInfo();
  93. reused_input_tensors_[0].name = InputInfoOfRuntime(0).name;
  94. if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
  95. FDERROR << "Failed to inference by runtime." << std::endl;
  96. return false;
  97. }
  98. if (!postprocessor_.Run(reused_output_tensors_, det_results,
  99. *batch_det_img_info)) {
  100. FDERROR << "Failed to postprocess the inference cls_results by runtime."
  101. << std::endl;
  102. return false;
  103. }
  104. return true;
  105. }
  106. } // namespace ocr
  107. } // namespace vision
  108. } // namespace ultra_infer