yolov5cls_pybind.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 BindYOLOv5Cls(pybind11::module &m) {
  17. pybind11::class_<vision::classification::YOLOv5ClsPreprocessor>(
  18. m, "YOLOv5ClsPreprocessor")
  19. .def(pybind11::init<>())
  20. .def("run",
  21. [](vision::classification::YOLOv5ClsPreprocessor &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. std::vector<FDTensor> outputs;
  28. std::vector<std::map<std::string, std::array<float, 2>>> ims_info;
  29. if (!self.Run(&images, &outputs, &ims_info)) {
  30. throw std::runtime_error(
  31. "raise Exception('Failed to preprocess the input data in "
  32. "YOLOv5ClsPreprocessor.')");
  33. }
  34. for (size_t i = 0; i < outputs.size(); ++i) {
  35. outputs[i].StopSharing();
  36. }
  37. return make_pair(outputs, ims_info);
  38. })
  39. .def_property("size",
  40. &vision::classification::YOLOv5ClsPreprocessor::GetSize,
  41. &vision::classification::YOLOv5ClsPreprocessor::SetSize);
  42. pybind11::class_<vision::classification::YOLOv5ClsPostprocessor>(
  43. m, "YOLOv5ClsPostprocessor")
  44. .def(pybind11::init<>())
  45. .def("run",
  46. [](vision::classification::YOLOv5ClsPostprocessor &self,
  47. std::vector<FDTensor> &inputs,
  48. const std::vector<std::map<std::string, std::array<float, 2>>>
  49. &ims_info) {
  50. std::vector<vision::ClassifyResult> results;
  51. if (!self.Run(inputs, &results, ims_info)) {
  52. throw std::runtime_error(
  53. "raise Exception('Failed to postprocess the runtime result "
  54. "in YOLOv5ClsPostprocessor.')");
  55. }
  56. return results;
  57. })
  58. .def("run",
  59. [](vision::classification::YOLOv5ClsPostprocessor &self,
  60. std::vector<pybind11::array> &input_array,
  61. const std::vector<std::map<std::string, std::array<float, 2>>>
  62. &ims_info) {
  63. std::vector<vision::ClassifyResult> results;
  64. std::vector<FDTensor> inputs;
  65. PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
  66. if (!self.Run(inputs, &results, ims_info)) {
  67. throw std::runtime_error(
  68. "raise Exception('Failed to postprocess the runtime result "
  69. "in YOLOv5ClsPostprocessor.')");
  70. }
  71. return results;
  72. })
  73. .def_property("topk",
  74. &vision::classification::YOLOv5ClsPostprocessor::GetTopK,
  75. &vision::classification::YOLOv5ClsPostprocessor::SetTopK);
  76. pybind11::class_<vision::classification::YOLOv5Cls, UltraInferModel>(
  77. m, "YOLOv5Cls")
  78. .def(pybind11::init<std::string, std::string, RuntimeOption,
  79. ModelFormat>())
  80. .def("predict",
  81. [](vision::classification::YOLOv5Cls &self, pybind11::array &data) {
  82. auto mat = PyArrayToCvMat(data);
  83. vision::ClassifyResult res;
  84. self.Predict(mat, &res);
  85. return res;
  86. })
  87. .def("batch_predict",
  88. [](vision::classification::YOLOv5Cls &self,
  89. std::vector<pybind11::array> &data) {
  90. std::vector<cv::Mat> images;
  91. for (size_t i = 0; i < data.size(); ++i) {
  92. images.push_back(PyArrayToCvMat(data[i]));
  93. }
  94. std::vector<vision::ClassifyResult> results;
  95. self.BatchPredict(images, &results);
  96. return results;
  97. })
  98. .def_property_readonly(
  99. "preprocessor", &vision::classification::YOLOv5Cls::GetPreprocessor)
  100. .def_property_readonly(
  101. "postprocessor",
  102. &vision::classification::YOLOv5Cls::GetPostprocessor);
  103. }
  104. } // namespace ultra_infer