bindings_utils.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #ifndef OPENCV_CORE_BINDINGS_UTILS_HPP
  5. #define OPENCV_CORE_BINDINGS_UTILS_HPP
  6. #include <opencv2/core/async.hpp>
  7. #include <opencv2/core/detail/async_promise.hpp>
  8. #include <opencv2/core/utils/logger.hpp>
  9. #include <stdexcept>
  10. namespace cv { namespace utils {
  11. //! @addtogroup core_utils
  12. //! @{
  13. CV_EXPORTS_W String dumpInputArray(InputArray argument);
  14. CV_EXPORTS_W String dumpInputArrayOfArrays(InputArrayOfArrays argument);
  15. CV_EXPORTS_W String dumpInputOutputArray(InputOutputArray argument);
  16. CV_EXPORTS_W String dumpInputOutputArrayOfArrays(InputOutputArrayOfArrays argument);
  17. CV_WRAP static inline
  18. String dumpBool(bool argument)
  19. {
  20. return (argument) ? String("Bool: True") : String("Bool: False");
  21. }
  22. CV_WRAP static inline
  23. String dumpInt(int argument)
  24. {
  25. return cv::format("Int: %d", argument);
  26. }
  27. CV_WRAP static inline
  28. String dumpSizeT(size_t argument)
  29. {
  30. std::ostringstream oss("size_t: ", std::ios::ate);
  31. oss << argument;
  32. return oss.str();
  33. }
  34. CV_WRAP static inline
  35. String dumpFloat(float argument)
  36. {
  37. return cv::format("Float: %.2f", argument);
  38. }
  39. CV_WRAP static inline
  40. String dumpDouble(double argument)
  41. {
  42. return cv::format("Double: %.2f", argument);
  43. }
  44. CV_WRAP static inline
  45. String dumpCString(const char* argument)
  46. {
  47. return cv::format("String: %s", argument);
  48. }
  49. CV_WRAP static inline
  50. String dumpString(const String& argument)
  51. {
  52. return cv::format("String: %s", argument.c_str());
  53. }
  54. CV_WRAP static inline
  55. String testOverloadResolution(int value, const Point& point = Point(42, 24))
  56. {
  57. return format("overload (int=%d, point=(x=%d, y=%d))", value, point.x,
  58. point.y);
  59. }
  60. CV_WRAP static inline
  61. String testOverloadResolution(const Rect& rect)
  62. {
  63. return format("overload (rect=(x=%d, y=%d, w=%d, h=%d))", rect.x, rect.y,
  64. rect.width, rect.height);
  65. }
  66. CV_WRAP static inline
  67. String dumpRect(const Rect& argument)
  68. {
  69. return format("rect: (x=%d, y=%d, w=%d, h=%d)", argument.x, argument.y,
  70. argument.width, argument.height);
  71. }
  72. CV_WRAP static inline
  73. String dumpTermCriteria(const TermCriteria& argument)
  74. {
  75. return format("term_criteria: (type=%d, max_count=%d, epsilon=%lf",
  76. argument.type, argument.maxCount, argument.epsilon);
  77. }
  78. CV_WRAP static inline
  79. String dumpRotatedRect(const RotatedRect& argument)
  80. {
  81. return format("rotated_rect: (c_x=%f, c_y=%f, w=%f, h=%f, a=%f)",
  82. argument.center.x, argument.center.y, argument.size.width,
  83. argument.size.height, argument.angle);
  84. }
  85. CV_WRAP static inline
  86. String dumpRange(const Range& argument)
  87. {
  88. if (argument == Range::all())
  89. {
  90. return "range: all";
  91. }
  92. else
  93. {
  94. return format("range: (s=%d, e=%d)", argument.start, argument.end);
  95. }
  96. }
  97. CV_WRAP static inline
  98. void testRaiseGeneralException()
  99. {
  100. throw std::runtime_error("exception text");
  101. }
  102. CV_WRAP static inline
  103. AsyncArray testAsyncArray(InputArray argument)
  104. {
  105. AsyncPromise p;
  106. p.setValue(argument);
  107. return p.getArrayResult();
  108. }
  109. CV_WRAP static inline
  110. AsyncArray testAsyncException()
  111. {
  112. AsyncPromise p;
  113. try
  114. {
  115. CV_Error(Error::StsOk, "Test: Generated async error");
  116. }
  117. catch (const cv::Exception& e)
  118. {
  119. p.setException(e);
  120. }
  121. return p.getArrayResult();
  122. }
  123. //! @} // core_utils
  124. } // namespace cv::utils
  125. //! @cond IGNORED
  126. CV_WRAP static inline
  127. int setLogLevel(int level)
  128. {
  129. // NB: Binding generators doesn't work with enums properly yet, so we define separate overload here
  130. return cv::utils::logging::setLogLevel((cv::utils::logging::LogLevel)level);
  131. }
  132. CV_WRAP static inline
  133. int getLogLevel()
  134. {
  135. return cv::utils::logging::getLogLevel();
  136. }
  137. //! @endcond IGNORED
  138. } // namespaces cv / utils
  139. #endif // OPENCV_CORE_BINDINGS_UTILS_HPP