centerface.cc 2.8 KB

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