centerface_pybind.cc 4.8 KB

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