yolov8_pybind.cc 5.6 KB

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