2013-04-11 22:34:04 +08:00
|
|
|
#!/usr/bin/env python
|
2012-11-24 02:57:22 +08:00
|
|
|
|
2013-06-13 04:02:30 +08:00
|
|
|
from __future__ import print_function
|
|
|
|
import hdr_parser, sys, re, os
|
2010-10-29 21:09:49 +08:00
|
|
|
from string import Template
|
2017-11-04 11:38:18 +08:00
|
|
|
from pprint import pprint
|
2020-01-13 23:11:34 +08:00
|
|
|
from collections import namedtuple
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2013-06-13 04:02:30 +08:00
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
from io import StringIO
|
|
|
|
else:
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
2020-01-13 23:11:34 +08:00
|
|
|
|
2018-09-28 21:53:05 +08:00
|
|
|
forbidden_arg_types = ["void*"]
|
|
|
|
|
2013-04-09 17:10:54 +08:00
|
|
|
ignored_arg_types = ["RNG*"]
|
|
|
|
|
2018-08-06 19:46:46 +08:00
|
|
|
pass_by_val_types = ["Point*", "Point2f*", "Rect*", "String*", "double*", "float*", "int*"]
|
|
|
|
|
2019-03-21 15:45:02 +08:00
|
|
|
gen_template_check_self = Template("""
|
|
|
|
${cname} * self1 = 0;
|
|
|
|
if (!pyopencv_${name}_getp(self, self1))
|
2012-09-11 20:47:25 +08:00
|
|
|
return failmsgp("Incorrect type of self (must be '${name}' or its derivative)");
|
2019-03-21 15:45:02 +08:00
|
|
|
${pname} _self_ = ${cvt}(self1);
|
2012-09-11 20:47:25 +08:00
|
|
|
""")
|
2017-09-05 13:38:17 +08:00
|
|
|
gen_template_call_constructor_prelude = Template("""new (&(self->v)) Ptr<$cname>(); // init Ptr with placement new
|
2013-08-14 16:32:41 +08:00
|
|
|
if(self) """)
|
2011-09-07 17:38:22 +08:00
|
|
|
|
2020-07-29 21:18:52 +08:00
|
|
|
gen_template_call_constructor = Template("""self->v.reset(new ${cname}${py_args})""")
|
2013-08-14 16:32:41 +08:00
|
|
|
|
2017-09-05 13:38:17 +08:00
|
|
|
gen_template_simple_call_constructor_prelude = Template("""if(self) """)
|
2013-08-14 16:32:41 +08:00
|
|
|
|
2020-07-29 21:18:52 +08:00
|
|
|
gen_template_simple_call_constructor = Template("""new (&(self->v)) ${cname}${py_args}""")
|
2010-10-29 21:09:49 +08:00
|
|
|
|
|
|
|
gen_template_parse_args = Template("""const char* keywords[] = { $kw_list, NULL };
|
2020-07-29 21:18:52 +08:00
|
|
|
if( PyArg_ParseTupleAndKeywords(py_args, kw, "$fmtspec", (char**)keywords, $parse_arglist)$code_cvt )""")
|
2010-10-29 21:09:49 +08:00
|
|
|
|
|
|
|
gen_template_func_body = Template("""$code_decl
|
|
|
|
$code_parse
|
|
|
|
{
|
2013-08-14 16:32:41 +08:00
|
|
|
${code_prelude}ERRWRAP2($code_fcall);
|
2010-11-03 01:58:22 +08:00
|
|
|
$code_ret;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
2018-08-19 01:34:47 +08:00
|
|
|
gen_template_mappable = Template("""
|
|
|
|
{
|
|
|
|
${mappable} _src;
|
2021-03-31 22:17:45 +08:00
|
|
|
if (pyopencv_to_safe(src, _src, info))
|
2018-08-19 01:34:47 +08:00
|
|
|
{
|
|
|
|
return cv_mappable_to(_src, dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""")
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
gen_template_type_decl = Template("""
|
2019-03-21 15:45:02 +08:00
|
|
|
// Converter (${name})
|
2011-09-07 17:38:22 +08:00
|
|
|
|
2018-09-08 15:33:23 +08:00
|
|
|
template<>
|
2019-03-21 15:45:02 +08:00
|
|
|
struct PyOpenCV_Converter< ${cname} >
|
2011-09-07 17:38:22 +08:00
|
|
|
{
|
2019-03-21 15:45:02 +08:00
|
|
|
static PyObject* from(const ${cname}& r)
|
2018-09-08 15:33:23 +08:00
|
|
|
{
|
2019-03-21 15:45:02 +08:00
|
|
|
return pyopencv_${name}_Instance(r);
|
2018-09-08 15:33:23 +08:00
|
|
|
}
|
2019-11-15 22:29:51 +08:00
|
|
|
static bool to(PyObject* src, ${cname}& dst, const ArgInfo& info)
|
2011-09-07 17:38:22 +08:00
|
|
|
{
|
2018-09-08 15:33:23 +08:00
|
|
|
if(!src || src == Py_None)
|
|
|
|
return true;
|
2019-03-21 15:45:02 +08:00
|
|
|
${cname} * dst_;
|
|
|
|
if (pyopencv_${name}_getp(src, dst_))
|
2018-09-08 15:33:23 +08:00
|
|
|
{
|
2019-03-21 15:45:02 +08:00
|
|
|
dst = *dst_;
|
2018-09-08 15:33:23 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
${mappable_code}
|
2019-11-15 22:29:51 +08:00
|
|
|
failmsg("Expected ${cname} for argument '%s'", info.name);
|
2018-09-08 15:33:23 +08:00
|
|
|
return false;
|
2011-09-07 17:38:22 +08:00
|
|
|
}
|
2018-09-08 15:33:23 +08:00
|
|
|
};
|
2011-09-07 17:38:22 +08:00
|
|
|
|
2019-03-21 15:45:02 +08:00
|
|
|
""")
|
2010-11-03 01:58:22 +08:00
|
|
|
|
|
|
|
gen_template_map_type_cvt = Template("""
|
2019-11-15 22:29:51 +08:00
|
|
|
template<> bool pyopencv_to(PyObject* src, ${cname}& dst, const ArgInfo& info);
|
2019-03-21 15:45:02 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
""")
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
gen_template_set_prop_from_map = Template("""
|
|
|
|
if( PyMapping_HasKeyString(src, (char*)"$propname") )
|
|
|
|
{
|
|
|
|
tmp = PyMapping_GetItemString(src, (char*)"$propname");
|
2021-03-31 22:17:45 +08:00
|
|
|
ok = tmp && pyopencv_to_safe(tmp, dst.$propname, ArgInfo("$propname", false));
|
2010-11-03 01:58:22 +08:00
|
|
|
Py_DECREF(tmp);
|
|
|
|
if(!ok) return false;
|
|
|
|
}""")
|
|
|
|
|
|
|
|
gen_template_type_impl = Template("""
|
2019-03-21 15:45:02 +08:00
|
|
|
// GetSet (${name})
|
2010-10-29 21:09:49 +08:00
|
|
|
|
|
|
|
${getset_code}
|
|
|
|
|
2019-03-21 15:45:02 +08:00
|
|
|
// Methods (${name})
|
|
|
|
|
|
|
|
${methods_code}
|
|
|
|
|
|
|
|
// Tables (${name})
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
static PyGetSetDef pyopencv_${name}_getseters[] =
|
|
|
|
{${getset_inits}
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyMethodDef pyopencv_${name}_methods[] =
|
|
|
|
{
|
|
|
|
${methods_inits}
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
""")
|
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
gen_template_get_prop = Template("""
|
|
|
|
static PyObject* pyopencv_${name}_get_${member}(pyopencv_${name}_t* p, void *closure)
|
|
|
|
{
|
2010-11-03 01:58:22 +08:00
|
|
|
return pyopencv_from(p->v${access}${member});
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
2012-09-12 00:04:18 +08:00
|
|
|
gen_template_get_prop_algo = Template("""
|
|
|
|
static PyObject* pyopencv_${name}_get_${member}(pyopencv_${name}_t* p, void *closure)
|
|
|
|
{
|
2017-07-06 20:57:12 +08:00
|
|
|
$cname* _self_ = dynamic_cast<$cname*>(p->v.get());
|
2018-08-06 19:46:46 +08:00
|
|
|
if (!_self_)
|
2017-07-06 20:57:12 +08:00
|
|
|
return failmsgp("Incorrect type of object (must be '${name}' or its derivative)");
|
|
|
|
return pyopencv_from(_self_${access}${member});
|
2012-09-12 00:04:18 +08:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
gen_template_set_prop = Template("""
|
|
|
|
static int pyopencv_${name}_set_${member}(pyopencv_${name}_t* p, PyObject *value, void *closure)
|
|
|
|
{
|
2018-08-06 19:46:46 +08:00
|
|
|
if (!value)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Cannot delete the ${member} attribute");
|
|
|
|
return -1;
|
|
|
|
}
|
2021-03-31 22:17:45 +08:00
|
|
|
return pyopencv_to_safe(value, p->v${access}${member}, ArgInfo("value", false)) ? 0 : -1;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
2012-09-12 00:04:18 +08:00
|
|
|
gen_template_set_prop_algo = Template("""
|
|
|
|
static int pyopencv_${name}_set_${member}(pyopencv_${name}_t* p, PyObject *value, void *closure)
|
|
|
|
{
|
2018-08-06 19:46:46 +08:00
|
|
|
if (!value)
|
2012-09-12 00:04:18 +08:00
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Cannot delete the ${member} attribute");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-07-06 20:57:12 +08:00
|
|
|
$cname* _self_ = dynamic_cast<$cname*>(p->v.get());
|
2018-08-06 19:46:46 +08:00
|
|
|
if (!_self_)
|
2017-07-06 20:57:12 +08:00
|
|
|
{
|
|
|
|
failmsgp("Incorrect type of object (must be '${name}' or its derivative)");
|
|
|
|
return -1;
|
|
|
|
}
|
2021-03-31 22:17:45 +08:00
|
|
|
return pyopencv_to_safe(value, _self_${access}${member}, ArgInfo("value", false)) ? 0 : -1;
|
2012-09-12 00:04:18 +08:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
gen_template_prop_init = Template("""
|
|
|
|
{(char*)"${member}", (getter)pyopencv_${name}_get_${member}, NULL, (char*)"${member}", NULL},""")
|
|
|
|
|
|
|
|
gen_template_rw_prop_init = Template("""
|
|
|
|
{(char*)"${member}", (getter)pyopencv_${name}_get_${member}, (setter)pyopencv_${name}_set_${member}, (char*)"${member}", NULL},""")
|
|
|
|
|
2021-01-12 22:50:07 +08:00
|
|
|
gen_template_overloaded_function_call = Template("""
|
|
|
|
{
|
|
|
|
${variant}
|
|
|
|
|
|
|
|
pyPopulateArgumentConversionErrors();
|
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
2020-01-13 23:11:34 +08:00
|
|
|
class FormatStrings:
|
|
|
|
string = 's'
|
|
|
|
unsigned_char = 'b'
|
|
|
|
short_int = 'h'
|
|
|
|
int = 'i'
|
|
|
|
unsigned_int = 'I'
|
|
|
|
long = 'l'
|
|
|
|
unsigned_long = 'k'
|
|
|
|
long_long = 'L'
|
|
|
|
unsigned_long_long = 'K'
|
|
|
|
size_t = 'n'
|
|
|
|
float = 'f'
|
|
|
|
double = 'd'
|
|
|
|
object = 'O'
|
|
|
|
|
|
|
|
ArgTypeInfo = namedtuple('ArgTypeInfo',
|
|
|
|
['atype', 'format_str', 'default_value',
|
|
|
|
'strict_conversion'])
|
|
|
|
# strict_conversion is False by default
|
|
|
|
ArgTypeInfo.__new__.__defaults__ = (False,)
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
simple_argtype_mapping = {
|
2020-01-13 23:11:34 +08:00
|
|
|
"bool": ArgTypeInfo("bool", FormatStrings.unsigned_char, "0", True),
|
|
|
|
"size_t": ArgTypeInfo("size_t", FormatStrings.unsigned_long_long, "0", True),
|
|
|
|
"int": ArgTypeInfo("int", FormatStrings.int, "0", True),
|
|
|
|
"float": ArgTypeInfo("float", FormatStrings.float, "0.f", True),
|
|
|
|
"double": ArgTypeInfo("double", FormatStrings.double, "0", True),
|
2020-11-17 05:34:42 +08:00
|
|
|
"c_string": ArgTypeInfo("char*", FormatStrings.string, '(char*)""'),
|
|
|
|
"string": ArgTypeInfo("std::string", FormatStrings.object, None, True),
|
2021-05-01 09:18:14 +08:00
|
|
|
"Stream": ArgTypeInfo("Stream", FormatStrings.object, 'Stream::Null()', True),
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
|
2021-08-01 16:59:16 +08:00
|
|
|
# Set of reserved keywords for Python. Can be acquired via the following call
|
|
|
|
# $ python -c "help('keywords')"
|
|
|
|
# Keywords that are reserved in C/C++ are excluded because they can not be
|
|
|
|
# used as variables identifiers
|
|
|
|
python_reserved_keywords = {
|
|
|
|
"True", "None", "False", "as", "assert", "def", "del", "elif", "except", "exec",
|
|
|
|
"finally", "from", "global", "import", "in", "is", "lambda", "nonlocal",
|
|
|
|
"pass", "print", "raise", "with", "yield"
|
|
|
|
}
|
|
|
|
|
2020-01-13 23:11:34 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
def normalize_class_name(name):
|
|
|
|
return re.sub(r"^cv\.", "", name).replace(".", "_")
|
|
|
|
|
2020-01-13 23:11:34 +08:00
|
|
|
|
|
|
|
def get_type_format_string(arg_type_info):
|
|
|
|
if arg_type_info.strict_conversion:
|
|
|
|
return FormatStrings.object
|
|
|
|
else:
|
|
|
|
return arg_type_info.format_str
|
|
|
|
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
class ClassProp(object):
|
|
|
|
def __init__(self, decl):
|
2010-11-03 01:58:22 +08:00
|
|
|
self.tp = decl[0].replace("*", "_ptr")
|
2010-10-29 21:09:49 +08:00
|
|
|
self.name = decl[1]
|
2020-12-18 20:00:42 +08:00
|
|
|
self.defval = decl[2]
|
2010-10-29 21:09:49 +08:00
|
|
|
self.readonly = True
|
|
|
|
if "/RW" in decl[3]:
|
|
|
|
self.readonly = False
|
|
|
|
|
|
|
|
class ClassInfo(object):
|
|
|
|
def __init__(self, name, decl=None):
|
|
|
|
self.cname = name.replace(".", "::")
|
2011-07-14 07:04:39 +08:00
|
|
|
self.name = self.wname = normalize_class_name(name)
|
2016-08-07 00:46:17 +08:00
|
|
|
self.sname = name[name.rfind('.') + 1:]
|
2010-10-29 21:09:49 +08:00
|
|
|
self.ismap = False
|
2010-11-03 01:58:22 +08:00
|
|
|
self.issimple = False
|
2012-09-11 20:47:25 +08:00
|
|
|
self.isalgorithm = False
|
2020-12-18 20:00:42 +08:00
|
|
|
self.isparams = False
|
2010-10-29 21:09:49 +08:00
|
|
|
self.methods = {}
|
|
|
|
self.props = []
|
2018-08-19 01:34:47 +08:00
|
|
|
self.mappables = []
|
2010-10-29 21:09:49 +08:00
|
|
|
self.consts = {}
|
2015-03-26 16:22:52 +08:00
|
|
|
self.base = None
|
2017-09-05 13:38:17 +08:00
|
|
|
self.constructor = None
|
2010-10-29 21:09:49 +08:00
|
|
|
customname = False
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
if decl:
|
2015-03-26 16:22:52 +08:00
|
|
|
bases = decl[1].split()[1:]
|
|
|
|
if len(bases) > 1:
|
2013-06-13 04:02:30 +08:00
|
|
|
print("Note: Class %s has more than 1 base class (not supported by Python C extensions)" % (self.name,))
|
2015-03-26 16:22:52 +08:00
|
|
|
print(" Bases: ", " ".join(bases))
|
2013-06-13 04:02:30 +08:00
|
|
|
print(" Only the first base class will be used")
|
2012-01-25 21:25:35 +08:00
|
|
|
#return sys.exit(-1)
|
2015-03-26 16:22:52 +08:00
|
|
|
elif len(bases) == 1:
|
|
|
|
self.base = bases[0].strip(",")
|
|
|
|
if self.base.startswith("cv::"):
|
|
|
|
self.base = self.base[4:]
|
|
|
|
if self.base == "Algorithm":
|
|
|
|
self.isalgorithm = True
|
|
|
|
self.base = self.base.replace("::", "_")
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
for m in decl[2]:
|
|
|
|
if m.startswith("="):
|
2021-03-31 04:54:11 +08:00
|
|
|
wname = m[1:]
|
|
|
|
npos = name.rfind('.')
|
|
|
|
if npos >= 0:
|
|
|
|
self.wname = normalize_class_name(name[:npos] + '.' + wname)
|
|
|
|
else:
|
|
|
|
self.wname = wname
|
2010-10-29 21:09:49 +08:00
|
|
|
customname = True
|
|
|
|
elif m == "/Map":
|
|
|
|
self.ismap = True
|
2010-11-03 01:58:22 +08:00
|
|
|
elif m == "/Simple":
|
|
|
|
self.issimple = True
|
2020-12-18 20:00:42 +08:00
|
|
|
elif m == "/Params":
|
|
|
|
self.isparams = True
|
2010-10-29 21:09:49 +08:00
|
|
|
self.props = [ClassProp(p) for p in decl[3]]
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
if not customname and self.wname.startswith("Cv"):
|
|
|
|
self.wname = self.wname[2:]
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2017-09-05 21:10:36 +08:00
|
|
|
def gen_map_code(self, codegen):
|
|
|
|
all_classes = codegen.classes
|
2019-11-15 22:29:51 +08:00
|
|
|
code = "static bool pyopencv_to(PyObject* src, %s& dst, const ArgInfo& info)\n{\n PyObject* tmp;\n bool ok;\n" % (self.cname)
|
2010-10-29 21:09:49 +08:00
|
|
|
code += "".join([gen_template_set_prop_from_map.substitute(propname=p.name,proptype=p.tp) for p in self.props])
|
2015-03-26 16:22:52 +08:00
|
|
|
if self.base:
|
2021-03-31 22:17:45 +08:00
|
|
|
code += "\n return pyopencv_to_safe(src, (%s&)dst, info);\n}\n" % all_classes[self.base].cname
|
2010-11-03 01:58:22 +08:00
|
|
|
else:
|
|
|
|
code += "\n return true;\n}\n"
|
2010-10-29 21:09:49 +08:00
|
|
|
return code
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2017-09-05 21:10:36 +08:00
|
|
|
def gen_code(self, codegen):
|
|
|
|
all_classes = codegen.classes
|
2010-10-29 21:09:49 +08:00
|
|
|
if self.ismap:
|
2017-09-05 21:10:36 +08:00
|
|
|
return self.gen_map_code(codegen)
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2013-06-13 04:02:30 +08:00
|
|
|
getset_code = StringIO()
|
|
|
|
getset_inits = StringIO()
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
sorted_props = [(p.name, p) for p in self.props]
|
|
|
|
sorted_props.sort()
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
access_op = "->"
|
|
|
|
if self.issimple:
|
|
|
|
access_op = "."
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
for pname, p in sorted_props:
|
2012-09-12 00:04:18 +08:00
|
|
|
if self.isalgorithm:
|
|
|
|
getset_code.write(gen_template_get_prop_algo.substitute(name=self.name, cname=self.cname, member=pname, membertype=p.tp, access=access_op))
|
|
|
|
else:
|
|
|
|
getset_code.write(gen_template_get_prop.substitute(name=self.name, member=pname, membertype=p.tp, access=access_op))
|
2010-10-29 21:09:49 +08:00
|
|
|
if p.readonly:
|
2010-11-03 01:58:22 +08:00
|
|
|
getset_inits.write(gen_template_prop_init.substitute(name=self.name, member=pname))
|
2010-10-29 21:09:49 +08:00
|
|
|
else:
|
2012-09-12 00:04:18 +08:00
|
|
|
if self.isalgorithm:
|
|
|
|
getset_code.write(gen_template_set_prop_algo.substitute(name=self.name, cname=self.cname, member=pname, membertype=p.tp, access=access_op))
|
|
|
|
else:
|
|
|
|
getset_code.write(gen_template_set_prop.substitute(name=self.name, member=pname, membertype=p.tp, access=access_op))
|
2010-11-03 01:58:22 +08:00
|
|
|
getset_inits.write(gen_template_rw_prop_init.substitute(name=self.name, member=pname))
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2013-06-13 04:02:30 +08:00
|
|
|
methods_code = StringIO()
|
|
|
|
methods_inits = StringIO()
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2013-06-13 04:02:30 +08:00
|
|
|
sorted_methods = list(self.methods.items())
|
2010-10-29 21:09:49 +08:00
|
|
|
sorted_methods.sort()
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2017-09-05 13:38:17 +08:00
|
|
|
if self.constructor is not None:
|
2017-09-05 21:10:36 +08:00
|
|
|
methods_code.write(self.constructor.gen_code(codegen))
|
2017-09-05 13:38:17 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
for mname, m in sorted_methods:
|
2017-09-05 21:10:36 +08:00
|
|
|
methods_code.write(m.gen_code(codegen))
|
2010-11-03 01:58:22 +08:00
|
|
|
methods_inits.write(m.get_tab_entry())
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2019-03-21 15:45:02 +08:00
|
|
|
code = gen_template_type_impl.substitute(name=self.name, wname=self.wname, cname=self.cname,
|
|
|
|
getset_code=getset_code.getvalue(), getset_inits=getset_inits.getvalue(),
|
|
|
|
methods_code=methods_code.getvalue(), methods_inits=methods_inits.getvalue())
|
|
|
|
|
|
|
|
return code
|
|
|
|
|
|
|
|
def gen_def(self, codegen):
|
|
|
|
all_classes = codegen.classes
|
|
|
|
baseptr = "NoBase"
|
2015-03-26 16:22:52 +08:00
|
|
|
if self.base and self.base in all_classes:
|
2019-03-21 15:45:02 +08:00
|
|
|
baseptr = all_classes[self.base].name
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2017-09-05 13:38:17 +08:00
|
|
|
constructor_name = "0"
|
|
|
|
if self.constructor is not None:
|
|
|
|
constructor_name = self.constructor.get_wrapper_name()
|
|
|
|
|
2021-03-31 04:54:11 +08:00
|
|
|
return "CVPY_TYPE({}, {}, {}, {}, {}, {});\n".format(
|
|
|
|
self.wname,
|
2019-03-21 15:45:02 +08:00
|
|
|
self.name,
|
|
|
|
self.cname if self.issimple else "Ptr<{}>".format(self.cname),
|
|
|
|
self.sname if self.issimple else "Ptr",
|
|
|
|
baseptr,
|
|
|
|
constructor_name
|
|
|
|
)
|
2012-05-31 19:04:11 +08:00
|
|
|
|
|
|
|
|
2014-05-12 18:58:54 +08:00
|
|
|
def handle_ptr(tp):
|
|
|
|
if tp.startswith('Ptr_'):
|
|
|
|
tp = 'Ptr<' + "::".join(tp.split('_')[1:]) + '>'
|
|
|
|
return tp
|
|
|
|
|
|
|
|
|
2020-12-18 20:00:42 +08:00
|
|
|
def get_named_params_info(all_classes, args):
|
|
|
|
extra_named_params = []
|
|
|
|
params_arg_name = ""
|
|
|
|
if args:
|
|
|
|
last_arg = args[-1]
|
|
|
|
if ("Params" in last_arg.tp) and (last_arg.tp in all_classes):
|
|
|
|
arg_classinfo = all_classes[last_arg.tp]
|
|
|
|
if arg_classinfo.isparams:
|
|
|
|
params_arg_name = last_arg.name
|
|
|
|
extra_named_params = arg_classinfo.props
|
|
|
|
return (params_arg_name, extra_named_params)
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
class ArgInfo(object):
|
|
|
|
def __init__(self, arg_tuple):
|
2014-05-12 18:58:54 +08:00
|
|
|
self.tp = handle_ptr(arg_tuple[0])
|
2010-10-29 21:09:49 +08:00
|
|
|
self.name = arg_tuple[1]
|
2021-08-01 16:59:16 +08:00
|
|
|
if self.name in python_reserved_keywords:
|
|
|
|
self.name += "_"
|
2010-10-29 21:09:49 +08:00
|
|
|
self.defval = arg_tuple[2]
|
|
|
|
self.isarray = False
|
|
|
|
self.arraylen = 0
|
|
|
|
self.arraycvt = None
|
|
|
|
self.inputarg = True
|
|
|
|
self.outputarg = False
|
2011-07-14 22:13:10 +08:00
|
|
|
self.returnarg = False
|
2020-07-29 21:18:52 +08:00
|
|
|
self.isrvalueref = False
|
2010-10-29 21:09:49 +08:00
|
|
|
for m in arg_tuple[3]:
|
|
|
|
if m == "/O":
|
|
|
|
self.inputarg = False
|
|
|
|
self.outputarg = True
|
2011-07-14 22:13:10 +08:00
|
|
|
self.returnarg = True
|
2010-10-29 21:09:49 +08:00
|
|
|
elif m == "/IO":
|
|
|
|
self.inputarg = True
|
|
|
|
self.outputarg = True
|
2013-03-15 20:55:58 +08:00
|
|
|
self.returnarg = True
|
2010-10-29 21:09:49 +08:00
|
|
|
elif m.startswith("/A"):
|
|
|
|
self.isarray = True
|
|
|
|
self.arraylen = m[2:].strip()
|
|
|
|
elif m.startswith("/CA"):
|
|
|
|
self.isarray = True
|
|
|
|
self.arraycvt = m[2:].strip()
|
2020-07-29 21:18:52 +08:00
|
|
|
elif m == "/RRef":
|
|
|
|
self.isrvalueref = True
|
2010-10-29 21:09:49 +08:00
|
|
|
self.py_inputarg = False
|
|
|
|
self.py_outputarg = False
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
def isbig(self):
|
2019-12-17 02:38:58 +08:00
|
|
|
return self.tp in ["Mat", "vector_Mat", "cuda::GpuMat", "GpuMat", "vector_GpuMat", "UMat", "vector_UMat"] # or self.tp.startswith("vector")
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2012-08-08 02:03:17 +08:00
|
|
|
def crepr(self):
|
|
|
|
return "ArgInfo(\"%s\", %d)" % (self.name, self.outputarg)
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
|
|
|
|
class FuncVariant(object):
|
2020-12-18 20:00:42 +08:00
|
|
|
def __init__(self, all_classes, classname, name, decl, isconstructor, isphantom=False):
|
2010-11-03 01:58:22 +08:00
|
|
|
self.classname = classname
|
|
|
|
self.name = self.wname = name
|
2010-10-29 21:09:49 +08:00
|
|
|
self.isconstructor = isconstructor
|
2018-08-19 01:34:47 +08:00
|
|
|
self.isphantom = isphantom
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2017-06-12 10:18:00 +08:00
|
|
|
self.docstring = decl[5]
|
|
|
|
|
|
|
|
self.rettype = decl[4] or handle_ptr(decl[1])
|
2010-10-29 21:09:49 +08:00
|
|
|
if self.rettype == "void":
|
|
|
|
self.rettype = ""
|
|
|
|
self.args = []
|
|
|
|
self.array_counters = {}
|
|
|
|
for a in decl[3]:
|
|
|
|
ainfo = ArgInfo(a)
|
|
|
|
if ainfo.isarray and not ainfo.arraycvt:
|
|
|
|
c = ainfo.arraylen
|
|
|
|
c_arrlist = self.array_counters.get(c, [])
|
|
|
|
if c_arrlist:
|
|
|
|
c_arrlist.append(ainfo.name)
|
|
|
|
else:
|
|
|
|
self.array_counters[c] = [ainfo.name]
|
|
|
|
self.args.append(ainfo)
|
2020-12-18 20:00:42 +08:00
|
|
|
self.init_pyproto(all_classes)
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2020-12-18 20:00:42 +08:00
|
|
|
def init_pyproto(self, all_classes):
|
2010-10-29 21:09:49 +08:00
|
|
|
# string representation of argument list, with '[', ']' symbols denoting optional arguments, e.g.
|
|
|
|
# "src1, src2[, dst[, mask]]" for cv.add
|
|
|
|
argstr = ""
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
# list of all input arguments of the Python function, with the argument numbers:
|
|
|
|
# [("src1", 0), ("src2", 1), ("dst", 2), ("mask", 3)]
|
|
|
|
# we keep an argument number to find the respective argument quickly, because
|
|
|
|
# some of the arguments of C function may not present in the Python function (such as array counters)
|
|
|
|
# or even go in a different order ("heavy" output parameters of the C function
|
|
|
|
# become the first optional input parameters of the Python function, and thus they are placed right after
|
2012-05-31 19:04:11 +08:00
|
|
|
# non-optional input parameters)
|
2010-10-29 21:09:49 +08:00
|
|
|
arglist = []
|
2020-12-18 20:00:42 +08:00
|
|
|
proto_arglist = []
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
# the list of "heavy" output parameters. Heavy parameters are the parameters
|
|
|
|
# that can be expensive to allocate each time, such as vectors and matrices (see isbig).
|
|
|
|
outarr_list = []
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
# the list of output parameters. Also includes input/output parameters.
|
|
|
|
outlist = []
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
firstoptarg = 1000000
|
2010-10-29 21:09:49 +08:00
|
|
|
argno = -1
|
|
|
|
for a in self.args:
|
|
|
|
argno += 1
|
|
|
|
if a.name in self.array_counters:
|
|
|
|
continue
|
2018-09-28 21:53:05 +08:00
|
|
|
assert not a.tp in forbidden_arg_types, 'Forbidden type "{}" for argument "{}" in "{}" ("{}")'.format(a.tp, a.name, self.name, self.classname)
|
2013-04-09 17:10:54 +08:00
|
|
|
if a.tp in ignored_arg_types:
|
|
|
|
continue
|
2011-07-14 22:13:10 +08:00
|
|
|
if a.returnarg:
|
2010-10-29 21:09:49 +08:00
|
|
|
outlist.append((a.name, argno))
|
2013-03-15 20:55:58 +08:00
|
|
|
if (not a.inputarg) and a.isbig():
|
2011-08-15 03:46:39 +08:00
|
|
|
outarr_list.append((a.name, argno))
|
2011-08-31 20:36:19 +08:00
|
|
|
continue
|
2011-08-15 03:46:39 +08:00
|
|
|
if not a.inputarg:
|
2010-10-29 21:09:49 +08:00
|
|
|
continue
|
|
|
|
if not a.defval:
|
|
|
|
arglist.append((a.name, argno))
|
2020-12-18 20:00:42 +08:00
|
|
|
proto_arglist.append((a.name, argno))
|
2010-10-29 21:09:49 +08:00
|
|
|
else:
|
2010-11-03 01:58:22 +08:00
|
|
|
firstoptarg = min(firstoptarg, len(arglist))
|
2010-10-29 21:09:49 +08:00
|
|
|
# if there are some array output parameters before the first default parameter, they
|
|
|
|
# are added as optional parameters before the first optional parameter
|
|
|
|
if outarr_list:
|
|
|
|
arglist += outarr_list
|
2020-12-18 20:00:42 +08:00
|
|
|
proto_arglist += [(aname_+"=None",argno_) for (aname_, argno_) in outarr_list]
|
2010-10-29 21:09:49 +08:00
|
|
|
outarr_list = []
|
|
|
|
arglist.append((a.name, argno))
|
2020-12-18 20:00:42 +08:00
|
|
|
proto_arglist.append((a.name+"="+a.defval, argno))
|
|
|
|
|
|
|
|
# exclude "params" from Python func parameters ==>
|
|
|
|
params_arg_name, extra_named_params = get_named_params_info(all_classes, self.args)
|
|
|
|
if params_arg_name:
|
|
|
|
arglist = arglist[:-1]
|
|
|
|
proto_arglist = proto_arglist[:-1]
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
if outarr_list:
|
2010-11-03 01:58:22 +08:00
|
|
|
firstoptarg = min(firstoptarg, len(arglist))
|
2020-12-18 20:00:42 +08:00
|
|
|
proto_arglist += [(aname+"=None",argno) for (aname, argno) in outarr_list]
|
2010-10-29 21:09:49 +08:00
|
|
|
arglist += outarr_list
|
2020-12-18 20:00:42 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
firstoptarg = min(firstoptarg, len(arglist))
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2020-12-18 20:00:42 +08:00
|
|
|
argnamelist = [aname for aname, argno in proto_arglist]
|
|
|
|
noptargs = len(argnamelist) - firstoptarg
|
|
|
|
if params_arg_name:
|
|
|
|
# ==> instead, add the individual parameters one by one
|
|
|
|
argnamelist += ["%s=%s" % (a.name, a.defval) for a in extra_named_params]
|
2010-10-29 21:09:49 +08:00
|
|
|
argstr = ", ".join(argnamelist[:firstoptarg])
|
2020-12-18 20:00:42 +08:00
|
|
|
argstr += "[, " + (", ".join(argnamelist[firstoptarg:]))
|
|
|
|
argstr += "]"
|
2010-10-29 21:09:49 +08:00
|
|
|
if self.rettype:
|
|
|
|
outlist = [("retval", -1)] + outlist
|
|
|
|
elif self.isconstructor:
|
|
|
|
assert outlist == []
|
|
|
|
outlist = [("self", -1)]
|
2010-11-03 01:58:22 +08:00
|
|
|
if self.isconstructor:
|
|
|
|
classname = self.classname
|
|
|
|
if classname.startswith("Cv"):
|
|
|
|
classname=classname[2:]
|
|
|
|
outstr = "<%s object>" % (classname,)
|
|
|
|
elif outlist:
|
2010-10-29 21:09:49 +08:00
|
|
|
outstr = ", ".join([o[0] for o in outlist])
|
|
|
|
else:
|
|
|
|
outstr = "None"
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2017-09-05 21:10:36 +08:00
|
|
|
self.py_arg_str = argstr
|
|
|
|
self.py_return_str = outstr
|
2017-06-12 10:18:00 +08:00
|
|
|
self.py_prototype = "%s(%s) -> %s" % (self.wname, argstr, outstr)
|
2010-10-29 21:09:49 +08:00
|
|
|
self.py_noptargs = noptargs
|
|
|
|
self.py_arglist = arglist
|
|
|
|
for aname, argno in arglist:
|
|
|
|
self.args[argno].py_inputarg = True
|
|
|
|
for aname, argno in outlist:
|
|
|
|
if argno >= 0:
|
|
|
|
self.args[argno].py_outputarg = True
|
|
|
|
self.py_outlist = outlist
|
|
|
|
|
|
|
|
|
|
|
|
class FuncInfo(object):
|
2018-08-30 21:49:35 +08:00
|
|
|
def __init__(self, classname, name, cname, isconstructor, namespace, is_static):
|
2010-10-29 21:09:49 +08:00
|
|
|
self.classname = classname
|
|
|
|
self.name = name
|
|
|
|
self.cname = cname
|
|
|
|
self.isconstructor = isconstructor
|
2014-08-16 00:44:32 +08:00
|
|
|
self.namespace = namespace
|
2018-08-30 21:49:35 +08:00
|
|
|
self.is_static = is_static
|
2010-10-29 21:09:49 +08:00
|
|
|
self.variants = []
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2020-12-18 20:00:42 +08:00
|
|
|
def add_variant(self, all_classes, decl, isphantom=False):
|
|
|
|
self.variants.append(FuncVariant(all_classes, self.classname, self.name, decl, self.isconstructor, isphantom))
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
def get_wrapper_name(self):
|
2010-11-03 01:58:22 +08:00
|
|
|
name = self.name
|
2010-10-29 21:09:49 +08:00
|
|
|
if self.classname:
|
2010-11-03 01:58:22 +08:00
|
|
|
classname = self.classname + "_"
|
|
|
|
if "[" in name:
|
|
|
|
name = "getelem"
|
2010-10-29 21:09:49 +08:00
|
|
|
else:
|
2010-11-03 01:58:22 +08:00
|
|
|
classname = ""
|
2017-09-05 13:38:17 +08:00
|
|
|
|
2018-08-30 21:49:35 +08:00
|
|
|
if self.is_static:
|
|
|
|
name += "_static"
|
2017-09-05 13:38:17 +08:00
|
|
|
|
2014-08-21 21:31:01 +08:00
|
|
|
return "pyopencv_" + self.namespace.replace('.','_') + '_' + classname + name
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2017-09-05 21:10:36 +08:00
|
|
|
def get_wrapper_prototype(self, codegen):
|
2010-10-29 21:09:49 +08:00
|
|
|
full_fname = self.get_wrapper_name()
|
2017-09-05 13:38:17 +08:00
|
|
|
if self.isconstructor:
|
2020-07-29 21:18:52 +08:00
|
|
|
return "static int {fn_name}(pyopencv_{type_name}_t* self, PyObject* py_args, PyObject* kw)".format(
|
2017-09-05 21:10:36 +08:00
|
|
|
fn_name=full_fname, type_name=codegen.classes[self.classname].name)
|
2017-09-05 13:38:17 +08:00
|
|
|
|
|
|
|
if self.classname:
|
2010-10-29 21:09:49 +08:00
|
|
|
self_arg = "self"
|
|
|
|
else:
|
|
|
|
self_arg = ""
|
2020-07-29 21:18:52 +08:00
|
|
|
return "static PyObject* %s(PyObject* %s, PyObject* py_args, PyObject* kw)" % (full_fname, self_arg)
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
def get_tab_entry(self):
|
2017-06-12 10:18:00 +08:00
|
|
|
prototype_list = []
|
2010-10-29 21:09:49 +08:00
|
|
|
docstring_list = []
|
2017-06-12 10:18:00 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
have_empty_constructor = False
|
|
|
|
for v in self.variants:
|
2017-06-12 10:18:00 +08:00
|
|
|
s = v.py_prototype
|
2010-10-29 21:09:49 +08:00
|
|
|
if (not v.py_arglist) and self.isconstructor:
|
|
|
|
have_empty_constructor = True
|
2017-06-12 10:18:00 +08:00
|
|
|
if s not in prototype_list:
|
|
|
|
prototype_list.append(s)
|
|
|
|
docstring_list.append(v.docstring)
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
# if there are just 2 constructors: default one and some other,
|
|
|
|
# we simplify the notation.
|
|
|
|
# Instead of ClassName(args ...) -> object or ClassName() -> object
|
|
|
|
# we write ClassName([args ...]) -> object
|
|
|
|
if have_empty_constructor and len(self.variants) == 2:
|
2010-11-03 01:58:22 +08:00
|
|
|
idx = self.variants[1].py_arglist != []
|
2017-06-12 10:18:00 +08:00
|
|
|
s = self.variants[idx].py_prototype
|
2011-06-30 06:06:42 +08:00
|
|
|
p1 = s.find("(")
|
|
|
|
p2 = s.rfind(")")
|
2017-06-12 10:18:00 +08:00
|
|
|
prototype_list = [s[:p1+1] + "[" + s[p1+1:p2] + "]" + s[p2:]]
|
|
|
|
|
|
|
|
# The final docstring will be: Each prototype, followed by
|
|
|
|
# their relevant doxygen comment
|
|
|
|
full_docstring = ""
|
|
|
|
for prototype, body in zip(prototype_list, docstring_list):
|
2020-12-18 20:00:42 +08:00
|
|
|
full_docstring += Template("$prototype\n$docstring").substitute(
|
|
|
|
prototype="\"" + prototype.replace("\\", "\\\\").replace("\"", "\\\"") + "\\n\\n\"",
|
|
|
|
docstring="\n".join(
|
|
|
|
[" \"" + line.replace("\\", "\\\\").replace("\"", "\\\"") + "\\n\""
|
2017-06-12 10:18:00 +08:00
|
|
|
for line in body.split('\n')]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Escape backslashes, newlines, and double quotes
|
2020-12-18 20:00:42 +08:00
|
|
|
#full_docstring = full_docstring.strip().replace("\\", "\\\\").replace("\"", "\\\"")
|
2017-06-12 10:18:00 +08:00
|
|
|
# Convert unicode chars to xml representation, but keep as string instead of bytes
|
|
|
|
full_docstring = full_docstring.encode('ascii', errors='xmlcharrefreplace').decode()
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2020-12-18 20:00:42 +08:00
|
|
|
return Template(' {"$py_funcname", CV_PY_FN_WITH_KW_($wrap_funcname, $flags),\n $py_docstring},\n'
|
2010-11-03 01:58:22 +08:00
|
|
|
).substitute(py_funcname = self.variants[0].wname, wrap_funcname=self.get_wrapper_name(),
|
2018-08-30 21:49:35 +08:00
|
|
|
flags = 'METH_STATIC' if self.is_static else '0', py_docstring = full_docstring)
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2017-09-05 21:10:36 +08:00
|
|
|
def gen_code(self, codegen):
|
|
|
|
all_classes = codegen.classes
|
|
|
|
proto = self.get_wrapper_prototype(codegen)
|
2010-10-29 21:09:49 +08:00
|
|
|
code = "%s\n{\n" % (proto,)
|
2014-08-16 00:44:32 +08:00
|
|
|
code += " using namespace %s;\n\n" % self.namespace.replace('.', '::')
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2019-03-21 15:45:02 +08:00
|
|
|
selfinfo = None
|
2010-10-29 21:09:49 +08:00
|
|
|
ismethod = self.classname != "" and not self.isconstructor
|
|
|
|
# full name is needed for error diagnostic in PyArg_ParseTupleAndKeywords
|
|
|
|
fullname = self.name
|
|
|
|
|
|
|
|
if self.classname:
|
|
|
|
selfinfo = all_classes[self.classname]
|
|
|
|
if not self.isconstructor:
|
2019-03-21 15:45:02 +08:00
|
|
|
if not self.is_static:
|
|
|
|
code += gen_template_check_self.substitute(
|
|
|
|
name=selfinfo.name,
|
|
|
|
cname=selfinfo.cname if selfinfo.issimple else "Ptr<{}>".format(selfinfo.cname),
|
|
|
|
pname=(selfinfo.cname + '*') if selfinfo.issimple else "Ptr<{}>".format(selfinfo.cname),
|
|
|
|
cvt='' if selfinfo.issimple else '*'
|
|
|
|
)
|
2010-10-29 21:09:49 +08:00
|
|
|
fullname = selfinfo.wname + "." + fullname
|
|
|
|
|
|
|
|
all_code_variants = []
|
2020-01-13 23:11:34 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
for v in self.variants:
|
|
|
|
code_decl = ""
|
|
|
|
code_ret = ""
|
|
|
|
code_cvt_list = []
|
|
|
|
|
2013-08-14 16:32:41 +08:00
|
|
|
code_args = "("
|
2010-10-29 21:09:49 +08:00
|
|
|
all_cargs = []
|
|
|
|
|
2018-08-30 21:49:35 +08:00
|
|
|
if v.isphantom and ismethod and not self.is_static:
|
2018-08-19 01:34:47 +08:00
|
|
|
code_args += "_self_"
|
|
|
|
|
2020-12-18 20:00:42 +08:00
|
|
|
params_arg_name, extra_named_params = get_named_params_info(all_classes, v.args)
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
# declare all the C function arguments,
|
|
|
|
# add necessary conversions from Python objects to code_cvt_list,
|
|
|
|
# form the function/method call,
|
|
|
|
# for the list of type mappings
|
|
|
|
for a in v.args:
|
2013-04-09 17:10:54 +08:00
|
|
|
if a.tp in ignored_arg_types:
|
|
|
|
defval = a.defval
|
|
|
|
if not defval and a.tp.endswith("*"):
|
2018-09-28 21:53:05 +08:00
|
|
|
defval = "0"
|
2013-04-09 17:10:54 +08:00
|
|
|
assert defval
|
2013-08-14 16:32:41 +08:00
|
|
|
if not code_args.endswith("("):
|
|
|
|
code_args += ", "
|
|
|
|
code_args += defval
|
2013-04-09 17:10:54 +08:00
|
|
|
all_cargs.append([[None, ""], ""])
|
|
|
|
continue
|
2010-10-29 21:09:49 +08:00
|
|
|
tp1 = tp = a.tp
|
|
|
|
amp = ""
|
|
|
|
defval0 = ""
|
2018-08-06 19:46:46 +08:00
|
|
|
if tp in pass_by_val_types:
|
2010-10-29 21:09:49 +08:00
|
|
|
tp = tp1 = tp[:-1]
|
|
|
|
amp = "&"
|
|
|
|
if tp.endswith("*"):
|
|
|
|
defval0 = "0"
|
|
|
|
tp1 = tp.replace("*", "_ptr")
|
2018-08-20 22:35:41 +08:00
|
|
|
tp_candidates = [a.tp, normalize_class_name(self.namespace + "." + a.tp)]
|
2018-09-28 14:39:51 +08:00
|
|
|
if any(tp in codegen.enums.keys() for tp in tp_candidates):
|
2018-08-20 22:35:41 +08:00
|
|
|
defval0 = "static_cast<%s>(%d)" % (a.tp, 0)
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2020-01-13 23:11:34 +08:00
|
|
|
arg_type_info = simple_argtype_mapping.get(tp, ArgTypeInfo(tp, FormatStrings.object, defval0, True))
|
2010-11-26 07:16:32 +08:00
|
|
|
parse_name = a.name
|
2010-10-29 21:09:49 +08:00
|
|
|
if a.py_inputarg:
|
2020-01-13 23:11:34 +08:00
|
|
|
if arg_type_info.strict_conversion:
|
2010-10-29 21:09:49 +08:00
|
|
|
code_decl += " PyObject* pyobj_%s = NULL;\n" % (a.name,)
|
2010-11-26 07:16:32 +08:00
|
|
|
parse_name = "pyobj_" + a.name
|
2013-04-12 21:39:16 +08:00
|
|
|
if a.tp == 'char':
|
2020-01-13 23:11:34 +08:00
|
|
|
code_cvt_list.append("convert_to_char(pyobj_%s, &%s, %s)" % (a.name, a.name, a.crepr()))
|
2013-04-12 21:39:16 +08:00
|
|
|
else:
|
2021-03-31 22:17:45 +08:00
|
|
|
code_cvt_list.append("pyopencv_to_safe(pyobj_%s, %s, %s)" % (a.name, a.name, a.crepr()))
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2020-01-13 23:11:34 +08:00
|
|
|
all_cargs.append([arg_type_info, parse_name])
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
defval = a.defval
|
|
|
|
if not defval:
|
2020-01-13 23:11:34 +08:00
|
|
|
defval = arg_type_info.default_value
|
2016-02-08 00:04:39 +08:00
|
|
|
else:
|
|
|
|
if "UMat" in tp:
|
|
|
|
if "Mat" in defval and "UMat" not in defval:
|
|
|
|
defval = defval.replace("Mat", "UMat")
|
2018-08-06 19:46:46 +08:00
|
|
|
if "cuda::GpuMat" in tp:
|
|
|
|
if "Mat" in defval and "GpuMat" not in defval:
|
|
|
|
defval = defval.replace("Mat", "cuda::GpuMat")
|
2010-10-29 21:09:49 +08:00
|
|
|
# "tp arg = tp();" is equivalent to "tp arg;" in the case of complex types
|
2020-01-13 23:11:34 +08:00
|
|
|
if defval == tp + "()" and arg_type_info.format_str == FormatStrings.object:
|
2010-10-29 21:09:49 +08:00
|
|
|
defval = ""
|
2010-11-03 01:58:22 +08:00
|
|
|
if a.outputarg and not a.inputarg:
|
|
|
|
defval = ""
|
2010-10-29 21:09:49 +08:00
|
|
|
if defval:
|
2020-01-13 23:11:34 +08:00
|
|
|
code_decl += " %s %s=%s;\n" % (arg_type_info.atype, a.name, defval)
|
2010-10-29 21:09:49 +08:00
|
|
|
else:
|
2020-01-13 23:11:34 +08:00
|
|
|
code_decl += " %s %s;\n" % (arg_type_info.atype, a.name)
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2013-08-14 16:32:41 +08:00
|
|
|
if not code_args.endswith("("):
|
|
|
|
code_args += ", "
|
2020-07-29 21:18:52 +08:00
|
|
|
|
|
|
|
if a.isrvalueref:
|
|
|
|
a.name = 'std::move(' + a.name + ')'
|
|
|
|
|
2013-08-14 16:32:41 +08:00
|
|
|
code_args += amp + a.name
|
|
|
|
|
2020-12-18 20:00:42 +08:00
|
|
|
if params_arg_name:
|
|
|
|
for a in extra_named_params:
|
|
|
|
code_decl += " PyObject* pyobj_kw_%s = NULL;\n" % (a.name,)
|
|
|
|
ainfo = "ArgInfo(\"params.%s\", %d)" % (a.name, 0)
|
|
|
|
if a.tp == 'char':
|
|
|
|
code_cvt_list.append("convert_to_char(pyobj_kw_%s, ¶ms.%s, %s)" % (a.name, a.name, ainfo))
|
|
|
|
else:
|
|
|
|
code_cvt_list.append("pyopencv_to(pyobj_kw_%s, params.%s, %s)" % (a.name, a.name, ainfo))
|
|
|
|
|
2013-08-14 16:32:41 +08:00
|
|
|
code_args += ")"
|
|
|
|
|
|
|
|
if self.isconstructor:
|
|
|
|
if selfinfo.issimple:
|
|
|
|
templ_prelude = gen_template_simple_call_constructor_prelude
|
|
|
|
templ = gen_template_simple_call_constructor
|
|
|
|
else:
|
|
|
|
templ_prelude = gen_template_call_constructor_prelude
|
|
|
|
templ = gen_template_call_constructor
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2013-08-14 16:32:41 +08:00
|
|
|
code_prelude = templ_prelude.substitute(name=selfinfo.name, cname=selfinfo.cname)
|
2020-07-29 21:18:52 +08:00
|
|
|
code_fcall = templ.substitute(name=selfinfo.name, cname=selfinfo.cname, py_args=code_args)
|
2018-08-19 01:34:47 +08:00
|
|
|
if v.isphantom:
|
|
|
|
code_fcall = code_fcall.replace("new " + selfinfo.cname, self.cname.replace("::", "_"))
|
2013-08-14 16:32:41 +08:00
|
|
|
else:
|
|
|
|
code_prelude = ""
|
|
|
|
code_fcall = ""
|
|
|
|
if v.rettype:
|
|
|
|
code_decl += " " + v.rettype + " retval;\n"
|
|
|
|
code_fcall += "retval = "
|
2018-08-25 13:15:50 +08:00
|
|
|
if not v.isphantom and ismethod and not self.is_static:
|
2013-08-14 16:32:41 +08:00
|
|
|
code_fcall += "_self_->" + self.cname
|
|
|
|
else:
|
|
|
|
code_fcall += self.cname
|
|
|
|
code_fcall += code_args
|
2010-10-29 21:09:49 +08:00
|
|
|
|
|
|
|
if code_cvt_list:
|
|
|
|
code_cvt_list = [""] + code_cvt_list
|
|
|
|
|
|
|
|
# add info about return value, if any, to all_cargs. if there non-void return value,
|
|
|
|
# it is encoded in v.py_outlist as ("retval", -1) pair.
|
|
|
|
# As [-1] in Python accesses the last element of a list, we automatically handle the return value by
|
|
|
|
# adding the necessary info to the end of all_cargs list.
|
|
|
|
if v.rettype:
|
|
|
|
tp = v.rettype
|
|
|
|
tp1 = tp.replace("*", "_ptr")
|
2020-01-13 23:11:34 +08:00
|
|
|
default_info = ArgTypeInfo(tp, FormatStrings.object, "0")
|
|
|
|
arg_type_info = simple_argtype_mapping.get(tp, default_info)
|
|
|
|
all_cargs.append(arg_type_info)
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2011-08-10 18:28:01 +08:00
|
|
|
if v.args and v.py_arglist:
|
2010-10-29 21:09:49 +08:00
|
|
|
# form the format spec for PyArg_ParseTupleAndKeywords
|
2020-01-13 23:11:34 +08:00
|
|
|
fmtspec = "".join([
|
|
|
|
get_type_format_string(all_cargs[argno][0])
|
|
|
|
for aname, argno in v.py_arglist
|
|
|
|
])
|
2010-10-29 21:09:49 +08:00
|
|
|
if v.py_noptargs > 0:
|
|
|
|
fmtspec = fmtspec[:-v.py_noptargs] + "|" + fmtspec[-v.py_noptargs:]
|
2020-12-18 20:00:42 +08:00
|
|
|
fmtspec += "O" * len(extra_named_params)
|
2010-10-29 21:09:49 +08:00
|
|
|
fmtspec += ":" + fullname
|
|
|
|
|
|
|
|
# form the argument parse code that:
|
|
|
|
# - declares the list of keyword parameters
|
|
|
|
# - calls PyArg_ParseTupleAndKeywords
|
|
|
|
# - converts complex arguments from PyObject's to native OpenCV types
|
2020-12-18 20:00:42 +08:00
|
|
|
kw_list = ['"' + aname + '"' for aname, argno in v.py_arglist]
|
|
|
|
kw_list += ['"' + a.name + '"' for a in extra_named_params]
|
|
|
|
parse_arglist = ["&" + all_cargs[argno][1] for aname, argno in v.py_arglist]
|
|
|
|
parse_arglist += ["&pyobj_kw_" + a.name for a in extra_named_params]
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
code_parse = gen_template_parse_args.substitute(
|
2020-12-18 20:00:42 +08:00
|
|
|
kw_list = ", ".join(kw_list),
|
2010-10-29 21:09:49 +08:00
|
|
|
fmtspec = fmtspec,
|
2020-12-18 20:00:42 +08:00
|
|
|
parse_arglist = ", ".join(parse_arglist),
|
2010-10-29 21:09:49 +08:00
|
|
|
code_cvt = " &&\n ".join(code_cvt_list))
|
|
|
|
else:
|
2020-07-29 21:18:52 +08:00
|
|
|
code_parse = "if(PyObject_Size(py_args) == 0 && (!kw || PyObject_Size(kw) == 0))"
|
2010-10-29 21:09:49 +08:00
|
|
|
|
|
|
|
if len(v.py_outlist) == 0:
|
2010-11-03 01:58:22 +08:00
|
|
|
code_ret = "Py_RETURN_NONE"
|
2010-10-29 21:09:49 +08:00
|
|
|
elif len(v.py_outlist) == 1:
|
|
|
|
if self.isconstructor:
|
2017-09-05 13:38:17 +08:00
|
|
|
code_ret = "return 0"
|
2010-10-29 21:09:49 +08:00
|
|
|
else:
|
|
|
|
aname, argno = v.py_outlist[0]
|
2010-11-03 01:58:22 +08:00
|
|
|
code_ret = "return pyopencv_from(%s)" % (aname,)
|
2010-10-29 21:09:49 +08:00
|
|
|
else:
|
Fix modules/ typos
Found using `codespell -q 3 -S ./3rdparty -L activ,amin,ang,atleast,childs,dof,endwhile,halfs,hist,iff,nd,od,uint`
2019-08-16 06:02:09 +08:00
|
|
|
# there is more than 1 return parameter; form the tuple out of them
|
2010-10-29 21:09:49 +08:00
|
|
|
fmtspec = "N"*len(v.py_outlist)
|
2010-11-03 01:58:22 +08:00
|
|
|
code_ret = "return Py_BuildValue(\"(%s)\", %s)" % \
|
2012-05-31 19:04:11 +08:00
|
|
|
(fmtspec, ", ".join(["pyopencv_from(" + aname + ")" for aname, argno in v.py_outlist]))
|
2010-10-29 21:09:49 +08:00
|
|
|
|
|
|
|
all_code_variants.append(gen_template_func_body.substitute(code_decl=code_decl,
|
2013-08-14 16:32:41 +08:00
|
|
|
code_parse=code_parse, code_prelude=code_prelude, code_fcall=code_fcall, code_ret=code_ret))
|
2010-10-29 21:09:49 +08:00
|
|
|
|
|
|
|
if len(all_code_variants)==1:
|
|
|
|
# if the function/method has only 1 signature, then just put it
|
|
|
|
code += all_code_variants[0]
|
|
|
|
else:
|
2021-01-12 22:50:07 +08:00
|
|
|
# try to execute each signature, add an interlude between function
|
|
|
|
# calls to collect error from all conversions
|
|
|
|
code += ' pyPrepareArgumentConversionErrorsStorage({});\n'.format(len(all_code_variants))
|
|
|
|
code += ' \n'.join(gen_template_overloaded_function_call.substitute(variant=v)
|
|
|
|
for v in all_code_variants)
|
|
|
|
code += ' pyRaiseCVOverloadException("{}");\n'.format(self.name)
|
2017-09-05 13:38:17 +08:00
|
|
|
|
|
|
|
def_ret = "NULL"
|
|
|
|
if self.isconstructor:
|
|
|
|
def_ret = "-1"
|
|
|
|
code += "\n return %s;\n}\n\n" % def_ret
|
2017-09-05 21:10:36 +08:00
|
|
|
|
|
|
|
cname = self.cname
|
2017-11-04 11:38:18 +08:00
|
|
|
classinfo = None
|
|
|
|
#dump = False
|
|
|
|
#if dump: pprint(vars(self))
|
|
|
|
#if dump: pprint(vars(self.variants[0]))
|
2017-09-05 21:10:36 +08:00
|
|
|
if self.classname:
|
|
|
|
classinfo = all_classes[self.classname]
|
2017-11-04 11:38:18 +08:00
|
|
|
#if dump: pprint(vars(classinfo))
|
|
|
|
if self.isconstructor:
|
|
|
|
py_name = 'cv.' + classinfo.wname
|
2018-08-30 21:49:35 +08:00
|
|
|
elif self.is_static:
|
2017-11-04 11:38:18 +08:00
|
|
|
py_name = '.'.join([self.namespace, classinfo.sname + '_' + self.variants[0].wname])
|
|
|
|
else:
|
|
|
|
cname = classinfo.cname + '::' + cname
|
|
|
|
py_name = 'cv.' + classinfo.wname + '.' + self.variants[0].wname
|
|
|
|
else:
|
|
|
|
py_name = '.'.join([self.namespace, self.variants[0].wname])
|
|
|
|
#if dump: print(cname + " => " + py_name)
|
2017-09-05 21:10:36 +08:00
|
|
|
py_signatures = codegen.py_signatures.setdefault(cname, [])
|
|
|
|
for v in self.variants:
|
2017-11-04 11:38:18 +08:00
|
|
|
s = dict(name=py_name, arg=v.py_arg_str, ret=v.py_return_str)
|
2017-09-05 21:10:36 +08:00
|
|
|
for old in py_signatures:
|
|
|
|
if s == old:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
py_signatures.append(s)
|
|
|
|
|
2012-05-31 19:04:11 +08:00
|
|
|
return code
|
|
|
|
|
|
|
|
|
2014-08-18 20:52:01 +08:00
|
|
|
class Namespace(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.funcs = {}
|
|
|
|
self.consts = {}
|
|
|
|
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
class PythonWrapperGenerator(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.clear()
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
def clear(self):
|
|
|
|
self.classes = {}
|
2014-08-18 20:52:01 +08:00
|
|
|
self.namespaces = {}
|
2010-10-29 21:09:49 +08:00
|
|
|
self.consts = {}
|
2018-09-28 14:39:51 +08:00
|
|
|
self.enums = {}
|
2014-05-12 18:58:54 +08:00
|
|
|
self.code_include = StringIO()
|
2018-09-28 14:39:51 +08:00
|
|
|
self.code_enums = StringIO()
|
2013-06-13 04:02:30 +08:00
|
|
|
self.code_types = StringIO()
|
|
|
|
self.code_funcs = StringIO()
|
2014-08-14 23:01:49 +08:00
|
|
|
self.code_ns_reg = StringIO()
|
2019-03-21 15:45:02 +08:00
|
|
|
self.code_ns_init = StringIO()
|
2017-09-05 13:38:17 +08:00
|
|
|
self.code_type_publish = StringIO()
|
2017-09-05 21:10:36 +08:00
|
|
|
self.py_signatures = dict()
|
2011-06-10 21:22:40 +08:00
|
|
|
self.class_idx = 0
|
2010-10-29 21:09:49 +08:00
|
|
|
|
|
|
|
def add_class(self, stype, name, decl):
|
|
|
|
classinfo = ClassInfo(name, decl)
|
2011-06-10 21:22:40 +08:00
|
|
|
classinfo.decl_idx = self.class_idx
|
|
|
|
self.class_idx += 1
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2013-06-13 04:02:30 +08:00
|
|
|
if classinfo.name in self.classes:
|
|
|
|
print("Generator error: class %s (cname=%s) already exists" \
|
|
|
|
% (classinfo.name, classinfo.cname))
|
2012-05-31 19:04:11 +08:00
|
|
|
sys.exit(-1)
|
2010-10-29 21:09:49 +08:00
|
|
|
self.classes[classinfo.name] = classinfo
|
2014-08-21 18:51:03 +08:00
|
|
|
|
2017-11-04 11:38:18 +08:00
|
|
|
# Add Class to json file.
|
|
|
|
namespace, classes, name = self.split_decl_name(name)
|
|
|
|
namespace = '.'.join(namespace)
|
|
|
|
name = '_'.join(classes+[name])
|
|
|
|
|
|
|
|
py_name = 'cv.' + classinfo.wname # use wrapper name
|
|
|
|
py_signatures = self.py_signatures.setdefault(classinfo.cname, [])
|
|
|
|
py_signatures.append(dict(name=py_name))
|
|
|
|
#print('class: ' + classinfo.cname + " => " + py_name)
|
|
|
|
|
2014-08-19 17:35:46 +08:00
|
|
|
def split_decl_name(self, name):
|
|
|
|
chunks = name.split('.')
|
|
|
|
namespace = chunks[:-1]
|
|
|
|
classes = []
|
|
|
|
while namespace and '.'.join(namespace) not in self.parser.namespaces:
|
|
|
|
classes.insert(0, namespace.pop())
|
|
|
|
return namespace, classes, chunks[-1]
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2014-08-19 17:35:46 +08:00
|
|
|
|
|
|
|
def add_const(self, name, decl):
|
|
|
|
cname = name.replace('.','::')
|
|
|
|
namespace, classes, name = self.split_decl_name(name)
|
|
|
|
namespace = '.'.join(namespace)
|
|
|
|
name = '_'.join(classes+[name])
|
|
|
|
ns = self.namespaces.setdefault(namespace, Namespace())
|
|
|
|
if name in ns.consts:
|
2013-06-13 04:02:30 +08:00
|
|
|
print("Generator error: constant %s (cname=%s) already exists" \
|
2014-08-19 17:35:46 +08:00
|
|
|
% (name, cname))
|
2012-05-31 19:04:11 +08:00
|
|
|
sys.exit(-1)
|
2014-08-19 17:35:46 +08:00
|
|
|
ns.consts[name] = cname
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2017-11-04 11:38:18 +08:00
|
|
|
value = decl[1]
|
|
|
|
py_name = '.'.join([namespace, name])
|
|
|
|
py_signatures = self.py_signatures.setdefault(cname, [])
|
|
|
|
py_signatures.append(dict(name=py_name, value=value))
|
|
|
|
#print(cname + ' => ' + str(py_name) + ' (value=' + value + ')')
|
|
|
|
|
2018-08-20 22:35:41 +08:00
|
|
|
def add_enum(self, name, decl):
|
2018-09-28 14:39:51 +08:00
|
|
|
wname = normalize_class_name(name)
|
|
|
|
if wname.endswith("<unnamed>"):
|
|
|
|
wname = None
|
2018-08-28 03:38:26 +08:00
|
|
|
else:
|
2018-09-28 14:39:51 +08:00
|
|
|
self.enums[wname] = name
|
2018-08-26 17:38:07 +08:00
|
|
|
const_decls = decl[3]
|
|
|
|
|
|
|
|
for decl in const_decls:
|
|
|
|
name = decl[0]
|
|
|
|
self.add_const(name.replace("const ", "").strip(), decl)
|
2018-08-20 22:35:41 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
def add_func(self, decl):
|
2014-08-19 17:35:46 +08:00
|
|
|
namespace, classes, barename = self.split_decl_name(decl[0])
|
|
|
|
cname = "::".join(namespace+classes+[barename])
|
|
|
|
name = barename
|
2014-08-18 20:15:30 +08:00
|
|
|
classname = ''
|
|
|
|
bareclassname = ''
|
|
|
|
if classes:
|
|
|
|
classname = normalize_class_name('.'.join(namespace+classes))
|
|
|
|
bareclassname = classes[-1]
|
2021-03-31 04:54:11 +08:00
|
|
|
namespace_str = '.'.join(namespace)
|
2014-08-18 20:15:30 +08:00
|
|
|
|
2014-08-14 23:01:49 +08:00
|
|
|
isconstructor = name == bareclassname
|
2018-08-30 21:49:35 +08:00
|
|
|
is_static = False
|
2018-08-19 01:34:47 +08:00
|
|
|
isphantom = False
|
|
|
|
mappable = None
|
2010-10-29 21:09:49 +08:00
|
|
|
for m in decl[2]:
|
|
|
|
if m == "/S":
|
2018-08-30 21:49:35 +08:00
|
|
|
is_static = True
|
2018-08-19 01:34:47 +08:00
|
|
|
elif m == "/phantom":
|
|
|
|
isphantom = True
|
|
|
|
cname = cname.replace("::", "_")
|
2010-10-29 21:09:49 +08:00
|
|
|
elif m.startswith("="):
|
|
|
|
name = m[1:]
|
2018-08-19 01:34:47 +08:00
|
|
|
elif m.startswith("/mappable="):
|
|
|
|
mappable = m[10:]
|
|
|
|
self.classes[classname].mappables.append(mappable)
|
|
|
|
return
|
|
|
|
|
2017-09-05 13:38:17 +08:00
|
|
|
if isconstructor:
|
2014-08-18 20:15:30 +08:00
|
|
|
name = "_".join(classes[:-1]+[name])
|
2014-08-14 23:01:49 +08:00
|
|
|
|
2018-08-30 21:49:35 +08:00
|
|
|
if is_static:
|
2017-09-05 13:38:17 +08:00
|
|
|
# Add it as a method to the class
|
2014-08-16 00:44:32 +08:00
|
|
|
func_map = self.classes[classname].methods
|
2021-03-31 04:54:11 +08:00
|
|
|
func = func_map.setdefault(name, FuncInfo(classname, name, cname, isconstructor, namespace_str, is_static))
|
2020-12-18 20:00:42 +08:00
|
|
|
func.add_variant(self.classes, decl, isphantom)
|
2017-09-05 13:38:17 +08:00
|
|
|
|
|
|
|
# Add it as global function
|
|
|
|
g_name = "_".join(classes+[name])
|
2021-03-31 04:54:11 +08:00
|
|
|
w_classes = []
|
|
|
|
for i in range(0, len(classes)):
|
|
|
|
classes_i = classes[:i+1]
|
|
|
|
classname_i = normalize_class_name('.'.join(namespace+classes_i))
|
|
|
|
w_classname = self.classes[classname_i].wname
|
|
|
|
namespace_prefix = normalize_class_name('.'.join(namespace)) + '_'
|
|
|
|
if w_classname.startswith(namespace_prefix):
|
|
|
|
w_classname = w_classname[len(namespace_prefix):]
|
|
|
|
w_classes.append(w_classname)
|
|
|
|
g_wname = "_".join(w_classes+[name])
|
|
|
|
func_map = self.namespaces.setdefault(namespace_str, Namespace()).funcs
|
|
|
|
func = func_map.setdefault(g_name, FuncInfo("", g_name, cname, isconstructor, namespace_str, False))
|
2020-12-18 20:00:42 +08:00
|
|
|
func.add_variant(self.classes, decl, isphantom)
|
2021-03-31 04:54:11 +08:00
|
|
|
if g_wname != g_name: # TODO OpenCV 5.0
|
|
|
|
wfunc = func_map.setdefault(g_wname, FuncInfo("", g_wname, cname, isconstructor, namespace_str, False))
|
2021-04-09 18:30:38 +08:00
|
|
|
wfunc.add_variant(self.classes, decl, isphantom)
|
2017-09-05 13:38:17 +08:00
|
|
|
else:
|
|
|
|
if classname and not isconstructor:
|
2018-08-25 13:15:50 +08:00
|
|
|
if not isphantom:
|
|
|
|
cname = barename
|
2017-09-05 13:38:17 +08:00
|
|
|
func_map = self.classes[classname].methods
|
|
|
|
else:
|
2021-03-31 04:54:11 +08:00
|
|
|
func_map = self.namespaces.setdefault(namespace_str, Namespace()).funcs
|
2017-09-05 13:38:17 +08:00
|
|
|
|
2021-03-31 04:54:11 +08:00
|
|
|
func = func_map.setdefault(name, FuncInfo(classname, name, cname, isconstructor, namespace_str, is_static))
|
2020-12-18 20:00:42 +08:00
|
|
|
func.add_variant(self.classes, decl, isphantom)
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2017-09-05 13:38:17 +08:00
|
|
|
if classname and isconstructor:
|
|
|
|
self.classes[classname].constructor = func
|
2012-05-31 19:04:11 +08:00
|
|
|
|
|
|
|
|
2014-08-14 23:01:49 +08:00
|
|
|
def gen_namespace(self, ns_name):
|
2014-08-19 17:35:46 +08:00
|
|
|
ns = self.namespaces[ns_name]
|
2014-08-14 23:01:49 +08:00
|
|
|
wname = normalize_class_name(ns_name)
|
2014-08-19 17:35:46 +08:00
|
|
|
|
2014-08-14 23:01:49 +08:00
|
|
|
self.code_ns_reg.write('static PyMethodDef methods_%s[] = {\n'%wname)
|
2014-08-19 17:35:46 +08:00
|
|
|
for name, func in sorted(ns.funcs.items()):
|
2017-09-05 13:38:17 +08:00
|
|
|
if func.isconstructor:
|
|
|
|
continue
|
2014-08-14 23:01:49 +08:00
|
|
|
self.code_ns_reg.write(func.get_tab_entry())
|
2021-04-01 04:24:32 +08:00
|
|
|
custom_entries_macro = 'PYOPENCV_EXTRA_METHODS_{}'.format(wname.upper())
|
|
|
|
self.code_ns_reg.write('#ifdef {}\n {}\n#endif\n'.format(custom_entries_macro, custom_entries_macro))
|
2014-08-14 23:01:49 +08:00
|
|
|
self.code_ns_reg.write(' {NULL, NULL}\n};\n\n')
|
|
|
|
|
2014-08-19 17:35:46 +08:00
|
|
|
self.code_ns_reg.write('static ConstDef consts_%s[] = {\n'%wname)
|
|
|
|
for name, cname in sorted(ns.consts.items()):
|
2018-08-28 03:38:26 +08:00
|
|
|
self.code_ns_reg.write(' {"%s", static_cast<long>(%s)},\n'%(name, cname))
|
2014-08-19 20:57:37 +08:00
|
|
|
compat_name = re.sub(r"([a-z])([A-Z])", r"\1_\2", name).upper()
|
|
|
|
if name != compat_name:
|
2018-08-28 03:38:26 +08:00
|
|
|
self.code_ns_reg.write(' {"%s", static_cast<long>(%s)},\n'%(compat_name, cname))
|
2021-04-01 04:24:32 +08:00
|
|
|
custom_entries_macro = 'PYOPENCV_EXTRA_CONSTANTS_{}'.format(wname.upper())
|
|
|
|
self.code_ns_reg.write('#ifdef {}\n {}\n#endif\n'.format(custom_entries_macro, custom_entries_macro))
|
2014-08-19 20:57:37 +08:00
|
|
|
self.code_ns_reg.write(' {NULL, 0}\n};\n\n')
|
2014-08-19 17:35:46 +08:00
|
|
|
|
2018-09-28 14:39:51 +08:00
|
|
|
def gen_enum_reg(self, enum_name):
|
|
|
|
name_seg = enum_name.split(".")
|
|
|
|
is_enum_class = False
|
|
|
|
if len(name_seg) >= 2 and name_seg[-1] == name_seg[-2]:
|
|
|
|
enum_name = ".".join(name_seg[:-1])
|
|
|
|
is_enum_class = True
|
|
|
|
|
|
|
|
wname = normalize_class_name(enum_name)
|
|
|
|
cname = enum_name.replace(".", "::")
|
|
|
|
|
|
|
|
code = ""
|
|
|
|
if re.sub(r"^cv\.", "", enum_name) != wname:
|
|
|
|
code += "typedef {0} {1};\n".format(cname, wname)
|
|
|
|
code += "CV_PY_FROM_ENUM({0});\nCV_PY_TO_ENUM({0});\n\n".format(wname)
|
|
|
|
self.code_enums.write(code)
|
2014-08-16 00:44:32 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
def save(self, path, name, buf):
|
2017-09-05 21:10:36 +08:00
|
|
|
with open(path + "/" + name, "wt") as f:
|
|
|
|
f.write(buf.getvalue())
|
|
|
|
|
|
|
|
def save_json(self, path, name, value):
|
|
|
|
import json
|
|
|
|
with open(path + "/" + name, "wt") as f:
|
|
|
|
json.dump(value, f)
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
def gen(self, srcfiles, output_path):
|
2010-10-29 21:09:49 +08:00
|
|
|
self.clear()
|
2018-08-06 19:46:46 +08:00
|
|
|
self.parser = hdr_parser.CppHeaderParser(generate_umat_decls=True, generate_gpumat_decls=True)
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
# step 1: scan the headers and build more descriptive maps of classes, consts, functions
|
|
|
|
for hdr in srcfiles:
|
2014-08-19 17:35:46 +08:00
|
|
|
decls = self.parser.parse(hdr)
|
2014-07-22 16:36:36 +08:00
|
|
|
if len(decls) == 0:
|
|
|
|
continue
|
2021-02-09 21:55:16 +08:00
|
|
|
|
|
|
|
if hdr.find('misc/python/shadow_') < 0: # Avoid including the "shadow_" files
|
|
|
|
if hdr.find('opencv2/') >= 0:
|
|
|
|
# put relative path
|
|
|
|
self.code_include.write('#include "{0}"\n'.format(hdr[hdr.rindex('opencv2/'):]))
|
|
|
|
else:
|
|
|
|
self.code_include.write('#include "{0}"\n'.format(hdr))
|
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
for decl in decls:
|
|
|
|
name = decl[0]
|
|
|
|
if name.startswith("struct") or name.startswith("class"):
|
|
|
|
# class/struct
|
|
|
|
p = name.find(" ")
|
|
|
|
stype = name[:p]
|
|
|
|
name = name[p+1:].strip()
|
|
|
|
self.add_class(stype, name, decl)
|
|
|
|
elif name.startswith("const"):
|
|
|
|
# constant
|
|
|
|
self.add_const(name.replace("const ", "").strip(), decl)
|
2018-08-20 22:35:41 +08:00
|
|
|
elif name.startswith("enum"):
|
|
|
|
# enum
|
2018-08-28 03:38:26 +08:00
|
|
|
self.add_enum(name.rsplit(" ", 1)[1], decl)
|
2010-11-03 01:58:22 +08:00
|
|
|
else:
|
|
|
|
# function
|
|
|
|
self.add_func(decl)
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2015-06-05 17:41:23 +08:00
|
|
|
# step 1.5 check if all base classes exist
|
|
|
|
for name, classinfo in self.classes.items():
|
|
|
|
if classinfo.base:
|
|
|
|
chunks = classinfo.base.split('_')
|
|
|
|
base = '_'.join(chunks)
|
|
|
|
while base not in self.classes and len(chunks)>1:
|
|
|
|
del chunks[-2]
|
|
|
|
base = '_'.join(chunks)
|
|
|
|
if base not in self.classes:
|
|
|
|
print("Generator error: unable to resolve base %s for %s"
|
|
|
|
% (classinfo.base, classinfo.name))
|
|
|
|
sys.exit(-1)
|
2017-08-28 20:29:33 +08:00
|
|
|
base_instance = self.classes[base]
|
2015-06-05 17:41:23 +08:00
|
|
|
classinfo.base = base
|
2017-08-28 20:29:33 +08:00
|
|
|
classinfo.isalgorithm |= base_instance.isalgorithm # wrong processing of 'isalgorithm' flag:
|
|
|
|
# doesn't work for trees(graphs) with depth > 2
|
2015-06-05 17:41:23 +08:00
|
|
|
self.classes[name] = classinfo
|
|
|
|
|
2017-08-28 20:29:33 +08:00
|
|
|
# tree-based propagation of 'isalgorithm'
|
|
|
|
processed = dict()
|
|
|
|
def process_isalgorithm(classinfo):
|
|
|
|
if classinfo.isalgorithm or classinfo in processed:
|
|
|
|
return classinfo.isalgorithm
|
|
|
|
res = False
|
|
|
|
if classinfo.base:
|
|
|
|
res = process_isalgorithm(self.classes[classinfo.base])
|
|
|
|
#assert not (res == True or classinfo.isalgorithm is False), "Internal error: " + classinfo.name + " => " + classinfo.base
|
|
|
|
classinfo.isalgorithm |= res
|
|
|
|
res = classinfo.isalgorithm
|
|
|
|
processed[classinfo] = True
|
|
|
|
return res
|
|
|
|
for name, classinfo in self.classes.items():
|
|
|
|
process_isalgorithm(classinfo)
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
# step 2: generate code for the classes and their methods
|
2013-06-13 04:02:30 +08:00
|
|
|
classlist = list(self.classes.items())
|
2010-10-29 21:09:49 +08:00
|
|
|
classlist.sort()
|
2010-11-03 01:58:22 +08:00
|
|
|
for name, classinfo in classlist:
|
2019-03-21 15:45:02 +08:00
|
|
|
self.code_types.write("//{}\n".format(80*"="))
|
|
|
|
self.code_types.write("// {} ({})\n".format(name, 'Map' if classinfo.ismap else 'Generic'))
|
|
|
|
self.code_types.write("//{}\n".format(80*"="))
|
|
|
|
self.code_types.write(classinfo.gen_code(self))
|
2010-11-03 01:58:22 +08:00
|
|
|
if classinfo.ismap:
|
2019-03-21 15:45:02 +08:00
|
|
|
self.code_types.write(gen_template_map_type_cvt.substitute(name=classinfo.name, cname=classinfo.cname))
|
2010-11-03 01:58:22 +08:00
|
|
|
else:
|
2018-08-19 01:34:47 +08:00
|
|
|
mappable_code = "\n".join([
|
|
|
|
gen_template_mappable.substitute(cname=classinfo.cname, mappable=mappable)
|
|
|
|
for mappable in classinfo.mappables])
|
2019-03-21 15:45:02 +08:00
|
|
|
code = gen_template_type_decl.substitute(
|
|
|
|
name=classinfo.name,
|
|
|
|
cname=classinfo.cname if classinfo.issimple else "Ptr<{}>".format(classinfo.cname),
|
|
|
|
mappable_code=mappable_code
|
|
|
|
)
|
|
|
|
self.code_types.write(code)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2011-06-10 21:22:40 +08:00
|
|
|
# register classes in the same order as they have been declared.
|
|
|
|
# this way, base classes will be registered in Python before their derivatives.
|
|
|
|
classlist1 = [(classinfo.decl_idx, name, classinfo) for name, classinfo in classlist]
|
|
|
|
classlist1.sort()
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2011-06-10 21:22:40 +08:00
|
|
|
for decl_idx, name, classinfo in classlist1:
|
2019-03-21 15:45:02 +08:00
|
|
|
if classinfo.ismap:
|
|
|
|
continue
|
|
|
|
self.code_type_publish.write(classinfo.gen_def(self))
|
|
|
|
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
# step 3: generate the code for all the global functions
|
2014-08-18 20:52:01 +08:00
|
|
|
for ns_name, ns in sorted(self.namespaces.items()):
|
2014-08-19 17:35:46 +08:00
|
|
|
if ns_name.split('.')[0] != 'cv':
|
|
|
|
continue
|
2014-08-18 20:52:01 +08:00
|
|
|
for name, func in sorted(ns.funcs.items()):
|
2017-09-05 13:38:17 +08:00
|
|
|
if func.isconstructor:
|
|
|
|
continue
|
2017-09-05 21:10:36 +08:00
|
|
|
code = func.gen_code(self)
|
2014-08-16 00:44:32 +08:00
|
|
|
self.code_funcs.write(code)
|
2014-08-18 20:52:01 +08:00
|
|
|
self.gen_namespace(ns_name)
|
2019-03-21 15:45:02 +08:00
|
|
|
self.code_ns_init.write('CVPY_MODULE("{}", {});\n'.format(ns_name[2:], normalize_class_name(ns_name)))
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2018-09-28 14:39:51 +08:00
|
|
|
# step 4: generate the code for enum types
|
|
|
|
enumlist = list(self.enums.values())
|
|
|
|
enumlist.sort()
|
|
|
|
for name in enumlist:
|
|
|
|
self.gen_enum_reg(name)
|
|
|
|
|
|
|
|
# step 5: generate the code for constants
|
2013-06-13 04:02:30 +08:00
|
|
|
constlist = list(self.consts.items())
|
2010-10-29 21:09:49 +08:00
|
|
|
constlist.sort()
|
|
|
|
for name, constinfo in constlist:
|
|
|
|
self.gen_const_reg(constinfo)
|
2012-05-31 19:04:11 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
# That's it. Now save all the files
|
2014-05-12 18:58:54 +08:00
|
|
|
self.save(output_path, "pyopencv_generated_include.h", self.code_include)
|
2010-10-29 21:09:49 +08:00
|
|
|
self.save(output_path, "pyopencv_generated_funcs.h", self.code_funcs)
|
2018-09-28 14:39:51 +08:00
|
|
|
self.save(output_path, "pyopencv_generated_enums.h", self.code_enums)
|
2019-03-21 15:45:02 +08:00
|
|
|
self.save(output_path, "pyopencv_generated_types.h", self.code_type_publish)
|
|
|
|
self.save(output_path, "pyopencv_generated_types_content.h", self.code_types)
|
|
|
|
self.save(output_path, "pyopencv_generated_modules.h", self.code_ns_init)
|
|
|
|
self.save(output_path, "pyopencv_generated_modules_content.h", self.code_ns_reg)
|
2017-09-05 21:10:36 +08:00
|
|
|
self.save_json(output_path, "pyopencv_signatures.json", self.py_signatures)
|
2010-10-29 21:09:49 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2010-11-03 01:58:22 +08:00
|
|
|
srcfiles = hdr_parser.opencv_hdr_list
|
2020-12-18 20:00:42 +08:00
|
|
|
dstdir = "."
|
2011-05-17 20:52:07 +08:00
|
|
|
if len(sys.argv) > 1:
|
2010-11-03 01:58:22 +08:00
|
|
|
dstdir = sys.argv[1]
|
2011-05-17 20:52:07 +08:00
|
|
|
if len(sys.argv) > 2:
|
2018-10-29 17:54:50 +08:00
|
|
|
with open(sys.argv[2], 'r') as f:
|
|
|
|
srcfiles = [l.strip() for l in f.readlines()]
|
2010-11-03 01:58:22 +08:00
|
|
|
generator = PythonWrapperGenerator()
|
|
|
|
generator.gen(srcfiles, dstdir)
|