det_preprocessor.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/det_preprocessor.h"
  15. #include "ultra_infer/vision/ocr/ppocr/utils/ocr_utils.h"
  16. namespace ultra_infer {
  17. namespace vision {
  18. namespace ocr {
  19. std::array<int, 4>
  20. DBDetectorPreprocessor::OcrDetectorGetInfo(FDMat *img, int max_size_len) {
  21. int w = img->Width();
  22. int h = img->Height();
  23. if (static_shape_infer_) {
  24. return {w, h, det_image_shape_[2], det_image_shape_[1]};
  25. }
  26. float ratio = 1.f;
  27. int max_wh = w >= h ? w : h;
  28. if (max_wh > max_size_len) {
  29. if (h > w) {
  30. ratio = float(max_size_len) / float(h);
  31. } else {
  32. ratio = float(max_size_len) / float(w);
  33. }
  34. }
  35. int resize_h = int(float(h) * ratio);
  36. int resize_w = int(float(w) * ratio);
  37. resize_h = std::max(int(std::round(float(resize_h) / 32) * 32), 32);
  38. resize_w = std::max(int(std::round(float(resize_w) / 32) * 32), 32);
  39. return {w, h, resize_w, resize_h};
  40. /*
  41. *ratio_h = float(resize_h) / float(h);
  42. *ratio_w = float(resize_w) / float(w);
  43. */
  44. }
  45. DBDetectorPreprocessor::DBDetectorPreprocessor() {
  46. resize_op_ = std::make_shared<Resize>(-1, -1);
  47. std::vector<float> value = {0, 0, 0};
  48. pad_op_ = std::make_shared<Pad>(0, 0, 0, 0, value);
  49. normalize_permute_op_ = std::make_shared<NormalizeAndPermute>(
  50. std::vector<float>({0.485f, 0.456f, 0.406f}),
  51. std::vector<float>({0.229f, 0.224f, 0.225f}), true);
  52. }
  53. bool DBDetectorPreprocessor::ResizeImage(FDMat *img, int resize_w, int resize_h,
  54. int max_resize_w, int max_resize_h) {
  55. resize_op_->SetWidthAndHeight(resize_w, resize_h);
  56. (*resize_op_)(img);
  57. pad_op_->SetPaddingSize(0, max_resize_h - resize_h, 0,
  58. max_resize_w - resize_w);
  59. (*pad_op_)(img);
  60. return true;
  61. }
  62. bool DBDetectorPreprocessor::Apply(FDMatBatch *image_batch,
  63. std::vector<FDTensor> *outputs) {
  64. int max_resize_w = 0;
  65. int max_resize_h = 0;
  66. batch_det_img_info_.clear();
  67. batch_det_img_info_.resize(image_batch->mats->size());
  68. for (size_t i = 0; i < image_batch->mats->size(); ++i) {
  69. FDMat *mat = &(image_batch->mats->at(i));
  70. batch_det_img_info_[i] = OcrDetectorGetInfo(mat, max_side_len_);
  71. max_resize_w = std::max(max_resize_w, batch_det_img_info_[i][2]);
  72. max_resize_h = std::max(max_resize_h, batch_det_img_info_[i][3]);
  73. }
  74. for (size_t i = 0; i < image_batch->mats->size(); ++i) {
  75. FDMat *mat = &(image_batch->mats->at(i));
  76. ResizeImage(mat, batch_det_img_info_[i][2], batch_det_img_info_[i][3],
  77. max_resize_w, max_resize_h);
  78. }
  79. if (!disable_normalize_ && !disable_permute_) {
  80. (*normalize_permute_op_)(image_batch);
  81. }
  82. outputs->resize(1);
  83. FDTensor *tensor = image_batch->Tensor();
  84. (*outputs)[0].SetExternalData(tensor->Shape(), tensor->Dtype(),
  85. tensor->Data(), tensor->device,
  86. tensor->device_id);
  87. return true;
  88. }
  89. } // namespace ocr
  90. } // namespace vision
  91. } // namespace ultra_infer