yolov7_pybind.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 BindYOLOv7(pybind11::module &m) {
  17. pybind11::class_<vision::detection::YOLOv7Preprocessor>(m,
  18. "YOLOv7Preprocessor")
  19. .def(pybind11::init<>())
  20. .def(
  21. "run",
  22. [](vision::detection::YOLOv7Preprocessor &self,
  23. std::vector<pybind11::array> &im_list) {
  24. std::vector<vision::FDMat> images;
  25. for (size_t i = 0; i < im_list.size(); ++i) {
  26. images.push_back(vision::WrapMat(PyArrayToCvMat(im_list[i])));
  27. }
  28. std::vector<FDTensor> outputs;
  29. std::vector<std::map<std::string, std::array<float, 2>>> ims_info;
  30. if (!self.Run(&images, &outputs, &ims_info)) {
  31. throw std::runtime_error(
  32. "Failed to preprocess the input data in YOLOV7Preprocessor.");
  33. }
  34. for (size_t i = 0; i < outputs.size(); ++i) {
  35. outputs[i].StopSharing();
  36. }
  37. return make_pair(outputs, ims_info);
  38. })
  39. .def_property("size", &vision::detection::YOLOv7Preprocessor::GetSize,
  40. &vision::detection::YOLOv7Preprocessor::SetSize)
  41. .def_property("padding_value",
  42. &vision::detection::YOLOv7Preprocessor::GetPaddingValue,
  43. &vision::detection::YOLOv7Preprocessor::SetPaddingValue)
  44. .def_property("is_scale_up",
  45. &vision::detection::YOLOv7Preprocessor::GetScaleUp,
  46. &vision::detection::YOLOv7Preprocessor::SetScaleUp);
  47. pybind11::class_<vision::detection::YOLOv7Postprocessor>(
  48. m, "YOLOv7Postprocessor")
  49. .def(pybind11::init<>())
  50. .def("run",
  51. [](vision::detection::YOLOv7Postprocessor &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::DetectionResult> 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::detection::YOLOv7Postprocessor &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::DetectionResult> 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::detection::YOLOv7Postprocessor::GetConfThreshold,
  78. &vision::detection::YOLOv7Postprocessor::SetConfThreshold)
  79. .def_property("nms_threshold",
  80. &vision::detection::YOLOv7Postprocessor::GetNMSThreshold,
  81. &vision::detection::YOLOv7Postprocessor::SetNMSThreshold);
  82. pybind11::class_<vision::detection::YOLOv7, UltraInferModel>(m, "YOLOv7")
  83. .def(pybind11::init<std::string, std::string, RuntimeOption,
  84. ModelFormat>())
  85. .def("predict",
  86. [](vision::detection::YOLOv7 &self, pybind11::array &data) {
  87. auto mat = PyArrayToCvMat(data);
  88. vision::DetectionResult res;
  89. self.Predict(mat, &res);
  90. return res;
  91. })
  92. .def("batch_predict",
  93. [](vision::detection::YOLOv7 &self,
  94. std::vector<pybind11::array> &data) {
  95. std::vector<cv::Mat> images;
  96. for (size_t i = 0; i < data.size(); ++i) {
  97. images.push_back(PyArrayToCvMat(data[i]));
  98. }
  99. std::vector<vision::DetectionResult> results;
  100. self.BatchPredict(images, &results);
  101. return results;
  102. })
  103. .def_property_readonly("preprocessor",
  104. &vision::detection::YOLOv7::GetPreprocessor)
  105. .def_property_readonly("postprocessor",
  106. &vision::detection::YOLOv7::GetPostprocessor);
  107. }
  108. } // namespace ultra_infer