preprocessor.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/facedet/contrib/centerface/preprocessor.h"
  15. #include "ultra_infer/function/concat.h"
  16. #include "ultra_infer/vision/common/processors/mat.h"
  17. namespace ultra_infer {
  18. namespace vision {
  19. namespace facedet {
  20. CenterFacePreprocessor::CenterFacePreprocessor() { size_ = {640, 640}; }
  21. bool CenterFacePreprocessor::Run(
  22. std::vector<FDMat> *images, std::vector<FDTensor> *outputs,
  23. std::vector<std::map<std::string, std::array<float, 2>>> *ims_info) {
  24. if (images->size() == 0) {
  25. FDERROR << "The size of input images should be greater than 0."
  26. << std::endl;
  27. return false;
  28. }
  29. ims_info->resize(images->size());
  30. outputs->resize(1);
  31. std::vector<FDTensor> tensors(images->size());
  32. for (size_t i = 0; i < images->size(); i++) {
  33. if (!Preprocess(&(*images)[i], &tensors[i], &(*ims_info)[i])) {
  34. FDERROR << "Failed to preprocess input image." << std::endl;
  35. return false;
  36. }
  37. }
  38. if (tensors.size() == 1) {
  39. (*outputs)[0] = std::move(tensors[0]);
  40. } else {
  41. function::Concat(tensors, &((*outputs)[0]), 0);
  42. }
  43. return true;
  44. }
  45. bool CenterFacePreprocessor::Preprocess(
  46. FDMat *mat, FDTensor *output,
  47. std::map<std::string, std::array<float, 2>> *im_info) {
  48. // Record the shape of image and the shape of preprocessed image
  49. (*im_info)["input_shape"] = {static_cast<float>(mat->Height()),
  50. static_cast<float>(mat->Width())};
  51. // centerface's preprocess steps
  52. // 1. Resize
  53. // 2. ConvertAndPermute
  54. Resize::Run(mat, size_[0], size_[1]);
  55. std::vector<float> alpha = {1.0f, 1.0f, 1.0f};
  56. std::vector<float> beta = {0.0f, 0.0f, 0.0f};
  57. ConvertAndPermute::Run(mat, alpha, beta, true);
  58. // Record output shape of preprocessed image
  59. (*im_info)["output_shape"] = {static_cast<float>(mat->Height()),
  60. static_cast<float>(mat->Width())};
  61. mat->ShareWithTensor(output);
  62. output->ExpandDim(0);
  63. return true;
  64. }
  65. } // namespace facedet
  66. } // namespace vision
  67. } // namespace ultra_infer