ppocr_pybind.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <pybind11/stl.h>
  15. #include "ultra_infer/pybind/main.h"
  16. namespace ultra_infer {
  17. void BindPPOCRv4(pybind11::module &m) {
  18. // PPOCRv4
  19. pybind11::class_<pipeline::PPOCRv4, UltraInferModel>(m, "PPOCRv4")
  20. .def(pybind11::init<ultra_infer::vision::ocr::DBDetector *,
  21. ultra_infer::vision::ocr::Classifier *,
  22. ultra_infer::vision::ocr::Recognizer *>())
  23. .def(pybind11::init<ultra_infer::vision::ocr::DBDetector *,
  24. ultra_infer::vision::ocr::Recognizer *>())
  25. .def_property("cls_batch_size", &pipeline::PPOCRv4::GetClsBatchSize,
  26. &pipeline::PPOCRv4::SetClsBatchSize)
  27. .def_property("rec_batch_size", &pipeline::PPOCRv4::GetRecBatchSize,
  28. &pipeline::PPOCRv4::SetRecBatchSize)
  29. .def("clone", [](pipeline::PPOCRv4 &self) { return self.Clone(); })
  30. .def("predict",
  31. [](pipeline::PPOCRv4 &self, pybind11::array &data) {
  32. auto mat = PyArrayToCvMat(data);
  33. vision::OCRResult res;
  34. self.Predict(&mat, &res);
  35. return res;
  36. })
  37. .def("batch_predict",
  38. [](pipeline::PPOCRv4 &self, std::vector<pybind11::array> &data) {
  39. std::vector<cv::Mat> images;
  40. for (size_t i = 0; i < data.size(); ++i) {
  41. images.push_back(PyArrayToCvMat(data[i]));
  42. }
  43. std::vector<vision::OCRResult> results;
  44. self.BatchPredict(images, &results);
  45. return results;
  46. });
  47. }
  48. void BindPPOCRv3(pybind11::module &m) {
  49. // PPOCRv3
  50. pybind11::class_<pipeline::PPOCRv3, UltraInferModel>(m, "PPOCRv3")
  51. .def(pybind11::init<ultra_infer::vision::ocr::DBDetector *,
  52. ultra_infer::vision::ocr::Classifier *,
  53. ultra_infer::vision::ocr::Recognizer *>())
  54. .def(pybind11::init<ultra_infer::vision::ocr::DBDetector *,
  55. ultra_infer::vision::ocr::Recognizer *>())
  56. .def_property("cls_batch_size", &pipeline::PPOCRv3::GetClsBatchSize,
  57. &pipeline::PPOCRv3::SetClsBatchSize)
  58. .def_property("rec_batch_size", &pipeline::PPOCRv3::GetRecBatchSize,
  59. &pipeline::PPOCRv3::SetRecBatchSize)
  60. .def("clone", [](pipeline::PPOCRv3 &self) { return self.Clone(); })
  61. .def("predict",
  62. [](pipeline::PPOCRv3 &self, pybind11::array &data) {
  63. auto mat = PyArrayToCvMat(data);
  64. vision::OCRResult res;
  65. self.Predict(&mat, &res);
  66. return res;
  67. })
  68. .def("batch_predict",
  69. [](pipeline::PPOCRv3 &self, std::vector<pybind11::array> &data) {
  70. std::vector<cv::Mat> images;
  71. for (size_t i = 0; i < data.size(); ++i) {
  72. images.push_back(PyArrayToCvMat(data[i]));
  73. }
  74. std::vector<vision::OCRResult> results;
  75. self.BatchPredict(images, &results);
  76. return results;
  77. });
  78. }
  79. void BindPPOCRv2(pybind11::module &m) {
  80. // PPOCRv2
  81. pybind11::class_<pipeline::PPOCRv2, UltraInferModel>(m, "PPOCRv2")
  82. .def(pybind11::init<ultra_infer::vision::ocr::DBDetector *,
  83. ultra_infer::vision::ocr::Classifier *,
  84. ultra_infer::vision::ocr::Recognizer *>())
  85. .def(pybind11::init<ultra_infer::vision::ocr::DBDetector *,
  86. ultra_infer::vision::ocr::Recognizer *>())
  87. .def_property("cls_batch_size", &pipeline::PPOCRv2::GetClsBatchSize,
  88. &pipeline::PPOCRv2::SetClsBatchSize)
  89. .def_property("rec_batch_size", &pipeline::PPOCRv2::GetRecBatchSize,
  90. &pipeline::PPOCRv2::SetRecBatchSize)
  91. .def("clone", [](pipeline::PPOCRv2 &self) { return self.Clone(); })
  92. .def("predict",
  93. [](pipeline::PPOCRv2 &self, pybind11::array &data) {
  94. auto mat = PyArrayToCvMat(data);
  95. vision::OCRResult res;
  96. self.Predict(&mat, &res);
  97. return res;
  98. })
  99. .def("batch_predict",
  100. [](pipeline::PPOCRv2 &self, std::vector<pybind11::array> &data) {
  101. std::vector<cv::Mat> images;
  102. for (size_t i = 0; i < data.size(); ++i) {
  103. images.push_back(PyArrayToCvMat(data[i]));
  104. }
  105. std::vector<vision::OCRResult> results;
  106. self.BatchPredict(images, &results);
  107. return results;
  108. });
  109. }
  110. void BindPPStructureV2Table(pybind11::module &m) {
  111. // PPStructureV2Table
  112. pybind11::class_<pipeline::PPStructureV2Table, UltraInferModel>(
  113. m, "PPStructureV2Table")
  114. .def(pybind11::init<ultra_infer::vision::ocr::DBDetector *,
  115. ultra_infer::vision::ocr::Recognizer *,
  116. ultra_infer::vision::ocr::StructureV2Table *>())
  117. .def_property("rec_batch_size",
  118. &pipeline::PPStructureV2Table::GetRecBatchSize,
  119. &pipeline::PPStructureV2Table::SetRecBatchSize)
  120. .def("clone",
  121. [](pipeline::PPStructureV2Table &self) { return self.Clone(); })
  122. .def("predict",
  123. [](pipeline::PPStructureV2Table &self, pybind11::array &data) {
  124. auto mat = PyArrayToCvMat(data);
  125. vision::OCRResult res;
  126. self.Predict(&mat, &res);
  127. return res;
  128. })
  129. .def("batch_predict", [](pipeline::PPStructureV2Table &self,
  130. std::vector<pybind11::array> &data) {
  131. std::vector<cv::Mat> images;
  132. for (size_t i = 0; i < data.size(); ++i) {
  133. images.push_back(PyArrayToCvMat(data[i]));
  134. }
  135. std::vector<vision::OCRResult> results;
  136. self.BatchPredict(images, &results);
  137. return results;
  138. });
  139. }
  140. } // namespace ultra_infer