ppseg_pybind.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 BindPPSeg(pybind11::module &m) {
  17. pybind11::class_<vision::segmentation::PaddleSegPreprocessor,
  18. vision::ProcessorManager>(m, "PaddleSegPreprocessor")
  19. .def(pybind11::init<std::string>())
  20. .def("run",
  21. [](vision::segmentation::PaddleSegPreprocessor &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. // Record the shape of input images
  28. std::map<std::string, std::vector<std::array<int, 2>>> imgs_info;
  29. std::vector<FDTensor> outputs;
  30. self.SetImgsInfo(&imgs_info);
  31. if (!self.Run(&images, &outputs)) {
  32. throw std::runtime_error(
  33. "Failed to preprocess the input data in "
  34. "PaddleSegPreprocessor.");
  35. }
  36. for (size_t i = 0; i < outputs.size(); ++i) {
  37. outputs[i].StopSharing();
  38. }
  39. return make_pair(outputs, imgs_info);
  40. ;
  41. })
  42. .def("disable_normalize",
  43. [](vision::segmentation::PaddleSegPreprocessor &self) {
  44. self.DisableNormalize();
  45. })
  46. .def("disable_permute",
  47. [](vision::segmentation::PaddleSegPreprocessor &self) {
  48. self.DisablePermute();
  49. })
  50. .def_property(
  51. "is_vertical_screen",
  52. &vision::segmentation::PaddleSegPreprocessor::GetIsVerticalScreen,
  53. &vision::segmentation::PaddleSegPreprocessor::SetIsVerticalScreen);
  54. pybind11::class_<vision::segmentation::PaddleSegModel, UltraInferModel>(
  55. m, "PaddleSegModel")
  56. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  57. ModelFormat>())
  58. .def("clone",
  59. [](vision::segmentation::PaddleSegModel &self) {
  60. return self.Clone();
  61. })
  62. .def("predict",
  63. [](vision::segmentation::PaddleSegModel &self,
  64. pybind11::array &data) {
  65. auto mat = PyArrayToCvMat(data);
  66. vision::SegmentationResult res;
  67. self.Predict(&mat, &res);
  68. return res;
  69. })
  70. .def("batch_predict",
  71. [](vision::segmentation::PaddleSegModel &self,
  72. std::vector<pybind11::array> &data) {
  73. std::vector<cv::Mat> images;
  74. for (size_t i = 0; i < data.size(); ++i) {
  75. images.push_back(PyArrayToCvMat(data[i]));
  76. }
  77. std::vector<vision::SegmentationResult> results;
  78. self.BatchPredict(images, &results);
  79. return results;
  80. })
  81. .def_property_readonly(
  82. "preprocessor",
  83. &vision::segmentation::PaddleSegModel::GetPreprocessor)
  84. .def_property_readonly(
  85. "postprocessor",
  86. &vision::segmentation::PaddleSegModel::GetPostprocessor);
  87. pybind11::class_<vision::segmentation::PaddleSegPostprocessor>(
  88. m, "PaddleSegPostprocessor")
  89. .def(pybind11::init<std::string>())
  90. .def("run",
  91. [](vision::segmentation::PaddleSegPostprocessor &self,
  92. std::vector<FDTensor> &inputs,
  93. const std::map<std::string, std::vector<std::array<int, 2>>>
  94. &imgs_info) {
  95. std::vector<vision::SegmentationResult> results;
  96. if (!self.Run(inputs, &results, imgs_info)) {
  97. throw std::runtime_error(
  98. "Failed to postprocess the runtime result in "
  99. "PaddleSegPostprocessor.");
  100. }
  101. return results;
  102. })
  103. .def("run",
  104. [](vision::segmentation::PaddleSegPostprocessor &self,
  105. std::vector<pybind11::array> &input_array,
  106. const std::map<std::string, std::vector<std::array<int, 2>>>
  107. &imgs_info) {
  108. std::vector<vision::SegmentationResult> results;
  109. std::vector<FDTensor> inputs;
  110. PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
  111. if (!self.Run(inputs, &results, imgs_info)) {
  112. throw std::runtime_error(
  113. "Failed to postprocess the runtime result in "
  114. "PaddleSegPostprocessor.");
  115. }
  116. return results;
  117. })
  118. .def_property(
  119. "apply_softmax",
  120. &vision::segmentation::PaddleSegPostprocessor::GetApplySoftmax,
  121. &vision::segmentation::PaddleSegPostprocessor::SetApplySoftmax)
  122. .def_property(
  123. "store_score_map",
  124. &vision::segmentation::PaddleSegPostprocessor::GetStoreScoreMap,
  125. &vision::segmentation::PaddleSegPostprocessor::SetStoreScoreMap);
  126. }
  127. } // namespace ultra_infer