From: Anatoliy Talamanov Date: Thu, 17 Sep 2020 19:00:03 +0000 (+0300) Subject: Merge pull request #18332 from TolyaTalamanov:at/wrap-GIn-GOut X-Git-Tag: submit/tizen/20210224.033012~2^2~49 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a07f064e5086876103ddaa8c456a4f1fc1e1a289;p=platform%2Fupstream%2Fopencv.git Merge pull request #18332 from TolyaTalamanov:at/wrap-GIn-GOut [G-API] Wrap GIn & GOut * Wrap GIn & GOut into python * Remove extra brackets * Use reinterpret_cast --- diff --git a/modules/gapi/include/opencv2/gapi/gcomputation.hpp b/modules/gapi/include/opencv2/gapi/gcomputation.hpp index d6b8561..0cf8b10 100644 --- a/modules/gapi/include/opencv2/gapi/gcomputation.hpp +++ b/modules/gapi/include/opencv2/gapi/gcomputation.hpp @@ -161,8 +161,8 @@ public: * * @sa @ref gapi_data_objects */ - GComputation(GProtoInputArgs &&ins, - GProtoOutputArgs &&outs); // Arg-to-arg overload + GAPI_WRAP GComputation(GProtoInputArgs &&ins, + GProtoOutputArgs &&outs); // Arg-to-arg overload // 2. Syntax sugar and compatibility overloads /** diff --git a/modules/gapi/include/opencv2/gapi/gproto.hpp b/modules/gapi/include/opencv2/gapi/gproto.hpp index 5319ae3..fbcccb3 100644 --- a/modules/gapi/include/opencv2/gapi/gproto.hpp +++ b/modules/gapi/include/opencv2/gapi/gproto.hpp @@ -57,6 +57,8 @@ template struct GIOProtoArgs { public: + // NB: Used by python wrapper + GIOProtoArgs() = default; explicit GIOProtoArgs(const GProtoArgs& args) : m_args(args) {} explicit GIOProtoArgs(GProtoArgs &&args) : m_args(std::move(args)) {} diff --git a/modules/gapi/misc/python/pyopencv_gapi.hpp b/modules/gapi/misc/python/pyopencv_gapi.hpp index 02f2624..7ef4cac 100644 --- a/modules/gapi/misc/python/pyopencv_gapi.hpp +++ b/modules/gapi/misc/python/pyopencv_gapi.hpp @@ -11,3 +11,35 @@ PyObject* pyopencv_from(const std::vector& value) { return pyopencv_from_generic_vec(value); } + +template +static PyObject* extract_proto_args(PyObject* py_args, PyObject* kw) +{ + using namespace cv; + + GProtoArgs args; + Py_ssize_t size = PyTuple_Size(py_args); + for (int i = 0; i < size; ++i) { + PyObject* item = PyTuple_GetItem(py_args, i); + if (PyObject_TypeCheck(item, reinterpret_cast(pyopencv_GScalar_TypePtr))) { + args.emplace_back(reinterpret_cast(item)->v); + } else if (PyObject_TypeCheck(item, reinterpret_cast(pyopencv_GMat_TypePtr))) { + args.emplace_back(reinterpret_cast(item)->v); + } else { + PyErr_SetString(PyExc_TypeError, "cv.GIn() supports only cv.GMat and cv.GScalar"); + return NULL; + } + } + + return pyopencv_from(T{std::move(args)}); +} + +static PyObject* pyopencv_cv_GIn(PyObject* , PyObject* py_args, PyObject* kw) +{ + return extract_proto_args(py_args, kw); +} + +static PyObject* pyopencv_cv_GOut(PyObject* , PyObject* py_args, PyObject* kw) +{ + return extract_proto_args(py_args, kw); +} diff --git a/modules/gapi/misc/python/shadow_gapi.hpp b/modules/gapi/misc/python/shadow_gapi.hpp index b36f046..2150b86 100644 --- a/modules/gapi/misc/python/shadow_gapi.hpp +++ b/modules/gapi/misc/python/shadow_gapi.hpp @@ -4,4 +4,10 @@ namespace cv { GAPI_EXPORTS_W GCompileArgs compile_args(gapi::GKernelPackage pkg); + class GAPI_EXPORTS_W_SIMPLE GProtoArg { }; + class GAPI_EXPORTS_W_SIMPLE GProtoInputArgs { }; + class GAPI_EXPORTS_W_SIMPLE GProtoOutputArgs { }; + + using GProtoInputArgs = GIOProtoArgs; + using GProtoOutputArgs = GIOProtoArgs; } // namespace cv diff --git a/modules/gapi/misc/python/test/test_gapi_core.py b/modules/gapi/misc/python/test/test_gapi_core.py index 773e542..7720dbc 100644 --- a/modules/gapi/misc/python/test/test_gapi_core.py +++ b/modules/gapi/misc/python/test/test_gapi_core.py @@ -30,7 +30,7 @@ class gapi_core_test(NewOpenCVTests): 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) + comp = cv.GComputation(cv.GIn(g_in1, g_in2), cv.GOut(g_out)) for pkg in pkgs: actual = comp.apply(in1, in2, args=cv.compile_args(pkg)) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 89dcbda..4ab0797 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -1891,7 +1891,6 @@ static int convert_to_char(PyObject *o, char *dst, const ArgInfo& info) #include "pyopencv_generated_enums.h" -#include "pyopencv_custom_headers.h" #ifdef CVPY_DYNAMIC_INIT #define CVPY_TYPE(NAME, STORAGE, SNAME, _1, _2) CVPY_TYPE_DECLARE_DYNAMIC(NAME, STORAGE, SNAME) @@ -1900,6 +1899,7 @@ static int convert_to_char(PyObject *o, char *dst, const ArgInfo& info) #endif #include "pyopencv_generated_types.h" #undef CVPY_TYPE +#include "pyopencv_custom_headers.h" #include "pyopencv_generated_types_content.h" #include "pyopencv_generated_funcs.h" @@ -1916,6 +1916,10 @@ static PyMethodDef special_methods[] = { {"dnn_registerLayer", CV_PY_FN_WITH_KW(pyopencv_cv_dnn_registerLayer), "registerLayer(type, class) -> None"}, {"dnn_unregisterLayer", CV_PY_FN_WITH_KW(pyopencv_cv_dnn_unregisterLayer), "unregisterLayer(type) -> None"}, #endif +#ifdef HAVE_OPENCV_GAPI + {"GIn", CV_PY_FN_WITH_KW(pyopencv_cv_GIn), "GIn(...) -> GInputProtoArgs"}, + {"GOut", CV_PY_FN_WITH_KW(pyopencv_cv_GOut), "GOut(...) -> GOutputProtoArgs"}, +#endif {NULL, NULL}, };