ppdet_pybind.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 BindPPDet(pybind11::module &m) {
  17. pybind11::class_<vision::detection::PaddleDetPreprocessor,
  18. vision::ProcessorManager>(m, "PaddleDetPreprocessor")
  19. .def(pybind11::init<std::string>())
  20. .def("run",
  21. [](vision::detection::PaddleDetPreprocessor &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. "PaddleDetPreprocessor.");
  32. }
  33. for (size_t i = 0; i < outputs.size(); ++i) {
  34. outputs[i].StopSharing();
  35. }
  36. return outputs;
  37. })
  38. .def("disable_normalize",
  39. [](vision::detection::PaddleDetPreprocessor &self) {
  40. self.DisableNormalize();
  41. })
  42. .def("disable_permute",
  43. [](vision::detection::PaddleDetPreprocessor &self) {
  44. self.DisablePermute();
  45. });
  46. pybind11::class_<vision::detection::NMSOption>(m, "NMSOption")
  47. .def(pybind11::init())
  48. .def_readwrite("background_label",
  49. &vision::detection::NMSOption::background_label)
  50. .def_readwrite("keep_top_k", &vision::detection::NMSOption::keep_top_k)
  51. .def_readwrite("nms_eta", &vision::detection::NMSOption::nms_eta)
  52. .def_readwrite("nms_threshold",
  53. &vision::detection::NMSOption::nms_threshold)
  54. .def_readwrite("nms_top_k", &vision::detection::NMSOption::nms_top_k)
  55. .def_readwrite("normalized", &vision::detection::NMSOption::normalized)
  56. .def_readwrite("score_threshold",
  57. &vision::detection::NMSOption::score_threshold);
  58. pybind11::class_<vision::detection::PaddleDetPostprocessor>(
  59. m, "PaddleDetPostprocessor")
  60. .def(pybind11::init<>())
  61. .def(pybind11::init<std::string>())
  62. .def("run",
  63. [](vision::detection::PaddleDetPostprocessor &self,
  64. std::vector<FDTensor> &inputs) {
  65. std::vector<vision::DetectionResult> results;
  66. if (!self.Run(inputs, &results)) {
  67. throw std::runtime_error(
  68. "Failed to postprocess the runtime result in "
  69. "PaddleDetPostprocessor.");
  70. }
  71. return results;
  72. })
  73. .def("set_nms_option",
  74. [](vision::detection::PaddleDetPostprocessor &self,
  75. vision::detection::NMSOption option) {
  76. self.SetNMSOption(option);
  77. })
  78. .def("set_nms_rotated_option",
  79. [](vision::detection::PaddleDetPostprocessor &self,
  80. vision::detection::NMSRotatedOption option) {
  81. self.SetNMSRotatedOption(option);
  82. })
  83. .def("apply_nms",
  84. [](vision::detection::PaddleDetPostprocessor &self) {
  85. self.ApplyNMS();
  86. })
  87. .def("run", [](vision::detection::PaddleDetPostprocessor &self,
  88. std::vector<pybind11::array> &input_array) {
  89. std::vector<vision::DetectionResult> results;
  90. std::vector<FDTensor> inputs;
  91. PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
  92. if (!self.Run(inputs, &results)) {
  93. throw std::runtime_error(
  94. "Failed to postprocess the runtime result in "
  95. "PaddleDetPostprocessor.");
  96. }
  97. return results;
  98. });
  99. pybind11::class_<vision::detection::PPDetBase, UltraInferModel>(m,
  100. "PPDetBase")
  101. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  102. ModelFormat>())
  103. .def("predict",
  104. [](vision::detection::PPDetBase &self, pybind11::array &data) {
  105. auto mat = PyArrayToCvMat(data);
  106. vision::DetectionResult res;
  107. self.Predict(&mat, &res);
  108. return res;
  109. })
  110. .def("batch_predict",
  111. [](vision::detection::PPDetBase &self,
  112. std::vector<pybind11::array> &data) {
  113. std::vector<cv::Mat> images;
  114. for (size_t i = 0; i < data.size(); ++i) {
  115. images.push_back(PyArrayToCvMat(data[i]));
  116. }
  117. std::vector<vision::DetectionResult> results;
  118. self.BatchPredict(images, &results);
  119. return results;
  120. })
  121. .def("clone",
  122. [](vision::detection::PPDetBase &self) { return self.Clone(); })
  123. .def_property_readonly("preprocessor",
  124. &vision::detection::PPDetBase::GetPreprocessor)
  125. .def_property_readonly("postprocessor",
  126. &vision::detection::PPDetBase::GetPostprocessor);
  127. pybind11::class_<vision::detection::PPYOLO, vision::detection::PPDetBase>(
  128. m, "PPYOLO")
  129. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  130. ModelFormat>());
  131. pybind11::class_<vision::detection::PPYOLOE, vision::detection::PPDetBase>(
  132. m, "PPYOLOE")
  133. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  134. ModelFormat>());
  135. pybind11::class_<vision::detection::PicoDet, vision::detection::PPDetBase>(
  136. m, "PicoDet")
  137. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  138. ModelFormat>());
  139. pybind11::class_<vision::detection::PaddleYOLOX,
  140. vision::detection::PPDetBase>(m, "PaddleYOLOX")
  141. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  142. ModelFormat>());
  143. pybind11::class_<vision::detection::FasterRCNN, vision::detection::PPDetBase>(
  144. m, "FasterRCNN")
  145. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  146. ModelFormat>());
  147. pybind11::class_<vision::detection::YOLOv3, vision::detection::PPDetBase>(
  148. m, "YOLOv3")
  149. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  150. ModelFormat>());
  151. pybind11::class_<vision::detection::MaskRCNN, vision::detection::PPDetBase>(
  152. m, "MaskRCNN")
  153. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  154. ModelFormat>());
  155. pybind11::class_<vision::detection::SSD, vision::detection::PPDetBase>(m,
  156. "SSD")
  157. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  158. ModelFormat>());
  159. pybind11::class_<vision::detection::PaddleYOLOv5,
  160. vision::detection::PPDetBase>(m, "PaddleYOLOv5")
  161. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  162. ModelFormat>());
  163. pybind11::class_<vision::detection::PaddleYOLOv6,
  164. vision::detection::PPDetBase>(m, "PaddleYOLOv6")
  165. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  166. ModelFormat>());
  167. pybind11::class_<vision::detection::PaddleYOLOv7,
  168. vision::detection::PPDetBase>(m, "PaddleYOLOv7")
  169. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  170. ModelFormat>());
  171. pybind11::class_<vision::detection::PaddleYOLOv8,
  172. vision::detection::PPDetBase>(m, "PaddleYOLOv8")
  173. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  174. ModelFormat>());
  175. pybind11::class_<vision::detection::RTMDet, vision::detection::PPDetBase>(
  176. m, "RTMDet")
  177. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  178. ModelFormat>());
  179. pybind11::class_<vision::detection::CascadeRCNN,
  180. vision::detection::PPDetBase>(m, "CascadeRCNN")
  181. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  182. ModelFormat>());
  183. pybind11::class_<vision::detection::PSSDet, vision::detection::PPDetBase>(
  184. m, "PSSDet")
  185. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  186. ModelFormat>());
  187. pybind11::class_<vision::detection::RetinaNet, vision::detection::PPDetBase>(
  188. m, "RetinaNet")
  189. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  190. ModelFormat>());
  191. pybind11::class_<vision::detection::PPYOLOESOD, vision::detection::PPDetBase>(
  192. m, "PPYOLOESOD")
  193. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  194. ModelFormat>());
  195. pybind11::class_<vision::detection::FCOS, vision::detection::PPDetBase>(
  196. m, "FCOS")
  197. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  198. ModelFormat>());
  199. pybind11::class_<vision::detection::TTFNet, vision::detection::PPDetBase>(
  200. m, "TTFNet")
  201. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  202. ModelFormat>());
  203. pybind11::class_<vision::detection::TOOD, vision::detection::PPDetBase>(
  204. m, "TOOD")
  205. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  206. ModelFormat>());
  207. pybind11::class_<vision::detection::GFL, vision::detection::PPDetBase>(m,
  208. "GFL")
  209. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  210. ModelFormat>());
  211. pybind11::class_<vision::detection::SOLOv2, vision::detection::PPDetBase>(
  212. m, "SOLOv2")
  213. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  214. ModelFormat>());
  215. pybind11::class_<vision::detection::PaddleDetectionModel,
  216. vision::detection::PPDetBase>(m, "PaddleDetectionModel")
  217. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  218. ModelFormat>());
  219. pybind11::class_<vision::detection::PPYOLOER, vision::detection::PPDetBase>(
  220. m, "PPYOLOER")
  221. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  222. ModelFormat>());
  223. pybind11::class_<vision::detection::NMSRotatedOption>(m, "NMSRotatedOption")
  224. .def(pybind11::init())
  225. .def_readwrite("background_label",
  226. &vision::detection::NMSRotatedOption::background_label)
  227. .def_readwrite("keep_top_k",
  228. &vision::detection::NMSRotatedOption::keep_top_k)
  229. .def_readwrite("nms_eta", &vision::detection::NMSRotatedOption::nms_eta)
  230. .def_readwrite("nms_threshold",
  231. &vision::detection::NMSRotatedOption::nms_threshold)
  232. .def_readwrite("nms_top_k",
  233. &vision::detection::NMSRotatedOption::nms_top_k)
  234. .def_readwrite("normalized",
  235. &vision::detection::NMSRotatedOption::normalized)
  236. .def_readwrite("score_threshold",
  237. &vision::detection::NMSRotatedOption::score_threshold);
  238. }
  239. } // namespace ultra_infer