Merge pull request #17493 from TolyaTalamanov:at/python-bindings-gapi

* Implement G-API python bindings

* Fix hdr_parser

* Drop initlization with brackets using regexp

* Handle bracket initilization another way

* Add test for core operations

* Declaration and definition of View constructor now in different files

* Refactor tests

* Remove combine decorator from tests

* Fix comment to review

* Fix test

* Fix comments to review

* Remove GCompilerArgs implementation from python

Co-authored-by: Pinaev <danil.pinaev@intel.com>
This commit is contained in:
Anatoliy Talamanov 2020-07-29 16:18:52 +03:00 committed by GitHub
parent afe9993376
commit c708f506a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 128 additions and 24 deletions

View File

@ -24,6 +24,8 @@ ocv_add_module(gapi
opencv_imgproc
OPTIONAL
opencv_video
WRAP
python
)
if(MSVC)

View File

@ -528,7 +528,7 @@ Supported matrix data types are @ref CV_8UC1, @ref CV_8UC3, @ref CV_16UC1, @ref
@param ddepth optional depth of the output matrix.
@sa sub, addWeighted
*/
GAPI_EXPORTS GMat add(const GMat& src1, const GMat& src2, int ddepth = -1);
GAPI_EXPORTS_W GMat add(const GMat& src1, const GMat& src2, int ddepth = -1);
/** @brief Calculates the per-element sum of matrix and given scalar.

View File

@ -16,7 +16,7 @@ namespace gapi {
namespace core {
namespace cpu {
GAPI_EXPORTS GKernelPackage kernels();
GAPI_EXPORTS_W cv::gapi::GKernelPackage kernels();
} // namespace cpu
} // namespace core

View File

@ -13,7 +13,7 @@
namespace cv { namespace gapi { namespace core { namespace fluid {
GAPI_EXPORTS GKernelPackage kernels();
GAPI_EXPORTS_W cv::gapi::GKernelPackage kernels();
}}}}

View File

@ -56,8 +56,6 @@ public:
}
};
View() = default;
const inline uint8_t* InLineB(int index) const // -(w-1)/2...0...+(w-1)/2 for Filters
{
return m_cache->linePtr(index);
@ -80,6 +78,7 @@ public:
Priv& priv(); // internal use only
const Priv& priv() const; // internal use only
View();
View(std::unique_ptr<Priv>&& p);
View(View&& v);
View& operator=(View&& v);

View File

@ -93,9 +93,12 @@ namespace detail {
* passed in (a variadic template parameter pack) into a vector of
* cv::GCompileArg objects.
*/
struct GAPI_EXPORTS GCompileArg
struct GAPI_EXPORTS_W_SIMPLE GCompileArg
{
public:
// NB: Required for pythnon bindings
GCompileArg() = default;
std::string tag;
// FIXME: use decay in GArg/other trait-based wrapper before leg is shot!

View File

@ -118,7 +118,7 @@ namespace I {
*
* @sa GCompiled
*/
class GAPI_EXPORTS GComputation
class GAPI_EXPORTS_W GComputation
{
public:
class Priv;
@ -191,7 +191,7 @@ public:
* @param in2 second input GMat of the defined binary computation
* @param out output GMat of the defined binary computation
*/
GComputation(GMat in1, GMat in2, GMat out); // Binary overload
GAPI_WRAP GComputation(GMat in1, GMat in2, GMat out); // Binary overload
/**
* @brief Defines a binary (two inputs -- one output) computation
@ -298,7 +298,7 @@ public:
* @param args compilation arguments for underlying compilation
* process.
*/
void apply(cv::Mat in1, cv::Mat in2, cv::Mat &out, GCompileArgs &&args = {}); // Binary overload
GAPI_WRAP void apply(cv::Mat in1, cv::Mat in2, CV_OUT cv::Mat &out, GCompileArgs &&args = {}); // Binary overload
/**
* @brief Execute an binary computation (with compilation on the fly)

View File

@ -445,7 +445,7 @@ namespace gapi {
* Finally, two kernel packages can be combined into a new one
* with function cv::gapi::combine().
*/
class GAPI_EXPORTS GKernelPackage
class GAPI_EXPORTS_W_SIMPLE GKernelPackage
{
/// @private
@ -712,6 +712,7 @@ namespace detail
static const char* tag() { return "gapi.use_only"; }
};
} // namespace detail
} // namespace cv
#endif // OPENCV_GAPI_GKERNEL_HPP

View File

@ -46,10 +46,10 @@ struct GOrigin;
* `cv::GArray<T>` | std::vector<T>
* `cv::GOpaque<T>` | T
*/
class GAPI_EXPORTS GMat
class GAPI_EXPORTS_W_SIMPLE GMat
{
public:
GMat(); // Empty constructor
GAPI_WRAP GMat(); // Empty constructor
GMat(const GNode &n, std::size_t out); // Operation result constructor
GOrigin& priv(); // Internal use only

View File

@ -16,7 +16,7 @@ namespace gapi {
namespace core {
namespace ocl {
GAPI_EXPORTS GKernelPackage kernels();
GAPI_EXPORTS_W cv::gapi::GKernelPackage kernels();
} // namespace ocl
} // namespace core

View File

@ -11,8 +11,15 @@
# if defined(__OPENCV_BUILD)
# include <opencv2/core/base.hpp>
# define GAPI_EXPORTS CV_EXPORTS
/* special informative macros for wrapper generators */
# define GAPI_WRAP CV_WRAP
# define GAPI_EXPORTS_W_SIMPLE CV_EXPORTS_W_SIMPLE
# define GAPI_EXPORTS_W CV_EXPORTS_W
# else
# define GAPI_WRAP
# define GAPI_EXPORTS
# define GAPI_EXPORTS_W_SIMPLE
# define GAPI_EXPORTS_W
#if 0 // Note: the following version currently is not needed for non-OpenCV build
# if defined _WIN32

View File

@ -13,7 +13,7 @@
namespace cv { namespace gapi { namespace core { namespace plaidml {
GAPI_EXPORTS GKernelPackage kernels();
GAPI_EXPORTS cv::gapi::GKernelPackage kernels();
}}}}

View File

@ -0,0 +1,13 @@
using gapi_GKernelPackage = cv::gapi::GKernelPackage;
template<>
bool pyopencv_to(PyObject* obj, std::vector<GCompileArg>& value, const ArgInfo& info)
{
return pyopencv_to_generic_vec(obj, value, info);
}
template<>
PyObject* pyopencv_from(const std::vector<GCompileArg>& value)
{
return pyopencv_from_generic_vec(value);
}

View File

@ -0,0 +1,7 @@
#error This is a shadow header file, which is not intended for processing by any compiler. \
Only bindings parser should handle this file.
namespace cv
{
GAPI_EXPORTS_W GCompileArgs compile_args(gapi::GKernelPackage pkg);
} // namespace cv

View File

@ -0,0 +1,42 @@
#!/usr/bin/env python
import numpy as np
import cv2 as cv
from tests_common import NewOpenCVTests
# Plaidml is an optional backend
pkgs = [
cv.gapi.core.ocl.kernels(),
cv.gapi.core.cpu.kernels(),
cv.gapi.core.fluid.kernels()
# cv.gapi.core.plaidml.kernels()
]
class gapi_core_test(NewOpenCVTests):
def test_add(self):
# TODO: Extend to use any type and size here
sz = (1280, 720)
in1 = np.random.randint(0, 100, sz).astype(np.uint8)
in2 = np.random.randint(0, 100, sz).astype(np.uint8)
# OpenCV
expected = in1 + in2
# G-API
g_in1 = cv.GMat()
g_in2 = cv.GMat()
g_out = cv.gapi.add(g_in1, g_in2)
comp = cv.GComputation(g_in1, g_in2, g_out)
for pkg in pkgs:
actual = comp.apply(in1, in2, args=cv.compile_args(pkg))
# Comparison
self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
if __name__ == '__main__':
NewOpenCVTests.bootstrap()

View File

@ -694,6 +694,8 @@ fluid::View::View(std::unique_ptr<Priv>&& p)
: m_priv(std::move(p)), m_cache(&m_priv->cache())
{ /* nothing */ }
fluid::View::View() = default;
fluid::View::View(View&&) = default;
fluid::View& fluid::View::operator=(View&&) = default;
fluid::View::~View() = default;

View File

@ -27,14 +27,14 @@ gen_template_check_self = Template("""
gen_template_call_constructor_prelude = Template("""new (&(self->v)) Ptr<$cname>(); // init Ptr with placement new
if(self) """)
gen_template_call_constructor = Template("""self->v.reset(new ${cname}${args})""")
gen_template_call_constructor = Template("""self->v.reset(new ${cname}${py_args})""")
gen_template_simple_call_constructor_prelude = Template("""if(self) """)
gen_template_simple_call_constructor = Template("""new (&(self->v)) ${cname}${args}""")
gen_template_simple_call_constructor = Template("""new (&(self->v)) ${cname}${py_args}""")
gen_template_parse_args = Template("""const char* keywords[] = { $kw_list, NULL };
if( PyArg_ParseTupleAndKeywords(args, kw, "$fmtspec", (char**)keywords, $parse_arglist)$code_cvt )""")
if( PyArg_ParseTupleAndKeywords(py_args, kw, "$fmtspec", (char**)keywords, $parse_arglist)$code_cvt )""")
gen_template_func_body = Template("""$code_decl
$code_parse
@ -362,6 +362,7 @@ class ArgInfo(object):
self.inputarg = True
self.outputarg = False
self.returnarg = False
self.isrvalueref = False
for m in arg_tuple[3]:
if m == "/O":
self.inputarg = False
@ -377,6 +378,8 @@ class ArgInfo(object):
elif m.startswith("/CA"):
self.isarray = True
self.arraycvt = m[2:].strip()
elif m == "/RRef":
self.isrvalueref = True
self.py_inputarg = False
self.py_outputarg = False
@ -529,14 +532,14 @@ class FuncInfo(object):
def get_wrapper_prototype(self, codegen):
full_fname = self.get_wrapper_name()
if self.isconstructor:
return "static int {fn_name}(pyopencv_{type_name}_t* self, PyObject* args, PyObject* kw)".format(
return "static int {fn_name}(pyopencv_{type_name}_t* self, PyObject* py_args, PyObject* kw)".format(
fn_name=full_fname, type_name=codegen.classes[self.classname].name)
if self.classname:
self_arg = "self"
else:
self_arg = ""
return "static PyObject* %s(PyObject* %s, PyObject* args, PyObject* kw)" % (full_fname, self_arg)
return "static PyObject* %s(PyObject* %s, PyObject* py_args, PyObject* kw)" % (full_fname, self_arg)
def get_tab_entry(self):
prototype_list = []
@ -682,6 +685,10 @@ class FuncInfo(object):
if not code_args.endswith("("):
code_args += ", "
if a.isrvalueref:
a.name = 'std::move(' + a.name + ')'
code_args += amp + a.name
code_args += ")"
@ -695,7 +702,7 @@ class FuncInfo(object):
templ = gen_template_call_constructor
code_prelude = templ_prelude.substitute(name=selfinfo.name, cname=selfinfo.cname)
code_fcall = templ.substitute(name=selfinfo.name, cname=selfinfo.cname, args=code_args)
code_fcall = templ.substitute(name=selfinfo.name, cname=selfinfo.cname, py_args=code_args)
if v.isphantom:
code_fcall = code_fcall.replace("new " + selfinfo.cname, self.cname.replace("::", "_"))
else:
@ -744,7 +751,7 @@ class FuncInfo(object):
parse_arglist = ", ".join(["&" + all_cargs[argno][1] for aname, argno in v.py_arglist]),
code_cvt = " &&\n ".join(code_cvt_list))
else:
code_parse = "if(PyObject_Size(args) == 0 && (!kw || PyObject_Size(kw) == 0))"
code_parse = "if(PyObject_Size(py_args) == 0 && (!kw || PyObject_Size(kw) == 0))"
if len(v.py_outlist) == 0:
code_ret = "Py_RETURN_NONE"

View File

@ -111,6 +111,11 @@ class CppHeaderParser(object):
if npos >= 0:
modlist.append("/C")
npos = arg_str.find("&&")
if npos >= 0:
arg_str = arg_str.replace("&&", '')
modlist.append("/RRef")
npos = arg_str.find("&")
if npos >= 0:
modlist.append("/Ref")
@ -715,6 +720,8 @@ class CppHeaderParser(object):
return stmt_type, classname, True, decl
if stmt.startswith("enum") or stmt.startswith("namespace"):
# NB: Drop inheritance syntax for enum
stmt = stmt.split(':')[0]
stmt_list = stmt.rsplit(" ", 1)
if len(stmt_list) < 2:
stmt_list.append("<unnamed>")
@ -812,6 +819,15 @@ class CppHeaderParser(object):
l = l0.strip()
# G-API specific aliases
l = self.batch_replace(l, [
("GAPI_EXPORTS", "CV_EXPORTS"),
("GAPI_EXPORTS_W", "CV_EXPORTS_W"),
("GAPI_EXPORTS_W_SIMPLE","CV_EXPORTS_W_SIMPLE"),
("GAPI_WRAP", "CV_WRAP"),
('defined(GAPI_STANDALONE)', '0'),
])
if state == SCAN and l.startswith("#"):
state = DIRECTIVE
# fall through to the if state == DIRECTIVE check
@ -867,6 +883,11 @@ class CppHeaderParser(object):
sys.exit(-1)
while 1:
# NB: Avoid parsing '{' for case:
# foo(Obj&& = {});
if re.search(r'=\s*\{\s*\}', l):
token, pos = ';', len(l)
else:
token, pos = self.find_next_token(l, [";", "\"", "{", "}", "//", "/*"])
if not token: