yolov7face_pybind.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 BindYOLOv7Face(pybind11::module &m) {
  17. pybind11::class_<vision::facedet::Yolov7FacePreprocessor>(
  18. m, "Yolov7FacePreprocessor")
  19. .def(pybind11::init<>())
  20. .def("run",
  21. [](vision::facedet::Yolov7FacePreprocessor &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 PaddleClasPreprocessor.");
  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::Yolov7FacePreprocessor::GetSize,
  39. &vision::facedet::Yolov7FacePreprocessor::SetSize)
  40. .def_property(
  41. "padding_color_value",
  42. &vision::facedet::Yolov7FacePreprocessor::GetPaddingColorValue,
  43. &vision::facedet::Yolov7FacePreprocessor::SetPaddingColorValue)
  44. .def_property("is_scale_up",
  45. &vision::facedet::Yolov7FacePreprocessor::GetScaleUp,
  46. &vision::facedet::Yolov7FacePreprocessor::SetScaleUp);
  47. pybind11::class_<vision::facedet::Yolov7FacePostprocessor>(
  48. m, "YOLOv7FacePostprocessor")
  49. .def(pybind11::init<>())
  50. .def("run",
  51. [](vision::facedet::Yolov7FacePostprocessor &self,
  52. std::vector<FDTensor> &inputs,
  53. const std::vector<std::map<std::string, std::array<float, 2>>>
  54. &ims_info) {
  55. std::vector<vision::FaceDetectionResult> results;
  56. if (!self.Run(inputs, &results, ims_info)) {
  57. throw std::runtime_error("Failed to postprocess the runtime "
  58. "result in Yolov7Postprocessor.");
  59. }
  60. return results;
  61. })
  62. .def("run",
  63. [](vision::facedet::Yolov7FacePostprocessor &self,
  64. std::vector<pybind11::array> &input_array,
  65. const std::vector<std::map<std::string, std::array<float, 2>>>
  66. &ims_info) {
  67. std::vector<vision::FaceDetectionResult> results;
  68. std::vector<FDTensor> inputs;
  69. PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
  70. if (!self.Run(inputs, &results, ims_info)) {
  71. throw std::runtime_error("Failed to postprocess the runtime "
  72. "result in YOLOv7Postprocessor.");
  73. }
  74. return results;
  75. })
  76. .def_property("conf_threshold",
  77. &vision::facedet::Yolov7FacePostprocessor::GetConfThreshold,
  78. &vision::facedet::Yolov7FacePostprocessor::SetConfThreshold)
  79. .def_property("nms_threshold",
  80. &vision::facedet::Yolov7FacePostprocessor::GetNMSThreshold,
  81. &vision::facedet::Yolov7FacePostprocessor::SetNMSThreshold)
  82. .def_property(
  83. "landmarks_per_face",
  84. &vision::facedet::Yolov7FacePostprocessor::GetLandmarksPerFace,
  85. &vision::facedet::Yolov7FacePostprocessor::SetLandmarksPerFace);
  86. pybind11::class_<vision::facedet::YOLOv7Face, UltraInferModel>(m,
  87. "YOLOv7Face")
  88. .def(pybind11::init<std::string, std::string, RuntimeOption,
  89. ModelFormat>())
  90. .def("predict",
  91. [](vision::facedet::YOLOv7Face &self, pybind11::array &data) {
  92. auto mat = PyArrayToCvMat(data);
  93. vision::FaceDetectionResult res;
  94. self.Predict(mat, &res);
  95. return res;
  96. })
  97. .def("batch_predict",
  98. [](vision::facedet::YOLOv7Face &self,
  99. std::vector<pybind11::array> &data) {
  100. std::vector<cv::Mat> images;
  101. for (size_t i = 0; i < data.size(); ++i) {
  102. images.push_back(PyArrayToCvMat(data[i]));
  103. }
  104. std::vector<vision::FaceDetectionResult> results;
  105. self.BatchPredict(images, &results);
  106. return results;
  107. })
  108. .def_property_readonly("preprocessor",
  109. &vision::facedet::YOLOv7Face::GetPreprocessor)
  110. .def_property_readonly("postprocessor",
  111. &vision::facedet::YOLOv7Face::GetPostprocessor);
  112. }
  113. } // namespace ultra_infer