caddn_pybind.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 BindCaddn(pybind11::module &m) {
  17. pybind11::class_<vision::perception::CaddnPreprocessor,
  18. vision::ProcessorManager>(m, "CaddnPreprocessor")
  19. .def(pybind11::init<std::string>())
  20. .def("run",
  21. [](vision::perception::CaddnPreprocessor &self,
  22. std::vector<pybind11::array> &im_list,
  23. std::vector<float> &cam_data, std::vector<float> &lidar_data) {
  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. if (!self.Run(&images, cam_data, lidar_data, &outputs)) {
  30. throw std::runtime_error(
  31. "Failed to preprocess the input data in CaddnPreprocessor.");
  32. }
  33. for (size_t i = 0; i < outputs.size(); ++i) {
  34. outputs[i].StopSharing();
  35. }
  36. return outputs;
  37. });
  38. pybind11::class_<vision::perception::CaddnPostprocessor>(m,
  39. "CaddnPostprocessor")
  40. .def(pybind11::init<>())
  41. .def("run",
  42. [](vision::perception::CaddnPostprocessor &self,
  43. std::vector<FDTensor> &inputs) {
  44. std::vector<vision::PerceptionResult> results;
  45. if (!self.Run(inputs, &results)) {
  46. throw std::runtime_error(
  47. "Failed to postprocess the runtime result in "
  48. "CaddnPostprocessor.");
  49. }
  50. return results;
  51. })
  52. .def("run", [](vision::perception::CaddnPostprocessor &self,
  53. std::vector<pybind11::array> &input_array) {
  54. std::vector<vision::PerceptionResult> results;
  55. std::vector<FDTensor> inputs;
  56. PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
  57. if (!self.Run(inputs, &results)) {
  58. throw std::runtime_error(
  59. "Failed to postprocess the runtime result in "
  60. "CaddnPostprocessor.");
  61. }
  62. return results;
  63. });
  64. pybind11::class_<vision::perception::Caddn, UltraInferModel>(m, "Caddn")
  65. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  66. ModelFormat>())
  67. .def("predict",
  68. [](vision::perception::Caddn &self, pybind11::array &data,
  69. std::vector<float> &cam_data, std::vector<float> &lidar_data) {
  70. auto mat = PyArrayToCvMat(data);
  71. vision::PerceptionResult res;
  72. self.Predict(mat, cam_data, lidar_data, &res);
  73. return res;
  74. })
  75. .def("batch_predict",
  76. [](vision::perception::Caddn &self,
  77. std::vector<pybind11::array> &data, std::vector<float> &cam_data,
  78. std::vector<float> &lidar_data) {
  79. std::vector<cv::Mat> images;
  80. for (size_t i = 0; i < data.size(); ++i) {
  81. images.push_back(PyArrayToCvMat(data[i]));
  82. }
  83. std::vector<vision::PerceptionResult> results;
  84. self.BatchPredict(images, cam_data, lidar_data, &results);
  85. return results;
  86. })
  87. .def_property_readonly("preprocessor",
  88. &vision::perception::Caddn::GetPreprocessor)
  89. .def_property_readonly("postprocessor",
  90. &vision::perception::Caddn::GetPostprocessor);
  91. }
  92. } // namespace ultra_infer