rec_preprocessor.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/rec_preprocessor.h"
  15. #include "ultra_infer/function/concat.h"
  16. #include "ultra_infer/utils/perf.h"
  17. #include "ultra_infer/vision/ocr/ppocr/utils/ocr_utils.h"
  18. namespace ultra_infer {
  19. namespace vision {
  20. namespace ocr {
  21. RecognizerPreprocessor::RecognizerPreprocessor() {
  22. resize_op_ = std::make_shared<Resize>(-1, -1);
  23. std::vector<float> value = {127, 127, 127};
  24. pad_op_ = std::make_shared<Pad>(0, 0, 0, 0, value);
  25. std::vector<float> mean = {0.5f, 0.5f, 0.5f};
  26. std::vector<float> std = {0.5f, 0.5f, 0.5f};
  27. normalize_permute_op_ =
  28. std::make_shared<NormalizeAndPermute>(mean, std, true);
  29. normalize_op_ = std::make_shared<Normalize>(mean, std, true);
  30. hwc2chw_op_ = std::make_shared<HWC2CHW>();
  31. cast_op_ = std::make_shared<Cast>("float");
  32. }
  33. void RecognizerPreprocessor::OcrRecognizerResizeImage(
  34. FDMat *mat, float max_wh_ratio, const std::vector<int> &rec_image_shape,
  35. bool static_shape_infer) {
  36. int img_h, img_w;
  37. img_h = rec_image_shape[1];
  38. img_w = rec_image_shape[2];
  39. if (!static_shape_infer) {
  40. img_w = int(img_h * max_wh_ratio);
  41. float ratio = float(mat->Width()) / float(mat->Height());
  42. int resize_w;
  43. if (ceilf(img_h * ratio) > img_w) {
  44. resize_w = img_w;
  45. } else {
  46. resize_w = int(ceilf(img_h * ratio));
  47. }
  48. resize_op_->SetWidthAndHeight(resize_w, img_h);
  49. (*resize_op_)(mat);
  50. pad_op_->SetPaddingSize(0, 0, 0, int(img_w - mat->Width()));
  51. (*pad_op_)(mat);
  52. } else {
  53. if (mat->Width() >= img_w) {
  54. // Reszie W to 320
  55. resize_op_->SetWidthAndHeight(img_w, img_h);
  56. (*resize_op_)(mat);
  57. } else {
  58. resize_op_->SetWidthAndHeight(mat->Width(), img_h);
  59. (*resize_op_)(mat);
  60. // Pad to 320
  61. pad_op_->SetPaddingSize(0, 0, 0, int(img_w - mat->Width()));
  62. (*pad_op_)(mat);
  63. }
  64. }
  65. }
  66. bool RecognizerPreprocessor::Run(std::vector<FDMat> *images,
  67. std::vector<FDTensor> *outputs,
  68. size_t start_index, size_t end_index,
  69. const std::vector<int> &indices) {
  70. if (images->size() == 0 || end_index <= start_index ||
  71. end_index > images->size()) {
  72. FDERROR << "images->size() or index error. Correct is: 0 <= start_index < "
  73. "end_index <= images->size()"
  74. << std::endl;
  75. return false;
  76. }
  77. std::vector<FDMat> mats(end_index - start_index);
  78. for (size_t i = start_index; i < end_index; ++i) {
  79. size_t real_index = i;
  80. if (indices.size() != 0) {
  81. real_index = indices[i];
  82. }
  83. mats[i - start_index] = images->at(real_index);
  84. }
  85. return Run(&mats, outputs);
  86. }
  87. bool RecognizerPreprocessor::Apply(FDMatBatch *image_batch,
  88. std::vector<FDTensor> *outputs) {
  89. int img_h = rec_image_shape_[1];
  90. int img_w = rec_image_shape_[2];
  91. float max_wh_ratio = img_w * 1.0 / img_h;
  92. float ori_wh_ratio;
  93. for (size_t i = 0; i < image_batch->mats->size(); ++i) {
  94. FDMat *mat = &(image_batch->mats->at(i));
  95. ori_wh_ratio = mat->Width() * 1.0 / mat->Height();
  96. max_wh_ratio = std::max(max_wh_ratio, ori_wh_ratio);
  97. }
  98. for (size_t i = 0; i < image_batch->mats->size(); ++i) {
  99. FDMat *mat = &(image_batch->mats->at(i));
  100. OcrRecognizerResizeImage(mat, max_wh_ratio, rec_image_shape_,
  101. static_shape_infer_);
  102. }
  103. if (!disable_normalize_ && !disable_permute_) {
  104. (*normalize_permute_op_)(image_batch);
  105. } else {
  106. if (!disable_normalize_) {
  107. (*normalize_op_)(image_batch);
  108. }
  109. if (!disable_permute_) {
  110. (*hwc2chw_op_)(image_batch);
  111. (*cast_op_)(image_batch);
  112. }
  113. }
  114. // Only have 1 output Tensor.
  115. outputs->resize(1);
  116. // Get the NCHW tensor
  117. FDTensor *tensor = image_batch->Tensor();
  118. (*outputs)[0].SetExternalData(tensor->Shape(), tensor->Dtype(),
  119. tensor->Data(), tensor->device,
  120. tensor->device_id);
  121. return true;
  122. }
  123. } // namespace ocr
  124. } // namespace vision
  125. } // namespace ultra_infer