blazeface.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/ppdet/blazeface/blazeface.h"
  15. #include "ultra_infer/utils/perf.h"
  16. #include "ultra_infer/vision/utils/utils.h"
  17. namespace ultra_infer {
  18. namespace vision {
  19. namespace facedet {
  20. BlazeFace::BlazeFace(const std::string &model_file,
  21. const std::string &params_file,
  22. const std::string &config_file,
  23. const RuntimeOption &custom_option,
  24. const ModelFormat &model_format)
  25. : preprocessor_(config_file) {
  26. valid_cpu_backends = {Backend::OPENVINO, Backend::PDINFER, Backend::LITE};
  27. valid_gpu_backends = {Backend::OPENVINO, Backend::LITE, Backend::PDINFER};
  28. runtime_option = custom_option;
  29. runtime_option.model_format = model_format;
  30. runtime_option.model_file = model_file;
  31. runtime_option.params_file = params_file;
  32. initialized = Initialize();
  33. }
  34. bool BlazeFace::Initialize() {
  35. if (!InitRuntime()) {
  36. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  37. return false;
  38. }
  39. return true;
  40. }
  41. bool BlazeFace::Predict(const cv::Mat &im, FaceDetectionResult *result) {
  42. std::vector<FaceDetectionResult> results;
  43. if (!this->BatchPredict({im}, &results)) {
  44. return false;
  45. }
  46. *result = std::move(results[0]);
  47. return true;
  48. }
  49. bool BlazeFace::BatchPredict(const std::vector<cv::Mat> &images,
  50. std::vector<FaceDetectionResult> *results) {
  51. std::vector<FDMat> fd_images = WrapMat(images);
  52. FDASSERT(images.size() == 1, "Only support batch = 1 now.");
  53. std::vector<std::map<std::string, std::array<float, 2>>> ims_info;
  54. if (!preprocessor_.Run(&fd_images, &reused_input_tensors_, &ims_info)) {
  55. FDERROR << "Failed to preprocess the input image." << std::endl;
  56. return false;
  57. }
  58. reused_input_tensors_[0].name = "image";
  59. reused_input_tensors_[1].name = "scale_factor";
  60. reused_input_tensors_[2].name = "im_shape";
  61. // Some models don't need scale_factor and im_shape as input
  62. while (reused_input_tensors_.size() != NumInputsOfRuntime()) {
  63. reused_input_tensors_.pop_back();
  64. }
  65. if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
  66. FDERROR << "Failed to inference by runtime." << std::endl;
  67. return false;
  68. }
  69. if (!postprocessor_.Run(reused_output_tensors_, results, ims_info)) {
  70. FDERROR << "Failed to postprocess the inference results by runtime."
  71. << std::endl;
  72. return false;
  73. }
  74. return true;
  75. }
  76. } // namespace facedet
  77. } // namespace vision
  78. } // namespace ultra_infer