python: resolve Ptr<FileStorage> requirement issue
authorAlexander Alekhin <alexander.a.alekhin@gmail.com>
Thu, 8 Dec 2022 23:37:39 +0000 (23:37 +0000)
committerAlexander Alekhin <alexander.a.alekhin@gmail.com>
Fri, 16 Dec 2022 00:47:44 +0000 (00:47 +0000)
modules/core/include/opencv2/core.hpp
modules/core/src/algorithm.cpp
modules/features2d/include/opencv2/features2d.hpp
modules/python/src2/gen2.py

index be0a3a0dc54b688eb933baa0367ff9db8596bfb3..1223651a83d19b7dd0cf590300402c6f616c330d 100644 (file)
@@ -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<FileStorage>& fs, const String& name = String()) const;
+    CV_WRAP void write(FileStorage& fs, const String& name) const;
 
     /** @brief Reads algorithm parameters from a file storage
     */
index 556f5a73284a9217145c104e83e5b23c6e7a2166..7186585323258e6f13340f02dbb3185ade937dfc 100644 (file)
@@ -55,17 +55,17 @@ Algorithm::~Algorithm()
     CV_TRACE_FUNCTION();
 }
 
-void Algorithm::write(const Ptr<FileStorage>& 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
index bf193599e1218692adf84a2c4767937e2d3f5aa3..633cd5c579a65502a3a1c5a40cc3b00713da3026 100644 (file)
@@ -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<FileStorage>& 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<FileStorage>& 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:
     /**
index 1a9239c07f2dcf3e99175f6c6b8fc1f38eba624e..b8be4da510e56226cdf643b72a70794c11632140 100755 (executable)
@@ -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: