ppshitu_pybind.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 BindPPShiTuV2(pybind11::module &m) {
  17. pybind11::class_<vision::classification::PPShiTuV2RecognizerPreprocessor,
  18. vision::ProcessorManager>(m,
  19. "PPShiTuV2RecognizerPreprocessor")
  20. .def(pybind11::init<std::string>())
  21. .def("disable_normalize",
  22. [](vision::classification::PPShiTuV2RecognizerPreprocessor &self) {
  23. self.DisableNormalize();
  24. })
  25. .def("disable_permute",
  26. [](vision::classification::PPShiTuV2RecognizerPreprocessor &self) {
  27. self.DisablePermute();
  28. })
  29. .def("initial_resize_on_cpu",
  30. [](vision::classification::PPShiTuV2RecognizerPreprocessor &self,
  31. bool v) { self.InitialResizeOnCpu(v); });
  32. pybind11::class_<vision::classification::PPShiTuV2RecognizerPostprocessor>(
  33. m, "PPShiTuV2RecognizerPostprocessor")
  34. .def(pybind11::init<>())
  35. .def("run",
  36. [](vision::classification::PPShiTuV2RecognizerPostprocessor &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. "PPShiTuV2RecognizerPostprocessor.");
  43. }
  44. return results;
  45. })
  46. .def("run",
  47. [](vision::classification::PPShiTuV2RecognizerPostprocessor &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. "PPShiTuV2RecognizerPostprocessor.");
  56. }
  57. return results;
  58. })
  59. .def_property("feature_norm",
  60. &vision::classification::PPShiTuV2RecognizerPostprocessor::
  61. GetFeatureNorm,
  62. &vision::classification::PPShiTuV2RecognizerPostprocessor::
  63. SetFeatureNorm);
  64. pybind11::class_<vision::classification::PPShiTuV2Recognizer,
  65. UltraInferModel>(m, "PPShiTuV2Recognizer")
  66. .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
  67. ModelFormat>())
  68. .def("clone",
  69. [](vision::classification::PPShiTuV2Recognizer &self) {
  70. return self.Clone();
  71. })
  72. .def("predict",
  73. [](vision::classification::PPShiTuV2Recognizer &self,
  74. pybind11::array &data) {
  75. cv::Mat im = PyArrayToCvMat(data);
  76. vision::ClassifyResult result;
  77. self.Predict(im, &result);
  78. return result;
  79. })
  80. .def("batch_predict",
  81. [](vision::classification::PPShiTuV2Recognizer &self,
  82. std::vector<pybind11::array> &data) {
  83. std::vector<cv::Mat> images;
  84. for (size_t i = 0; i < data.size(); ++i) {
  85. images.push_back(PyArrayToCvMat(data[i]));
  86. }
  87. std::vector<vision::ClassifyResult> results;
  88. self.BatchPredict(images, &results);
  89. return results;
  90. })
  91. .def_property_readonly(
  92. "preprocessor",
  93. &vision::classification::PPShiTuV2Recognizer::GetPreprocessor)
  94. .def_property_readonly(
  95. "postprocessor",
  96. &vision::classification::PPShiTuV2Recognizer::GetPostprocessor);
  97. }
  98. } // namespace ultra_infer