fastestdet_pybind.cc 4.9 KB

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