From 6a8c5a1d27663a0d498c828216db391420d02817 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 8 Dec 2022 23:37:39 +0000 Subject: [PATCH 1/2] python: resolve Ptr requirement issue --- modules/core/include/opencv2/core.hpp | 6 +++--- modules/core/src/algorithm.cpp | 10 +++++----- .../features2d/include/opencv2/features2d.hpp | 4 ++-- modules/python/src2/gen2.py | 20 +++++++++++++++++-- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/modules/core/include/opencv2/core.hpp b/modules/core/include/opencv2/core.hpp index be0a3a0dc5..1223651a83 100644 --- a/modules/core/include/opencv2/core.hpp +++ b/modules/core/include/opencv2/core.hpp @@ -3095,12 +3095,12 @@ public: /** @brief Stores algorithm parameters in a file storage */ - virtual void write(FileStorage& fs) const { CV_UNUSED(fs); } + CV_WRAP virtual void write(FileStorage& fs) const { CV_UNUSED(fs); } - /** @brief simplified API for language bindings + /** * @overload */ - CV_WRAP void write(const Ptr& fs, const String& name = String()) const; + CV_WRAP void write(FileStorage& fs, const String& name) const; /** @brief Reads algorithm parameters from a file storage */ diff --git a/modules/core/src/algorithm.cpp b/modules/core/src/algorithm.cpp index 556f5a7328..7186585323 100644 --- a/modules/core/src/algorithm.cpp +++ b/modules/core/src/algorithm.cpp @@ -55,17 +55,17 @@ Algorithm::~Algorithm() CV_TRACE_FUNCTION(); } -void Algorithm::write(const Ptr& fs, const String& name) const +void Algorithm::write(FileStorage& fs, const String& name) const { CV_TRACE_FUNCTION(); if(name.empty()) { - write(*fs); + write(fs); return; } - *fs << name << "{"; - write(*fs); - *fs << "}"; + fs << name << "{"; + write(fs); + fs << "}"; } void Algorithm::save(const String& filename) const diff --git a/modules/features2d/include/opencv2/features2d.hpp b/modules/features2d/include/opencv2/features2d.hpp index bf193599e1..633cd5c579 100644 --- a/modules/features2d/include/opencv2/features2d.hpp +++ b/modules/features2d/include/opencv2/features2d.hpp @@ -212,7 +212,7 @@ public: CV_WRAP virtual String getDefaultName() const CV_OVERRIDE; // see corresponding cv::Algorithm method - CV_WRAP inline void write(const Ptr& fs, const String& name = String()) const { Algorithm::write(fs, name); } + CV_WRAP inline void write(FileStorage& fs, const String& name) const { Algorithm::write(fs, name); } }; /** Feature detectors in OpenCV have wrappers with a common interface that enables you to easily switch @@ -1101,7 +1101,7 @@ public: // see corresponding cv::Algorithm method - CV_WRAP inline void write(const Ptr& fs, const String& name = String()) const { Algorithm::write(fs, name); } + CV_WRAP inline void write(FileStorage& fs, const String& name) const { Algorithm::write(fs, name); } protected: /** diff --git a/modules/python/src2/gen2.py b/modules/python/src2/gen2.py index 1a9239c07f..b8be4da510 100755 --- a/modules/python/src2/gen2.py +++ b/modules/python/src2/gen2.py @@ -209,7 +209,8 @@ simple_argtype_mapping = { "int": ArgTypeInfo("int", FormatStrings.int, "0", True), "float": ArgTypeInfo("float", FormatStrings.float, "0.f", True), "double": ArgTypeInfo("double", FormatStrings.double, "0", True), - "c_string": ArgTypeInfo("char*", FormatStrings.string, '(char*)""') + "c_string": ArgTypeInfo("char*", FormatStrings.string, '(char*)""'), + "UMat": ArgTypeInfo("UMat", FormatStrings.object, 'UMat()', True), # FIXIT: switch to CV_EXPORTS_W_SIMPLE as UMat is already a some kind of smart pointer } # Set of reserved keywords for Python. Can be acquired via the following call @@ -422,6 +423,7 @@ class ArgInfo(object): self.name += "_" self.defval = arg_tuple[2] self.isarray = False + self.is_smart_ptr = self.tp.startswith('Ptr<') # FIXIT: handle through modifiers - need to modify parser self.arraylen = 0 self.arraycvt = None self.inputarg = True @@ -713,7 +715,21 @@ class FuncInfo(object): if any(tp in codegen.enums.keys() for tp in tp_candidates): defval0 = "static_cast<%s>(%d)" % (a.tp, 0) - arg_type_info = simple_argtype_mapping.get(tp, ArgTypeInfo(tp, FormatStrings.object, defval0, True)) + if tp in simple_argtype_mapping: + arg_type_info = simple_argtype_mapping[tp] + else: + if tp in all_classes: + tp_classinfo = all_classes[tp] + cname_of_value = tp_classinfo.cname if tp_classinfo.issimple else "Ptr<{}>".format(tp_classinfo.cname) + arg_type_info = ArgTypeInfo(cname_of_value, FormatStrings.object, defval0, True) + assert not (a.is_smart_ptr and tp_classinfo.issimple), "Can't pass 'simple' type as Ptr<>" + if not a.is_smart_ptr and not tp_classinfo.issimple: + assert amp == '' + amp = '*' + else: + # FIXIT: Ptr_ / vector_ / enums / nested types + arg_type_info = ArgTypeInfo(tp, FormatStrings.object, defval0, True) + parse_name = a.name if a.py_inputarg: if arg_type_info.strict_conversion: From 6e3700593f46549206e52fa0659774c92e42ab13 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 9 Dec 2022 21:05:17 +0000 Subject: [PATCH 2/2] compatibility: keep Ptr stubs till OpenCV 5.0 --- modules/core/include/opencv2/core.hpp | 4 ++++ modules/core/src/algorithm.cpp | 8 ++++++++ modules/features2d/include/opencv2/features2d.hpp | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/modules/core/include/opencv2/core.hpp b/modules/core/include/opencv2/core.hpp index 1223651a83..0445be1753 100644 --- a/modules/core/include/opencv2/core.hpp +++ b/modules/core/include/opencv2/core.hpp @@ -3101,6 +3101,10 @@ public: * @overload */ CV_WRAP void write(FileStorage& fs, const String& name) const; +#if CV_VERSION_MAJOR < 5 + /** @deprecated */ + void write(const Ptr& fs, const String& name = String()) const; +#endif /** @brief Reads algorithm parameters from a file storage */ diff --git a/modules/core/src/algorithm.cpp b/modules/core/src/algorithm.cpp index 7186585323..798df39638 100644 --- a/modules/core/src/algorithm.cpp +++ b/modules/core/src/algorithm.cpp @@ -68,6 +68,14 @@ void Algorithm::write(FileStorage& fs, const String& name) const fs << "}"; } +#if CV_VERSION_MAJOR < 5 +void Algorithm::write(const Ptr& fs, const String& name) const +{ + CV_Assert(fs); + write(*fs, name); +} +#endif + void Algorithm::save(const String& filename) const { CV_TRACE_FUNCTION(); diff --git a/modules/features2d/include/opencv2/features2d.hpp b/modules/features2d/include/opencv2/features2d.hpp index 633cd5c579..401fe6135c 100644 --- a/modules/features2d/include/opencv2/features2d.hpp +++ b/modules/features2d/include/opencv2/features2d.hpp @@ -213,6 +213,9 @@ public: // see corresponding cv::Algorithm method CV_WRAP inline void write(FileStorage& fs, const String& name) const { Algorithm::write(fs, name); } +#if CV_VERSION_MAJOR < 5 + inline void write(const Ptr& fs, const String& name) const { CV_Assert(fs); Algorithm::write(*fs, name); } +#endif }; /** Feature detectors in OpenCV have wrappers with a common interface that enables you to easily switch @@ -1102,6 +1105,9 @@ public: // see corresponding cv::Algorithm method CV_WRAP inline void write(FileStorage& fs, const String& name) const { Algorithm::write(fs, name); } +#if CV_VERSION_MAJOR < 5 + inline void write(const Ptr& fs, const String& name) const { CV_Assert(fs); Algorithm::write(*fs, name); } +#endif protected: /**