|
|
|
@ -660,7 +660,7 @@ static cv::GRunArgs run_py_kernel(cv::detail::PyObjectHolder kernel, |
|
|
|
|
// NB: Doesn't increase reference counter (false),
|
|
|
|
|
// because PyObject already have ownership.
|
|
|
|
|
// In case exception decrement reference counter.
|
|
|
|
|
cv::detail::PyObjectHolder args(PyTuple_New(ins.size()), false); |
|
|
|
|
cv::detail::PyObjectHolder args(PyTuple_New(ctx.m_isStateful ? ins.size() + 1: ins.size()), false); |
|
|
|
|
for (size_t i = 0; i < ins.size(); ++i) |
|
|
|
|
{ |
|
|
|
|
// NB: If meta is monostate then object isn't associated with G-TYPE.
|
|
|
|
@ -690,6 +690,12 @@ static cv::GRunArgs run_py_kernel(cv::detail::PyObjectHolder kernel, |
|
|
|
|
} |
|
|
|
|
++in_idx; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (ctx.m_isStateful) |
|
|
|
|
{ |
|
|
|
|
PyTuple_SetItem(args.get(), ins.size(), pyopencv_from(ctx.m_state)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NB: Doesn't increase reference counter (false).
|
|
|
|
|
// In case PyObject_CallObject return NULL, do nothing in destructor.
|
|
|
|
|
cv::detail::PyObjectHolder result( |
|
|
|
@ -736,6 +742,80 @@ static cv::GRunArgs run_py_kernel(cv::detail::PyObjectHolder kernel, |
|
|
|
|
return outs; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static cv::GArg setup_py(cv::detail::PyObjectHolder out_meta, const cv::GMetaArgs& meta, |
|
|
|
|
const cv::GArgs& gargs) |
|
|
|
|
{ |
|
|
|
|
PyGILState_STATE gstate; |
|
|
|
|
gstate = PyGILState_Ensure(); |
|
|
|
|
|
|
|
|
|
cv::GArg out; |
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
// NB: Doesn't increase reference counter (false),
|
|
|
|
|
// because PyObject already have ownership.
|
|
|
|
|
// In case exception decrement reference counter.
|
|
|
|
|
cv::detail::PyObjectHolder args(PyTuple_New(meta.size()), false); |
|
|
|
|
size_t idx = 0; |
|
|
|
|
for (auto&& m : meta) |
|
|
|
|
{ |
|
|
|
|
switch (m.index()) |
|
|
|
|
{ |
|
|
|
|
case cv::GMetaArg::index_of<cv::GMatDesc>(): |
|
|
|
|
PyTuple_SetItem(args.get(), idx, pyopencv_from(cv::util::get<cv::GMatDesc>(m))); |
|
|
|
|
break; |
|
|
|
|
case cv::GMetaArg::index_of<cv::GScalarDesc>(): |
|
|
|
|
PyTuple_SetItem(args.get(), idx, |
|
|
|
|
pyopencv_from(cv::util::get<cv::GScalarDesc>(m))); |
|
|
|
|
break; |
|
|
|
|
case cv::GMetaArg::index_of<cv::GArrayDesc>(): |
|
|
|
|
PyTuple_SetItem(args.get(), idx, |
|
|
|
|
pyopencv_from(cv::util::get<cv::GArrayDesc>(m))); |
|
|
|
|
break; |
|
|
|
|
case cv::GMetaArg::index_of<cv::GOpaqueDesc>(): |
|
|
|
|
PyTuple_SetItem(args.get(), idx, |
|
|
|
|
pyopencv_from(cv::util::get<cv::GOpaqueDesc>(m))); |
|
|
|
|
break; |
|
|
|
|
case cv::GMetaArg::index_of<cv::util::monostate>(): |
|
|
|
|
PyTuple_SetItem(args.get(), idx, pyopencv_from(gargs[idx])); |
|
|
|
|
break; |
|
|
|
|
case cv::GMetaArg::index_of<cv::GFrameDesc>(): |
|
|
|
|
util::throw_error( |
|
|
|
|
std::logic_error("GFrame isn't supported for custom operation")); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
++idx; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// PyTuple_SetItem(args.get(), idx, pyopencv_from(gmarg));
|
|
|
|
|
|
|
|
|
|
// NB: Doesn't increase reference counter (false).
|
|
|
|
|
// In case PyObject_CallObject return NULL, do nothing in destructor.
|
|
|
|
|
cv::detail::PyObjectHolder result(PyObject_CallObject(out_meta.get(), args.get()), false); |
|
|
|
|
|
|
|
|
|
if (PyErr_Occurred()) |
|
|
|
|
{ |
|
|
|
|
PyErr_PrintEx(0); |
|
|
|
|
PyErr_Clear(); |
|
|
|
|
throw std::logic_error("Python kernel failed with error!"); |
|
|
|
|
} |
|
|
|
|
// NB: In fact it's impossible situation, because errors were handled above.
|
|
|
|
|
GAPI_Assert(result.get() && "Python kernel returned NULL!"); |
|
|
|
|
|
|
|
|
|
if (!pyopencv_to(result.get(), out, ArgInfo("arg", false))) |
|
|
|
|
{ |
|
|
|
|
util::throw_error(std::logic_error("Unsupported output meta type")); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (...) |
|
|
|
|
{ |
|
|
|
|
PyGILState_Release(gstate); |
|
|
|
|
throw; |
|
|
|
|
} |
|
|
|
|
PyGILState_Release(gstate); |
|
|
|
|
return out; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static GMetaArg get_meta_arg(PyObject* obj) |
|
|
|
|
{ |
|
|
|
|
cv::GMetaArg arg; |
|
|
|
@ -860,6 +940,10 @@ static PyObject* pyopencv_cv_gapi_kernels(PyObject* , PyObject* py_args, PyObjec |
|
|
|
|
"Python kernel should contain run, please use cv.gapi.kernel to define kernel"); |
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
PyObject* setup = nullptr; |
|
|
|
|
if (PyObject_HasAttrString(user_kernel, "setup")) { |
|
|
|
|
setup = PyObject_GetAttrString(user_kernel, "setup"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string id; |
|
|
|
|
if (!pyopencv_to(id_obj, id, ArgInfo("id", false))) |
|
|
|
@ -869,10 +953,22 @@ static PyObject* pyopencv_cv_gapi_kernels(PyObject* , PyObject* py_args, PyObjec |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
using namespace std::placeholders; |
|
|
|
|
gapi::python::GPythonFunctor f(id.c_str(), |
|
|
|
|
std::bind(run_py_meta , cv::detail::PyObjectHolder{out_meta}, _1, _2), |
|
|
|
|
std::bind(run_py_kernel, cv::detail::PyObjectHolder{run} , _1)); |
|
|
|
|
pkg.include(f); |
|
|
|
|
|
|
|
|
|
if (setup) |
|
|
|
|
{ |
|
|
|
|
gapi::python::GPythonFunctor f( |
|
|
|
|
id.c_str(), std::bind(run_py_meta, cv::detail::PyObjectHolder{out_meta}, _1, _2), |
|
|
|
|
std::bind(run_py_kernel, cv::detail::PyObjectHolder{run}, _1), |
|
|
|
|
std::bind(setup_py, cv::detail::PyObjectHolder{setup}, _1, _2)); |
|
|
|
|
pkg.include(f); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
gapi::python::GPythonFunctor f( |
|
|
|
|
id.c_str(), std::bind(run_py_meta, cv::detail::PyObjectHolder{out_meta}, _1, _2), |
|
|
|
|
std::bind(run_py_kernel, cv::detail::PyObjectHolder{run}, _1)); |
|
|
|
|
pkg.include(f); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return pyopencv_from(pkg); |
|
|
|
|
} |
|
|
|
|