bindings_utils.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 dumpInt64(int64 argument)
  29. {
  30. std::ostringstream oss("Int64: ", std::ios::ate);
  31. oss << argument;
  32. return oss.str();
  33. }
  34. CV_WRAP static inline
  35. String dumpSizeT(size_t argument)
  36. {
  37. std::ostringstream oss("size_t: ", std::ios::ate);
  38. oss << argument;
  39. return oss.str();
  40. }
  41. CV_WRAP static inline
  42. String dumpFloat(float argument)
  43. {
  44. return cv::format("Float: %.2f", argument);
  45. }
  46. CV_WRAP static inline
  47. String dumpDouble(double argument)
  48. {
  49. return cv::format("Double: %.2f", argument);
  50. }
  51. CV_WRAP static inline
  52. String dumpCString(const char* argument)
  53. {
  54. return cv::format("String: %s", argument);
  55. }
  56. CV_WRAP static inline
  57. String dumpString(const String& argument)
  58. {
  59. return cv::format("String: %s", argument.c_str());
  60. }
  61. CV_WRAP static inline
  62. String dumpRect(const Rect& argument)
  63. {
  64. return format("rect: (x=%d, y=%d, w=%d, h=%d)", argument.x, argument.y,
  65. argument.width, argument.height);
  66. }
  67. CV_WRAP static inline
  68. String dumpTermCriteria(const TermCriteria& argument)
  69. {
  70. return format("term_criteria: (type=%d, max_count=%d, epsilon=%lf",
  71. argument.type, argument.maxCount, argument.epsilon);
  72. }
  73. CV_WRAP static inline
  74. String dumpRotatedRect(const RotatedRect& argument)
  75. {
  76. return format("rotated_rect: (c_x=%f, c_y=%f, w=%f, h=%f, a=%f)",
  77. argument.center.x, argument.center.y, argument.size.width,
  78. argument.size.height, argument.angle);
  79. }
  80. CV_WRAP static inline
  81. String dumpRange(const Range& argument)
  82. {
  83. if (argument == Range::all())
  84. {
  85. return "range: all";
  86. }
  87. else
  88. {
  89. return format("range: (s=%d, e=%d)", argument.start, argument.end);
  90. }
  91. }
  92. CV_EXPORTS_W String dumpVectorOfInt(const std::vector<int>& vec);
  93. CV_EXPORTS_W String dumpVectorOfDouble(const std::vector<double>& vec);
  94. CV_EXPORTS_W String dumpVectorOfRect(const std::vector<Rect>& vec);
  95. //! @cond IGNORED
  96. CV_WRAP static inline
  97. String testOverloadResolution(int value, const Point& point = Point(42, 24))
  98. {
  99. return format("overload (int=%d, point=(x=%d, y=%d))", value, point.x,
  100. point.y);
  101. }
  102. CV_WRAP static inline
  103. String testOverloadResolution(const Rect& rect)
  104. {
  105. return format("overload (rect=(x=%d, y=%d, w=%d, h=%d))", rect.x, rect.y,
  106. rect.width, rect.height);
  107. }
  108. CV_WRAP static inline
  109. RotatedRect testRotatedRect(float x, float y, float w, float h, float angle)
  110. {
  111. return RotatedRect(Point2f(x, y), Size2f(w, h), angle);
  112. }
  113. CV_WRAP static inline
  114. std::vector<RotatedRect> testRotatedRectVector(float x, float y, float w, float h, float angle)
  115. {
  116. std::vector<RotatedRect> result;
  117. for (int i = 0; i < 10; i++)
  118. result.push_back(RotatedRect(Point2f(x + i, y + 2 * i), Size2f(w, h), angle + 10 * i));
  119. return result;
  120. }
  121. CV_WRAP static inline
  122. int testOverwriteNativeMethod(int argument)
  123. {
  124. return argument;
  125. }
  126. CV_WRAP static inline
  127. String testReservedKeywordConversion(int positional_argument, int lambda = 2, int from = 3)
  128. {
  129. return format("arg=%d, lambda=%d, from=%d", positional_argument, lambda, from);
  130. }
  131. CV_WRAP static inline
  132. void generateVectorOfRect(size_t len, CV_OUT std::vector<Rect>& vec)
  133. {
  134. vec.resize(len);
  135. if (len > 0)
  136. {
  137. RNG rng(12345);
  138. Mat tmp(static_cast<int>(len), 1, CV_32SC4);
  139. rng.fill(tmp, RNG::UNIFORM, 10, 20);
  140. tmp.copyTo(vec);
  141. }
  142. }
  143. CV_WRAP static inline
  144. void generateVectorOfInt(size_t len, CV_OUT std::vector<int>& vec)
  145. {
  146. vec.resize(len);
  147. if (len > 0)
  148. {
  149. RNG rng(554433);
  150. Mat tmp(static_cast<int>(len), 1, CV_32SC1);
  151. rng.fill(tmp, RNG::UNIFORM, -10, 10);
  152. tmp.copyTo(vec);
  153. }
  154. }
  155. CV_WRAP static inline
  156. void generateVectorOfMat(size_t len, int rows, int cols, int dtype, CV_OUT std::vector<Mat>& vec)
  157. {
  158. vec.resize(len);
  159. if (len > 0)
  160. {
  161. RNG rng(65431);
  162. for (size_t i = 0; i < len; ++i)
  163. {
  164. vec[i].create(rows, cols, dtype);
  165. rng.fill(vec[i], RNG::UNIFORM, 0, 10);
  166. }
  167. }
  168. }
  169. CV_WRAP static inline
  170. void testRaiseGeneralException()
  171. {
  172. throw std::runtime_error("exception text");
  173. }
  174. CV_WRAP static inline
  175. AsyncArray testAsyncArray(InputArray argument)
  176. {
  177. AsyncPromise p;
  178. p.setValue(argument);
  179. return p.getArrayResult();
  180. }
  181. CV_WRAP static inline
  182. AsyncArray testAsyncException()
  183. {
  184. AsyncPromise p;
  185. try
  186. {
  187. CV_Error(Error::StsOk, "Test: Generated async error");
  188. }
  189. catch (const cv::Exception& e)
  190. {
  191. p.setException(e);
  192. }
  193. return p.getArrayResult();
  194. }
  195. CV_WRAP static inline
  196. String dumpVec2i(const cv::Vec2i value = cv::Vec2i(42, 24)) {
  197. return format("Vec2i(%d, %d)", value[0], value[1]);
  198. }
  199. struct CV_EXPORTS_W_SIMPLE ClassWithKeywordProperties {
  200. CV_PROP_RW int lambda;
  201. CV_PROP int except;
  202. CV_WRAP explicit ClassWithKeywordProperties(int lambda_arg = 24, int except_arg = 42)
  203. {
  204. lambda = lambda_arg;
  205. except = except_arg;
  206. }
  207. };
  208. struct CV_EXPORTS_W_PARAMS FunctionParams
  209. {
  210. CV_PROP_RW int lambda = -1;
  211. CV_PROP_RW float sigma = 0.0f;
  212. FunctionParams& setLambda(int value) CV_NOEXCEPT
  213. {
  214. lambda = value;
  215. return *this;
  216. }
  217. FunctionParams& setSigma(float value) CV_NOEXCEPT
  218. {
  219. sigma = value;
  220. return *this;
  221. }
  222. };
  223. CV_WRAP static inline String
  224. copyMatAndDumpNamedArguments(InputArray src, OutputArray dst,
  225. const FunctionParams& params = FunctionParams())
  226. {
  227. src.copyTo(dst);
  228. return format("lambda=%d, sigma=%.1f", params.lambda,
  229. params.sigma);
  230. }
  231. namespace nested {
  232. CV_WRAP static inline bool testEchoBooleanFunction(bool flag) {
  233. return flag;
  234. }
  235. class CV_EXPORTS_W CV_WRAP_AS(ExportClassName) OriginalClassName
  236. {
  237. public:
  238. struct CV_EXPORTS_W_SIMPLE Params
  239. {
  240. CV_PROP_RW int int_value;
  241. CV_PROP_RW float float_value;
  242. CV_WRAP explicit Params(int int_param = 123, float float_param = 3.5f)
  243. {
  244. int_value = int_param;
  245. float_value = float_param;
  246. }
  247. };
  248. explicit OriginalClassName(const OriginalClassName::Params& params = OriginalClassName::Params())
  249. {
  250. params_ = params;
  251. }
  252. CV_WRAP int getIntParam() const
  253. {
  254. return params_.int_value;
  255. }
  256. CV_WRAP float getFloatParam() const
  257. {
  258. return params_.float_value;
  259. }
  260. CV_WRAP static std::string originalName()
  261. {
  262. return "OriginalClassName";
  263. }
  264. CV_WRAP static Ptr<OriginalClassName>
  265. create(const OriginalClassName::Params& params = OriginalClassName::Params())
  266. {
  267. return makePtr<OriginalClassName>(params);
  268. }
  269. private:
  270. OriginalClassName::Params params_;
  271. };
  272. typedef OriginalClassName::Params OriginalClassName_Params;
  273. } // namespace nested
  274. //! @endcond IGNORED
  275. namespace fs {
  276. CV_EXPORTS_W cv::String getCacheDirectoryForDownloads();
  277. } // namespace fs
  278. //! @} // core_utils
  279. } // namespace cv::utils
  280. //! @cond IGNORED
  281. CV_WRAP static inline
  282. int setLogLevel(int level)
  283. {
  284. // NB: Binding generators doesn't work with enums properly yet, so we define separate overload here
  285. return cv::utils::logging::setLogLevel((cv::utils::logging::LogLevel)level);
  286. }
  287. CV_WRAP static inline
  288. int getLogLevel()
  289. {
  290. return cv::utils::logging::getLogLevel();
  291. }
  292. //! @endcond IGNORED
  293. } // namespaces cv / utils
  294. #endif // OPENCV_CORE_BINDINGS_UTILS_HPP