uvdocwarpper.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/uvdocwarpper.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. UVDocWarpper::UVDocWarpper() {}
  21. UVDocWarpper::UVDocWarpper(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 UVDocWarpper::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<UVDocWarpper> UVDocWarpper::Clone() const {
  52. std::unique_ptr<UVDocWarpper> clone_model =
  53. utils::make_unique<UVDocWarpper>(UVDocWarpper(*this));
  54. clone_model->SetRuntime(clone_model->CloneRuntime());
  55. return clone_model;
  56. }
  57. bool UVDocWarpper::Predict(cv::Mat *im, FDTensor *result) {
  58. return Predict(*im, result);
  59. }
  60. bool UVDocWarpper::Predict(const cv::Mat &img, FDTensor *result) {
  61. std::vector<FDTensor> results;
  62. if (!BatchPredict({img}, &results)) {
  63. return false;
  64. }
  65. *result = std::move(results[0]);
  66. return true;
  67. }
  68. bool UVDocWarpper::BatchPredict(const std::vector<cv::Mat> &images,
  69. std::vector<FDTensor> *results) {
  70. std::vector<FDMat> fd_images = WrapMat(images);
  71. if (!preprocessor_.Run(&fd_images, &reused_input_tensors_)) {
  72. FDERROR << "Failed to preprocess input image." << std::endl;
  73. return false;
  74. }
  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. FDERROR << "Failed to postprocess while using model:" << ModelName() << "."
  82. << std::endl;
  83. return false;
  84. }
  85. return true;
  86. }
  87. } // namespace ocr
  88. } // namespace vision
  89. } // namespace ultra_infer