petr_pybind.cc 3.7 KB

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