dbcurvedetector.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/dbcurvedetector.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. DBCURVEDetector::DBCURVEDetector() {}
  21. DBCURVEDetector::DBCURVEDetector(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 DBCURVEDetector::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<DBCURVEDetector> DBCURVEDetector::Clone() const {
  52. std::unique_ptr<DBCURVEDetector> clone_model =
  53. utils::make_unique<DBCURVEDetector>(DBCURVEDetector(*this));
  54. clone_model->SetRuntime(clone_model->CloneRuntime());
  55. return clone_model;
  56. }
  57. bool DBCURVEDetector::Predict(const cv::Mat &img,
  58. std::vector<std::vector<int>> *boxes_result) {
  59. std::vector<std::vector<std::vector<int>>> 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 DBCURVEDetector::Predict(const cv::Mat &img,
  67. vision::OCRCURVEResult *ocr_result) {
  68. if (!Predict(img, &(ocr_result->boxes))) {
  69. return false;
  70. }
  71. return true;
  72. }
  73. bool DBCURVEDetector::BatchPredict(
  74. const std::vector<cv::Mat> &images,
  75. std::vector<vision::OCRCURVEResult> *ocr_results) {
  76. std::vector<std::vector<std::vector<int>>> det_results;
  77. if (!BatchPredict(images, &det_results)) {
  78. return false;
  79. }
  80. ocr_results->resize(det_results.size());
  81. for (int i = 0; i < det_results.size(); i++) {
  82. (*ocr_results)[i].boxes = std::move(det_results[i]);
  83. }
  84. return true;
  85. }
  86. bool DBCURVEDetector::BatchPredict(
  87. const std::vector<cv::Mat> &images,
  88. std::vector<std::vector<std::vector<int>>> *det_results) {
  89. std::vector<FDMat> fd_images = WrapMat(images);
  90. if (!preprocessor_.Run(&fd_images, &reused_input_tensors_)) {
  91. FDERROR << "Failed to preprocess input image." << std::endl;
  92. return false;
  93. }
  94. auto batch_det_img_info = preprocessor_.GetBatchImgInfo();
  95. reused_input_tensors_[0].name = InputInfoOfRuntime(0).name;
  96. if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
  97. FDERROR << "Failed to inference by runtime." << std::endl;
  98. return false;
  99. }
  100. if (!postprocessor_.Run(reused_output_tensors_, det_results,
  101. *batch_det_img_info)) {
  102. FDERROR << "Failed to postprocess the inference cls_results by runtime."
  103. << std::endl;
  104. return false;
  105. }
  106. return true;
  107. }
  108. } // namespace ocr
  109. } // namespace vision
  110. } // namespace ultra_infer