cls_preprocessor.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/cls_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. ClassifierPreprocessor::ClassifierPreprocessor() {
  22. resize_op_ = std::make_shared<Resize>(-1, -1);
  23. std::vector<float> value = {0, 0, 0};
  24. pad_op_ = std::make_shared<Pad>(0, 0, 0, 0, value);
  25. normalize_op_ =
  26. std::make_shared<Normalize>(std::vector<float>({0.5f, 0.5f, 0.5f}),
  27. std::vector<float>({0.5f, 0.5f, 0.5f}), true);
  28. hwc2chw_op_ = std::make_shared<HWC2CHW>();
  29. }
  30. void ClassifierPreprocessor::OcrClassifierResizeImage(
  31. FDMat *mat, const std::vector<int> &cls_image_shape) {
  32. int img_c = cls_image_shape[0];
  33. int img_h = cls_image_shape[1];
  34. int img_w = cls_image_shape[2];
  35. float ratio = float(mat->Width()) / float(mat->Height());
  36. int resize_w;
  37. if (ceilf(img_h * ratio) > img_w)
  38. resize_w = img_w;
  39. else
  40. resize_w = int(ceilf(img_h * ratio));
  41. resize_op_->SetWidthAndHeight(resize_w, img_h);
  42. (*resize_op_)(mat);
  43. }
  44. bool ClassifierPreprocessor::Run(std::vector<FDMat> *images,
  45. std::vector<FDTensor> *outputs,
  46. size_t start_index, size_t end_index) {
  47. if (images->size() == 0 || start_index < 0 || end_index <= start_index ||
  48. end_index > images->size()) {
  49. FDERROR << "images->size() or index error. Correct is: 0 <= start_index < "
  50. "end_index <= images->size()"
  51. << std::endl;
  52. return false;
  53. }
  54. std::vector<FDMat> mats(end_index - start_index);
  55. for (size_t i = start_index; i < end_index; ++i) {
  56. mats[i - start_index] = images->at(i);
  57. }
  58. return Run(&mats, outputs);
  59. }
  60. bool ClassifierPreprocessor::Apply(FDMatBatch *image_batch,
  61. std::vector<FDTensor> *outputs) {
  62. for (size_t i = 0; i < image_batch->mats->size(); ++i) {
  63. FDMat *mat = &(image_batch->mats->at(i));
  64. OcrClassifierResizeImage(mat, cls_image_shape_);
  65. if (!disable_normalize_) {
  66. (*normalize_op_)(mat);
  67. }
  68. std::vector<float> value = {0, 0, 0};
  69. if (mat->Width() < cls_image_shape_[2]) {
  70. pad_op_->SetPaddingSize(0, 0, 0, cls_image_shape_[2] - mat->Width());
  71. (*pad_op_)(mat);
  72. }
  73. if (!disable_permute_) {
  74. (*hwc2chw_op_)(mat);
  75. }
  76. }
  77. // Only have 1 output tensor.
  78. outputs->resize(1);
  79. // Get the NCHW tensor
  80. FDTensor *tensor = image_batch->Tensor();
  81. (*outputs)[0].SetExternalData(tensor->Shape(), tensor->Dtype(),
  82. tensor->Data(), tensor->device,
  83. tensor->device_id);
  84. return true;
  85. }
  86. } // namespace ocr
  87. } // namespace vision
  88. } // namespace ultra_infer