rkyolo_pybind.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 BindRKYOLO(pybind11::module &m) {
  17. pybind11::class_<vision::detection::RKYOLOPreprocessor>(m,
  18. "RKYOLOPreprocessor")
  19. .def(pybind11::init<>())
  20. .def("run",
  21. [](vision::detection::RKYOLOPreprocessor &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. if (!self.Run(&images, &outputs)) {
  29. throw std::runtime_error(
  30. "Failed to preprocess the input data in "
  31. "PaddleClasPreprocessor.");
  32. }
  33. for (size_t i = 0; i < outputs.size(); ++i) {
  34. outputs[i].StopSharing();
  35. }
  36. return outputs;
  37. })
  38. .def_property("size", &vision::detection::RKYOLOPreprocessor::GetSize,
  39. &vision::detection::RKYOLOPreprocessor::SetSize)
  40. .def_property("padding_value",
  41. &vision::detection::RKYOLOPreprocessor::GetPaddingValue,
  42. &vision::detection::RKYOLOPreprocessor::SetPaddingValue)
  43. .def_property("is_scale_up",
  44. &vision::detection::RKYOLOPreprocessor::GetScaleUp,
  45. &vision::detection::RKYOLOPreprocessor::SetScaleUp);
  46. pybind11::class_<vision::detection::RKYOLOPostprocessor>(
  47. m, "RKYOLOPostprocessor")
  48. .def(pybind11::init<>())
  49. .def("run",
  50. [](vision::detection::RKYOLOPostprocessor &self,
  51. std::vector<FDTensor> &inputs) {
  52. std::vector<vision::DetectionResult> results;
  53. if (!self.Run(inputs, &results)) {
  54. throw std::runtime_error(
  55. "Failed to postprocess the runtime result in "
  56. "RKYOLOV5Postprocessor.");
  57. }
  58. return results;
  59. })
  60. .def("run",
  61. [](vision::detection::RKYOLOPostprocessor &self,
  62. std::vector<pybind11::array> &input_array) {
  63. std::vector<vision::DetectionResult> results;
  64. std::vector<FDTensor> inputs;
  65. PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
  66. if (!self.Run(inputs, &results)) {
  67. throw std::runtime_error(
  68. "Failed to postprocess the runtime result in "
  69. "RKYOLOV5Postprocessor.");
  70. }
  71. return results;
  72. })
  73. .def("set_anchor", [](vision::detection::RKYOLOPostprocessor &self,
  74. std::vector<int> &data) { self.SetAnchor(data); })
  75. .def_property("conf_threshold",
  76. &vision::detection::RKYOLOPostprocessor::GetConfThreshold,
  77. &vision::detection::RKYOLOPostprocessor::SetConfThreshold)
  78. .def_property("nms_threshold",
  79. &vision::detection::RKYOLOPostprocessor::GetNMSThreshold,
  80. &vision::detection::RKYOLOPostprocessor::SetNMSThreshold)
  81. .def_property("class_num",
  82. &vision::detection::RKYOLOPostprocessor::GetClassNum,
  83. &vision::detection::RKYOLOPostprocessor::SetClassNum);
  84. pybind11::class_<vision::detection::RKYOLOV5, UltraInferModel>(m, "RKYOLOV5")
  85. .def(pybind11::init<std::string, RuntimeOption, ModelFormat>())
  86. .def("predict",
  87. [](vision::detection::RKYOLOV5 &self, pybind11::array &data) {
  88. auto mat = PyArrayToCvMat(data);
  89. vision::DetectionResult res;
  90. self.Predict(mat, &res);
  91. return res;
  92. })
  93. .def("batch_predict",
  94. [](vision::detection::RKYOLOV5 &self,
  95. std::vector<pybind11::array> &data) {
  96. std::vector<cv::Mat> images;
  97. for (size_t i = 0; i < data.size(); ++i) {
  98. images.push_back(PyArrayToCvMat(data[i]));
  99. }
  100. std::vector<vision::DetectionResult> results;
  101. self.BatchPredict(images, &results);
  102. return results;
  103. })
  104. .def_property_readonly("preprocessor",
  105. &vision::detection::RKYOLOV5::GetPreprocessor)
  106. .def_property_readonly("postprocessor",
  107. &vision::detection::RKYOLOV5::GetPostprocessor);
  108. pybind11::class_<vision::detection::RKYOLOX, UltraInferModel>(m, "RKYOLOX")
  109. .def(pybind11::init<std::string, RuntimeOption, ModelFormat>())
  110. .def("predict",
  111. [](vision::detection::RKYOLOX &self, pybind11::array &data) {
  112. auto mat = PyArrayToCvMat(data);
  113. vision::DetectionResult res;
  114. self.Predict(mat, &res);
  115. return res;
  116. })
  117. .def("batch_predict",
  118. [](vision::detection::RKYOLOX &self,
  119. std::vector<pybind11::array> &data) {
  120. std::vector<cv::Mat> images;
  121. for (size_t i = 0; i < data.size(); ++i) {
  122. images.push_back(PyArrayToCvMat(data[i]));
  123. }
  124. std::vector<vision::DetectionResult> results;
  125. self.BatchPredict(images, &results);
  126. return results;
  127. })
  128. .def_property_readonly("preprocessor",
  129. &vision::detection::RKYOLOX::GetPreprocessor)
  130. .def_property_readonly("postprocessor",
  131. &vision::detection::RKYOLOX::GetPostprocessor);
  132. pybind11::class_<vision::detection::RKYOLOV7, UltraInferModel>(m, "RKYOLOV7")
  133. .def(pybind11::init<std::string, RuntimeOption, ModelFormat>())
  134. .def("predict",
  135. [](vision::detection::RKYOLOV7 &self, pybind11::array &data) {
  136. auto mat = PyArrayToCvMat(data);
  137. vision::DetectionResult res;
  138. self.Predict(mat, &res);
  139. return res;
  140. })
  141. .def("batch_predict",
  142. [](vision::detection::RKYOLOV7 &self,
  143. std::vector<pybind11::array> &data) {
  144. std::vector<cv::Mat> images;
  145. for (size_t i = 0; i < data.size(); ++i) {
  146. images.push_back(PyArrayToCvMat(data[i]));
  147. }
  148. std::vector<vision::DetectionResult> results;
  149. self.BatchPredict(images, &results);
  150. return results;
  151. })
  152. .def_property_readonly("preprocessor",
  153. &vision::detection::RKYOLOV7::GetPreprocessor)
  154. .def_property_readonly("postprocessor",
  155. &vision::detection::RKYOLOV7::GetPostprocessor);
  156. }
  157. } // namespace ultra_infer