base.cc 2.8 KB

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