structurev2_layout.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2023 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/structurev2_layout.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. StructureV2Layout::StructureV2Layout() {}
  21. StructureV2Layout::StructureV2Layout(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. bool StructureV2Layout::Initialize() {
  44. if (!InitRuntime()) {
  45. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  46. return false;
  47. }
  48. return true;
  49. }
  50. std::unique_ptr<StructureV2Layout> StructureV2Layout::Clone() const {
  51. std::unique_ptr<StructureV2Layout> clone_model =
  52. utils::make_unique<StructureV2Layout>(StructureV2Layout(*this));
  53. clone_model->SetRuntime(clone_model->CloneRuntime());
  54. return clone_model;
  55. }
  56. bool StructureV2Layout::Predict(cv::Mat *im, DetectionResult *result) {
  57. return Predict(*im, result);
  58. }
  59. bool StructureV2Layout::Predict(const cv::Mat &im, DetectionResult *result) {
  60. std::vector<DetectionResult> results;
  61. if (!BatchPredict({im}, &results)) {
  62. return false;
  63. }
  64. *result = std::move(results[0]);
  65. return true;
  66. }
  67. bool StructureV2Layout::BatchPredict(const std::vector<cv::Mat> &images,
  68. std::vector<DetectionResult> *results) {
  69. std::vector<FDMat> fd_images = WrapMat(images);
  70. if (!preprocessor_.Run(&fd_images, &reused_input_tensors_)) {
  71. FDERROR << "Failed to preprocess input image." << std::endl;
  72. return false;
  73. }
  74. auto batch_layout_img_info = preprocessor_.GetBatchLayoutImgInfo();
  75. reused_input_tensors_[0].name = InputInfoOfRuntime(0).name;
  76. if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
  77. FDERROR << "Failed to inference by runtime." << std::endl;
  78. return false;
  79. }
  80. if (!postprocessor_.Run(reused_output_tensors_, results,
  81. *batch_layout_img_info)) {
  82. FDERROR << "Failed to postprocess the inference results." << std::endl;
  83. return false;
  84. }
  85. return true;
  86. }
  87. } // namespace ocr
  88. } // namespace vision
  89. } // namespace ultra_infer