ppcls_pybind.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 BindPaddleClas(pybind11::module &m) {
  17. pybind11::class_<vision::classification::PaddleClasPreprocessor,
  18. vision::ProcessorManager>(m, "PaddleClasPreprocessor")
  19. .def(pybind11::init<std::string>())
  20. .def("disable_normalize",
  21. [](vision::classification::PaddleClasPreprocessor &self) {
  22. self.DisableNormalize();
  23. })
  24. .def("disable_permute",
  25. [](vision::classification::PaddleClasPreprocessor &self) {
  26. self.DisablePermute();
  27. })
  28. .def("initial_resize_on_cpu",
  29. [](vision::classification::PaddleClasPreprocessor &self, bool v) {
  30. self.InitialResizeOnCpu(v);
  31. });
  32. pybind11::class_<vision::classification::PaddleClasPostprocessor>(
  33. m, "PaddleClasPostprocessor")
  34. .def(pybind11::init<int>())
  35. .def("run",
  36. [](vision::classification::PaddleClasPostprocessor &self,
  37. std::vector<FDTensor> &inputs) {
  38. std::vector<vision::ClassifyResult> results;
  39. if (!self.Run(inputs, &results)) {
  40. throw std::runtime_error(
  41. "Failed to postprocess the runtime result in "
  42. "PaddleClasPostprocessor.");
  43. }
  44. return results;
  45. })
  46. .def("run",
  47. [](vision::classification::PaddleClasPostprocessor &self,
  48. std::vector<pybind11::array> &input_array) {
  49. std::vector<vision::ClassifyResult> results;
  50. std::vector<FDTensor> inputs;
  51. PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
  52. if (!self.Run(inputs, &results)) {
  53. throw std::runtime_error(
  54. "Failed to postprocess the runtime result in "
  55. "PaddleClasPostprocessor.");
  56. }
  57. return results;
  58. })
  59. .def_property("topk",
  60. &vision::classification::PaddleClasPostprocessor::GetTopk,
  61. &vision::classification::PaddleClasPostprocessor::SetTopk);
  62. pybind11::class_<vision::classification::PaddleClasModel, UltraInferModel>(
  63. m, "PaddleClasModel")
  64. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  65. ModelFormat>())
  66. .def("clone",
  67. [](vision::classification::PaddleClasModel &self) {
  68. return self.Clone();
  69. })
  70. .def("predict",
  71. [](vision::classification::PaddleClasModel &self,
  72. pybind11::array &data) {
  73. cv::Mat im = PyArrayToCvMat(data);
  74. vision::ClassifyResult result;
  75. self.Predict(im, &result);
  76. return result;
  77. })
  78. .def("batch_predict",
  79. [](vision::classification::PaddleClasModel &self,
  80. std::vector<pybind11::array> &data) {
  81. std::vector<cv::Mat> images;
  82. for (size_t i = 0; i < data.size(); ++i) {
  83. images.push_back(PyArrayToCvMat(data[i]));
  84. }
  85. std::vector<vision::ClassifyResult> results;
  86. self.BatchPredict(images, &results);
  87. return results;
  88. })
  89. .def_property_readonly(
  90. "preprocessor",
  91. &vision::classification::PaddleClasModel::GetPreprocessor)
  92. .def_property_readonly(
  93. "postprocessor",
  94. &vision::classification::PaddleClasModel::GetPostprocessor);
  95. }
  96. } // namespace ultra_infer