blazeface_pybind.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 BindBlazeFace(pybind11::module &m) {
  17. pybind11::class_<vision::facedet::BlazeFacePreprocessor>(
  18. m, "BlazeFacePreprocessor")
  19. .def(pybind11::init<>())
  20. .def("run", [](vision::facedet::BlazeFacePreprocessor &self,
  21. std::vector<pybind11::array> &im_list) {
  22. std::vector<vision::FDMat> images;
  23. for (size_t i = 0; i < im_list.size(); ++i) {
  24. images.push_back(vision::WrapMat(PyArrayToCvMat(im_list[i])));
  25. }
  26. std::vector<FDTensor> outputs;
  27. std::vector<std::map<std::string, std::array<float, 2>>> ims_info;
  28. if (!self.Run(&images, &outputs, &ims_info)) {
  29. throw std::runtime_error(
  30. "Failed to preprocess the input data in BlazeFacePreprocessor.");
  31. }
  32. for (size_t i = 0; i < outputs.size(); ++i) {
  33. outputs[i].StopSharing();
  34. }
  35. return make_pair(outputs, ims_info);
  36. });
  37. pybind11::class_<vision::facedet::BlazeFacePostprocessor>(
  38. m, "BlazeFacePostprocessor")
  39. .def(pybind11::init<>())
  40. .def("run",
  41. [](vision::facedet::BlazeFacePostprocessor &self,
  42. std::vector<FDTensor> &inputs,
  43. const std::vector<std::map<std::string, std::array<float, 2>>>
  44. &ims_info) {
  45. std::vector<vision::FaceDetectionResult> results;
  46. if (!self.Run(inputs, &results, ims_info)) {
  47. throw std::runtime_error("Failed to postprocess the runtime "
  48. "result in BlazeFacePostprocessor.");
  49. }
  50. return results;
  51. })
  52. .def("run",
  53. [](vision::facedet::BlazeFacePostprocessor &self,
  54. std::vector<pybind11::array> &input_array,
  55. const std::vector<std::map<std::string, std::array<float, 2>>>
  56. &ims_info) {
  57. std::vector<vision::FaceDetectionResult> results;
  58. std::vector<FDTensor> inputs;
  59. PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
  60. if (!self.Run(inputs, &results, ims_info)) {
  61. throw std::runtime_error("Failed to postprocess the runtime "
  62. "result in BlazePostprocessor.");
  63. }
  64. return results;
  65. })
  66. .def_property("conf_threshold",
  67. &vision::facedet::BlazeFacePostprocessor::GetConfThreshold,
  68. &vision::facedet::BlazeFacePostprocessor::SetConfThreshold)
  69. .def_property("nms_threshold",
  70. &vision::facedet::BlazeFacePostprocessor::GetNMSThreshold,
  71. &vision::facedet::BlazeFacePostprocessor::SetNMSThreshold);
  72. pybind11::class_<vision::facedet::BlazeFace, UltraInferModel>(m, "BlazeFace")
  73. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  74. ModelFormat>())
  75. .def("predict",
  76. [](vision::facedet::BlazeFace &self, pybind11::array &data) {
  77. auto mat = PyArrayToCvMat(data);
  78. vision::FaceDetectionResult res;
  79. self.Predict(mat, &res);
  80. return res;
  81. })
  82. .def("batch_predict",
  83. [](vision::facedet::BlazeFace &self,
  84. std::vector<pybind11::array> &data) {
  85. std::vector<cv::Mat> images;
  86. for (size_t i = 0; i < data.size(); ++i) {
  87. images.push_back(PyArrayToCvMat(data[i]));
  88. }
  89. std::vector<vision::FaceDetectionResult> results;
  90. self.BatchPredict(images, &results);
  91. return results;
  92. })
  93. .def_property_readonly("preprocessor",
  94. &vision::facedet::BlazeFace::GetPreprocessor)
  95. .def_property_readonly("postprocessor",
  96. &vision::facedet::BlazeFace::GetPostprocessor);
  97. }
  98. } // namespace ultra_infer