insightface_pybind.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/pybind/main.h"
  15. namespace ultra_infer {
  16. void BindInsightFace(pybind11::module &m) {
  17. pybind11::class_<vision::faceid::InsightFaceRecognitionPreprocessor>(
  18. m, "InsightFaceRecognitionPreprocessor")
  19. .def(pybind11::init())
  20. .def("run",
  21. [](vision::faceid::InsightFaceRecognitionPreprocessor &self,
  22. std::vector<pybind11::array> &im_list) {
  23. std::vector<vision::FDMat> images;
  24. for (size_t i = 0; i < im_list.size(); ++i) {
  25. images.push_back(vision::WrapMat(PyArrayToCvMat(im_list[i])));
  26. }
  27. std::vector<FDTensor> outputs;
  28. if (!self.Run(&images, &outputs)) {
  29. throw std::runtime_error(
  30. "Failed to preprocess the input data in "
  31. "InsightFaceRecognitionPreprocessor.");
  32. }
  33. for (size_t i = 0; i < outputs.size(); ++i) {
  34. outputs[i].StopSharing();
  35. }
  36. return outputs;
  37. })
  38. .def(
  39. "disable_normalize",
  40. &vision::faceid::InsightFaceRecognitionPreprocessor::DisableNormalize)
  41. .def("disable_permute",
  42. &vision::faceid::InsightFaceRecognitionPreprocessor::DisablePermute)
  43. .def_property(
  44. "alpha",
  45. &vision::faceid::InsightFaceRecognitionPreprocessor::GetAlpha,
  46. &vision::faceid::InsightFaceRecognitionPreprocessor::SetAlpha)
  47. .def_property(
  48. "beta", &vision::faceid::InsightFaceRecognitionPreprocessor::GetBeta,
  49. &vision::faceid::InsightFaceRecognitionPreprocessor::SetBeta)
  50. .def_property(
  51. "size", &vision::faceid::InsightFaceRecognitionPreprocessor::GetSize,
  52. &vision::faceid::InsightFaceRecognitionPreprocessor::SetSize);
  53. pybind11::class_<vision::faceid::InsightFaceRecognitionPostprocessor>(
  54. m, "InsightFaceRecognitionPostprocessor")
  55. .def(pybind11::init())
  56. .def("run",
  57. [](vision::faceid::InsightFaceRecognitionPostprocessor &self,
  58. std::vector<FDTensor> &inputs) {
  59. std::vector<vision::FaceRecognitionResult> results;
  60. if (!self.Run(inputs, &results)) {
  61. throw std::runtime_error(
  62. "Failed to postprocess the runtime result in "
  63. "InsightFaceRecognitionPostprocessor.");
  64. }
  65. return results;
  66. })
  67. .def("run",
  68. [](vision::faceid::InsightFaceRecognitionPostprocessor &self,
  69. std::vector<pybind11::array> &input_array) {
  70. std::vector<vision::FaceRecognitionResult> results;
  71. std::vector<FDTensor> inputs;
  72. PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
  73. if (!self.Run(inputs, &results)) {
  74. throw std::runtime_error(
  75. "Failed to postprocess the runtime result in "
  76. "InsightFaceRecognitionPostprocessor.");
  77. }
  78. return results;
  79. })
  80. .def_property(
  81. "l2_normalize",
  82. &vision::faceid::InsightFaceRecognitionPostprocessor::GetL2Normalize,
  83. &vision::faceid::InsightFaceRecognitionPostprocessor::SetL2Normalize);
  84. pybind11::class_<vision::faceid::InsightFaceRecognitionBase, UltraInferModel>(
  85. m, "InsightFaceRecognitionBase")
  86. .def(pybind11::init<std::string, std::string, RuntimeOption,
  87. ModelFormat>())
  88. .def("predict",
  89. [](vision::faceid::InsightFaceRecognitionBase &self,
  90. pybind11::array &data) {
  91. cv::Mat im = PyArrayToCvMat(data);
  92. vision::FaceRecognitionResult result;
  93. self.Predict(im, &result);
  94. return result;
  95. })
  96. .def("batch_predict",
  97. [](vision::faceid::InsightFaceRecognitionBase &self,
  98. std::vector<pybind11::array> &data) {
  99. std::vector<cv::Mat> images;
  100. for (size_t i = 0; i < data.size(); ++i) {
  101. images.push_back(PyArrayToCvMat(data[i]));
  102. }
  103. std::vector<vision::FaceRecognitionResult> results;
  104. self.BatchPredict(images, &results);
  105. return results;
  106. })
  107. .def_property_readonly(
  108. "preprocessor",
  109. &vision::faceid::InsightFaceRecognitionBase::GetPreprocessor)
  110. .def_property_readonly(
  111. "postprocessor",
  112. &vision::faceid::InsightFaceRecognitionBase::GetPostprocessor);
  113. pybind11::class_<vision::faceid::ArcFace,
  114. vision::faceid::InsightFaceRecognitionBase>(m, "ArcFace")
  115. .def(pybind11::init<std::string, std::string, RuntimeOption,
  116. ModelFormat>());
  117. pybind11::class_<vision::faceid::CosFace,
  118. vision::faceid::InsightFaceRecognitionBase>(m, "CosFace")
  119. .def(pybind11::init<std::string, std::string, RuntimeOption,
  120. ModelFormat>());
  121. pybind11::class_<vision::faceid::PartialFC,
  122. vision::faceid::InsightFaceRecognitionBase>(m, "PartialFC")
  123. .def(pybind11::init<std::string, std::string, RuntimeOption,
  124. ModelFormat>());
  125. pybind11::class_<vision::faceid::VPL,
  126. vision::faceid::InsightFaceRecognitionBase>(m, "VPL")
  127. .def(pybind11::init<std::string, std::string, RuntimeOption,
  128. ModelFormat>());
  129. }
  130. } // namespace ultra_infer