Merge pull request #19322 from TolyaTalamanov:at/python-callbacks
[G-API] Introduce cv.gin/cv.descr_of for python * Implement cv.gin/cv.descr_of * Fix macos build * Fix gcomputation tests * Add test * Add using to a void exceeded length for windows build * Add using to a void exceeded length for windows build * Fix comments to review * Fix comments to review * Update from latest master * Avoid graph compilation to obtain in/out info * Fix indentation * Fix comments to review * Avoid using default in switches * Post output meta for giebackend
This commit is contained in:
committed by
GitHub
parent
7bcb51eded
commit
eb82ba36a3
@@ -8,6 +8,32 @@ using gapi_GKernelPackage = cv::gapi::GKernelPackage;
|
||||
using gapi_GNetPackage = cv::gapi::GNetPackage;
|
||||
using gapi_ie_PyParams = cv::gapi::ie::PyParams;
|
||||
using gapi_wip_IStreamSource_Ptr = cv::Ptr<cv::gapi::wip::IStreamSource>;
|
||||
using detail_ExtractArgsCallback = cv::detail::ExtractArgsCallback;
|
||||
using detail_ExtractMetaCallback = cv::detail::ExtractMetaCallback;
|
||||
|
||||
// NB: Python wrapper generate T_U for T<U>
|
||||
// This behavior is only observed for inputs
|
||||
using GOpaque_bool = cv::GOpaque<bool>;
|
||||
using GOpaque_int = cv::GOpaque<int>;
|
||||
using GOpaque_double = cv::GOpaque<double>;
|
||||
using GOpaque_float = cv::GOpaque<double>;
|
||||
using GOpaque_string = cv::GOpaque<std::string>;
|
||||
using GOpaque_Point = cv::GOpaque<cv::Point>;
|
||||
using GOpaque_Point2f = cv::GOpaque<cv::Point2f>;
|
||||
using GOpaque_Size = cv::GOpaque<cv::Size>;
|
||||
using GOpaque_Rect = cv::GOpaque<cv::Rect>;
|
||||
|
||||
using GArray_bool = cv::GArray<bool>;
|
||||
using GArray_int = cv::GArray<int>;
|
||||
using GArray_double = cv::GArray<double>;
|
||||
using GArray_float = cv::GArray<double>;
|
||||
using GArray_string = cv::GArray<std::string>;
|
||||
using GArray_Point = cv::GArray<cv::Point>;
|
||||
using GArray_Point2f = cv::GArray<cv::Point2f>;
|
||||
using GArray_Size = cv::GArray<cv::Size>;
|
||||
using GArray_Rect = cv::GArray<cv::Rect>;
|
||||
using GArray_Scalar = cv::GArray<cv::Scalar>;
|
||||
using GArray_Mat = cv::GArray<cv::Mat>;
|
||||
|
||||
// FIXME: Python wrapper generate code without namespace std,
|
||||
// so it cause error: "string wasn't declared"
|
||||
@@ -32,38 +58,76 @@ bool pyopencv_to(PyObject* obj, GRunArgs& value, const ArgInfo& info)
|
||||
return pyopencv_to_generic_vec(obj, value, info);
|
||||
}
|
||||
|
||||
static PyObject* from_grunarg(const GRunArg& v)
|
||||
template <>
|
||||
PyObject* pyopencv_from(const cv::detail::OpaqueRef& o)
|
||||
{
|
||||
switch (o.getKind())
|
||||
{
|
||||
case cv::detail::OpaqueKind::CV_BOOL : return pyopencv_from(o.rref<bool>());
|
||||
case cv::detail::OpaqueKind::CV_INT : return pyopencv_from(o.rref<int>());
|
||||
case cv::detail::OpaqueKind::CV_DOUBLE : return pyopencv_from(o.rref<double>());
|
||||
case cv::detail::OpaqueKind::CV_FLOAT : return pyopencv_from(o.rref<float>());
|
||||
case cv::detail::OpaqueKind::CV_STRING : return pyopencv_from(o.rref<std::string>());
|
||||
case cv::detail::OpaqueKind::CV_POINT : return pyopencv_from(o.rref<cv::Point>());
|
||||
case cv::detail::OpaqueKind::CV_POINT2F : return pyopencv_from(o.rref<cv::Point2f>());
|
||||
case cv::detail::OpaqueKind::CV_SIZE : return pyopencv_from(o.rref<cv::Size>());
|
||||
case cv::detail::OpaqueKind::CV_RECT : return pyopencv_from(o.rref<cv::Rect>());
|
||||
case cv::detail::OpaqueKind::CV_UNKNOWN : break;
|
||||
case cv::detail::OpaqueKind::CV_UINT64 : break;
|
||||
case cv::detail::OpaqueKind::CV_SCALAR : break;
|
||||
case cv::detail::OpaqueKind::CV_MAT : break;
|
||||
case cv::detail::OpaqueKind::CV_DRAW_PRIM : break;
|
||||
}
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, "Unsupported GOpaque type");
|
||||
return NULL;
|
||||
};
|
||||
|
||||
template <>
|
||||
PyObject* pyopencv_from(const cv::detail::VectorRef& v)
|
||||
{
|
||||
switch (v.getKind())
|
||||
{
|
||||
case cv::detail::OpaqueKind::CV_BOOL : return pyopencv_from_generic_vec(v.rref<bool>());
|
||||
case cv::detail::OpaqueKind::CV_INT : return pyopencv_from_generic_vec(v.rref<int>());
|
||||
case cv::detail::OpaqueKind::CV_DOUBLE : return pyopencv_from_generic_vec(v.rref<double>());
|
||||
case cv::detail::OpaqueKind::CV_FLOAT : return pyopencv_from_generic_vec(v.rref<float>());
|
||||
case cv::detail::OpaqueKind::CV_STRING : return pyopencv_from_generic_vec(v.rref<std::string>());
|
||||
case cv::detail::OpaqueKind::CV_POINT : return pyopencv_from_generic_vec(v.rref<cv::Point>());
|
||||
case cv::detail::OpaqueKind::CV_POINT2F : return pyopencv_from_generic_vec(v.rref<cv::Point2f>());
|
||||
case cv::detail::OpaqueKind::CV_SIZE : return pyopencv_from_generic_vec(v.rref<cv::Size>());
|
||||
case cv::detail::OpaqueKind::CV_RECT : return pyopencv_from_generic_vec(v.rref<cv::Rect>());
|
||||
case cv::detail::OpaqueKind::CV_SCALAR : return pyopencv_from_generic_vec(v.rref<cv::Scalar>());
|
||||
case cv::detail::OpaqueKind::CV_MAT : return pyopencv_from_generic_vec(v.rref<cv::Mat>());
|
||||
case cv::detail::OpaqueKind::CV_UNKNOWN : break;
|
||||
case cv::detail::OpaqueKind::CV_UINT64 : break;
|
||||
case cv::detail::OpaqueKind::CV_DRAW_PRIM : break;
|
||||
}
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, "Unsupported GArray type");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
template <>
|
||||
PyObject* pyopencv_from(const GRunArg& v)
|
||||
{
|
||||
switch (v.index())
|
||||
{
|
||||
case GRunArg::index_of<cv::Mat>():
|
||||
{
|
||||
const auto& m = util::get<cv::Mat>(v);
|
||||
return pyopencv_from(m);
|
||||
}
|
||||
return pyopencv_from(util::get<cv::Mat>(v));
|
||||
|
||||
case GRunArg::index_of<cv::Scalar>():
|
||||
{
|
||||
const auto& s = util::get<cv::Scalar>(v);
|
||||
return pyopencv_from(s);
|
||||
}
|
||||
return pyopencv_from(util::get<cv::Scalar>(v));
|
||||
|
||||
case GRunArg::index_of<cv::detail::VectorRef>():
|
||||
{
|
||||
const auto& vref = util::get<cv::detail::VectorRef>(v);
|
||||
switch (vref.getKind())
|
||||
{
|
||||
case cv::detail::OpaqueKind::CV_POINT2F:
|
||||
return pyopencv_from(vref.rref<cv::Point2f>());
|
||||
default:
|
||||
PyErr_SetString(PyExc_TypeError, "Unsupported kind for GArray");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
default:
|
||||
PyErr_SetString(PyExc_TypeError, "Failed to unpack GRunArgs");
|
||||
return NULL;
|
||||
return pyopencv_from(util::get<cv::detail::VectorRef>(v));
|
||||
|
||||
case GRunArg::index_of<cv::detail::OpaqueRef>():
|
||||
return pyopencv_from(util::get<cv::detail::OpaqueRef>(v));
|
||||
}
|
||||
GAPI_Assert(false);
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, "Failed to unpack GRunArgs");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
template<>
|
||||
@@ -74,7 +138,7 @@ PyObject* pyopencv_from(const GRunArgs& value)
|
||||
// NB: It doesn't make sense to return list with a single element
|
||||
if (n == 1)
|
||||
{
|
||||
PyObject* item = from_grunarg(value[0]);
|
||||
PyObject* item = pyopencv_from(value[0]);
|
||||
if(!item)
|
||||
{
|
||||
return NULL;
|
||||
@@ -85,7 +149,7 @@ PyObject* pyopencv_from(const GRunArgs& value)
|
||||
PyObject* list = PyList_New(n);
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
PyObject* item = from_grunarg(value[i]);
|
||||
PyObject* item = pyopencv_from(value[i]);
|
||||
if(!item)
|
||||
{
|
||||
Py_DECREF(list);
|
||||
@@ -110,6 +174,26 @@ PyObject* pyopencv_from(const GMetaArgs& value)
|
||||
return pyopencv_from_generic_vec(value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void pyopencv_to_with_check(PyObject* from, T& to, const std::string& msg = "")
|
||||
{
|
||||
if (!pyopencv_to(from, to, ArgInfo("", false)))
|
||||
{
|
||||
cv::util::throw_error(std::logic_error(msg));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void pyopencv_to_generic_vec_with_check(PyObject* from,
|
||||
std::vector<T>& to,
|
||||
const std::string& msg = "")
|
||||
{
|
||||
if (!pyopencv_to_generic_vec(from, to, ArgInfo("", false)))
|
||||
{
|
||||
cv::util::throw_error(std::logic_error(msg));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static PyObject* extract_proto_args(PyObject* py_args, PyObject* kw)
|
||||
{
|
||||
@@ -128,9 +212,13 @@ static PyObject* extract_proto_args(PyObject* py_args, PyObject* kw)
|
||||
{
|
||||
args.emplace_back(reinterpret_cast<pyopencv_GMat_t*>(item)->v);
|
||||
}
|
||||
else if (PyObject_TypeCheck(item, reinterpret_cast<PyTypeObject*>(pyopencv_GArrayP2f_TypePtr)))
|
||||
else if (PyObject_TypeCheck(item, reinterpret_cast<PyTypeObject*>(pyopencv_GOpaqueT_TypePtr)))
|
||||
{
|
||||
args.emplace_back(reinterpret_cast<pyopencv_GArrayP2f_t*>(item)->v.strip());
|
||||
args.emplace_back(reinterpret_cast<pyopencv_GOpaqueT_t*>(item)->v.strip());
|
||||
}
|
||||
else if (PyObject_TypeCheck(item, reinterpret_cast<PyTypeObject*>(pyopencv_GArrayT_TypePtr)))
|
||||
{
|
||||
args.emplace_back(reinterpret_cast<pyopencv_GArrayT_t*>(item)->v.strip());
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -152,63 +240,270 @@ static PyObject* pyopencv_cv_GOut(PyObject* , PyObject* py_args, PyObject* kw)
|
||||
return extract_proto_args<GProtoOutputArgs>(py_args, kw);
|
||||
}
|
||||
|
||||
static PyObject* pyopencv_cv_gin(PyObject* , PyObject* py_args, PyObject* kw)
|
||||
static cv::detail::OpaqueRef extract_opaque_ref(PyObject* from, cv::detail::OpaqueKind kind)
|
||||
{
|
||||
using namespace cv;
|
||||
|
||||
GRunArgs args;
|
||||
Py_ssize_t size = PyTuple_Size(py_args);
|
||||
for (int i = 0; i < size; ++i)
|
||||
#define HANDLE_CASE(T, O) case cv::detail::OpaqueKind::CV_##T: \
|
||||
{ \
|
||||
O obj{}; \
|
||||
pyopencv_to_with_check(from, obj, "Failed to obtain " # O); \
|
||||
return cv::detail::OpaqueRef{std::move(obj)}; \
|
||||
}
|
||||
#define UNSUPPORTED(T) case cv::detail::OpaqueKind::CV_##T: break
|
||||
switch (kind)
|
||||
{
|
||||
PyObject* item = PyTuple_GetItem(py_args, i);
|
||||
if (PyTuple_Check(item))
|
||||
HANDLE_CASE(BOOL, bool);
|
||||
HANDLE_CASE(INT, int);
|
||||
HANDLE_CASE(DOUBLE, double);
|
||||
HANDLE_CASE(FLOAT, float);
|
||||
HANDLE_CASE(STRING, std::string);
|
||||
HANDLE_CASE(POINT, cv::Point);
|
||||
HANDLE_CASE(POINT2F, cv::Point2f);
|
||||
HANDLE_CASE(SIZE, cv::Size);
|
||||
HANDLE_CASE(RECT, cv::Rect);
|
||||
UNSUPPORTED(UNKNOWN);
|
||||
UNSUPPORTED(UINT64);
|
||||
UNSUPPORTED(SCALAR);
|
||||
UNSUPPORTED(MAT);
|
||||
UNSUPPORTED(DRAW_PRIM);
|
||||
}
|
||||
#undef HANDLE_CASE
|
||||
#undef UNSUPPORTED
|
||||
|
||||
util::throw_error(std::logic_error("Unsupported type for GOpaqueT"));
|
||||
}
|
||||
|
||||
static cv::detail::VectorRef extract_vector_ref(PyObject* from, cv::detail::OpaqueKind kind)
|
||||
{
|
||||
#define HANDLE_CASE(T, O) case cv::detail::OpaqueKind::CV_##T: \
|
||||
{ \
|
||||
std::vector<O> obj; \
|
||||
pyopencv_to_generic_vec_with_check(from, obj, "Failed to obtain vector of " # O); \
|
||||
return cv::detail::VectorRef{std::move(obj)}; \
|
||||
}
|
||||
#define UNSUPPORTED(T) case cv::detail::OpaqueKind::CV_##T: break
|
||||
switch (kind)
|
||||
{
|
||||
HANDLE_CASE(BOOL, bool);
|
||||
HANDLE_CASE(INT, int);
|
||||
HANDLE_CASE(DOUBLE, double);
|
||||
HANDLE_CASE(FLOAT, float);
|
||||
HANDLE_CASE(STRING, std::string);
|
||||
HANDLE_CASE(POINT, cv::Point);
|
||||
HANDLE_CASE(POINT2F, cv::Point2f);
|
||||
HANDLE_CASE(SIZE, cv::Size);
|
||||
HANDLE_CASE(RECT, cv::Rect);
|
||||
HANDLE_CASE(SCALAR, cv::Scalar);
|
||||
HANDLE_CASE(MAT, cv::Mat);
|
||||
UNSUPPORTED(UNKNOWN);
|
||||
UNSUPPORTED(UINT64);
|
||||
UNSUPPORTED(DRAW_PRIM);
|
||||
#undef HANDLE_CASE
|
||||
#undef UNSUPPORTED
|
||||
}
|
||||
|
||||
util::throw_error(std::logic_error("Unsupported type for GOpaqueT"));
|
||||
}
|
||||
|
||||
static cv::GRunArg extract_run_arg(const cv::GTypeInfo& info, PyObject* item)
|
||||
{
|
||||
switch (info.shape)
|
||||
{
|
||||
case cv::GShape::GMAT:
|
||||
{
|
||||
cv::Scalar s;
|
||||
if (pyopencv_to(item, s, ArgInfo("scalar", false)))
|
||||
// NB: In case streaming it can be IStreamSource or cv::Mat
|
||||
if (PyObject_TypeCheck(item,
|
||||
reinterpret_cast<PyTypeObject*>(pyopencv_gapi_wip_IStreamSource_TypePtr)))
|
||||
{
|
||||
args.emplace_back(s);
|
||||
cv::gapi::wip::IStreamSource::Ptr source =
|
||||
reinterpret_cast<pyopencv_gapi_wip_IStreamSource_t*>(item)->v;
|
||||
return source;
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError, "Failed convert tuple to cv::Scalar");
|
||||
return NULL;
|
||||
cv::Mat obj;
|
||||
pyopencv_to_with_check(item, obj, "Failed to obtain cv::Mat");
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
else if (PyArray_Check(item))
|
||||
case cv::GShape::GSCALAR:
|
||||
{
|
||||
cv::Mat m;
|
||||
if (pyopencv_to(item, m, ArgInfo("mat", false)))
|
||||
{
|
||||
args.emplace_back(m);
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError, "Failed convert array to cv::Mat");
|
||||
return NULL;
|
||||
}
|
||||
cv::Scalar obj;
|
||||
pyopencv_to_with_check(item, obj, "Failed to obtain cv::Scalar");
|
||||
return obj;
|
||||
}
|
||||
else if (PyObject_TypeCheck(item,
|
||||
reinterpret_cast<PyTypeObject*>(pyopencv_gapi_wip_IStreamSource_TypePtr)))
|
||||
case cv::GShape::GOPAQUE:
|
||||
{
|
||||
cv::gapi::wip::IStreamSource::Ptr source =
|
||||
reinterpret_cast<pyopencv_gapi_wip_IStreamSource_t*>(item)->v;
|
||||
args.emplace_back(source);
|
||||
return extract_opaque_ref(item, info.kind);
|
||||
}
|
||||
else
|
||||
case cv::GShape::GARRAY:
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError, "cv.gin can works only with cv::Mat,"
|
||||
"cv::Scalar, cv::gapi::wip::IStreamSource::Ptr");
|
||||
return NULL;
|
||||
return extract_vector_ref(item, info.kind);
|
||||
}
|
||||
case cv::GShape::GFRAME:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return pyopencv_from_generic_vec(args);
|
||||
util::throw_error(std::logic_error("Unsupported output shape"));
|
||||
}
|
||||
|
||||
static PyObject* pyopencv_cv_gout(PyObject* o, PyObject* py_args, PyObject* kw)
|
||||
static cv::GRunArgs extract_run_args(const cv::GTypesInfo& info, PyObject* py_args)
|
||||
{
|
||||
return pyopencv_cv_gin(o, py_args, kw);
|
||||
cv::GRunArgs args;
|
||||
Py_ssize_t tuple_size = PyTuple_Size(py_args);
|
||||
args.reserve(tuple_size);
|
||||
|
||||
for (int i = 0; i < tuple_size; ++i)
|
||||
{
|
||||
args.push_back(extract_run_arg(info[i], PyTuple_GetItem(py_args, i)));
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
static cv::GMetaArg extract_meta_arg(const cv::GTypeInfo& info, PyObject* item)
|
||||
{
|
||||
switch (info.shape)
|
||||
{
|
||||
case cv::GShape::GMAT:
|
||||
{
|
||||
cv::Mat obj;
|
||||
pyopencv_to_with_check(item, obj, "Failed to obtain cv::Mat");
|
||||
return cv::GMetaArg{cv::descr_of(obj)};
|
||||
}
|
||||
case cv::GShape::GSCALAR:
|
||||
{
|
||||
cv::Scalar obj;
|
||||
pyopencv_to_with_check(item, obj, "Failed to obtain cv::Scalar");
|
||||
return cv::GMetaArg{cv::descr_of(obj)};
|
||||
}
|
||||
case cv::GShape::GARRAY:
|
||||
{
|
||||
return cv::GMetaArg{cv::empty_array_desc()};
|
||||
}
|
||||
case cv::GShape::GOPAQUE:
|
||||
{
|
||||
return cv::GMetaArg{cv::empty_gopaque_desc()};
|
||||
}
|
||||
case cv::GShape::GFRAME:
|
||||
{
|
||||
// NB: Isn't supported yet.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
util::throw_error(std::logic_error("Unsupported output shape"));
|
||||
}
|
||||
|
||||
static cv::GMetaArgs extract_meta_args(const cv::GTypesInfo& info, PyObject* py_args)
|
||||
{
|
||||
cv::GMetaArgs metas;
|
||||
Py_ssize_t tuple_size = PyTuple_Size(py_args);
|
||||
metas.reserve(tuple_size);
|
||||
|
||||
for (int i = 0; i < tuple_size; ++i)
|
||||
{
|
||||
metas.push_back(extract_meta_arg(info[i], PyTuple_GetItem(py_args, i)));
|
||||
}
|
||||
|
||||
return metas;
|
||||
}
|
||||
|
||||
static PyObject* pyopencv_cv_gin(PyObject*, PyObject* py_args, PyObject*)
|
||||
{
|
||||
Py_INCREF(py_args);
|
||||
auto callback = cv::detail::ExtractArgsCallback{[=](const cv::GTypesInfo& info)
|
||||
{
|
||||
PyGILState_STATE gstate;
|
||||
gstate = PyGILState_Ensure();
|
||||
|
||||
cv::GRunArgs args;
|
||||
try
|
||||
{
|
||||
args = extract_run_args(info, py_args);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
PyGILState_Release(gstate);
|
||||
throw;
|
||||
}
|
||||
PyGILState_Release(gstate);
|
||||
return args;
|
||||
}};
|
||||
|
||||
return pyopencv_from(callback);
|
||||
}
|
||||
|
||||
static PyObject* pyopencv_cv_descr_of(PyObject*, PyObject* py_args, PyObject*)
|
||||
{
|
||||
Py_INCREF(py_args);
|
||||
auto callback = cv::detail::ExtractMetaCallback{[=](const cv::GTypesInfo& info)
|
||||
{
|
||||
PyGILState_STATE gstate;
|
||||
gstate = PyGILState_Ensure();
|
||||
|
||||
cv::GMetaArgs args;
|
||||
try
|
||||
{
|
||||
args = extract_meta_args(info, py_args);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
PyGILState_Release(gstate);
|
||||
throw;
|
||||
}
|
||||
PyGILState_Release(gstate);
|
||||
return args;
|
||||
}};
|
||||
return pyopencv_from(callback);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
struct PyOpenCV_Converter<cv::GArray<T>>
|
||||
{
|
||||
static PyObject* from(const cv::GArray<T>& p)
|
||||
{
|
||||
return pyopencv_from(cv::GArrayT(p));
|
||||
}
|
||||
static bool to(PyObject *obj, cv::GArray<T>& value, const ArgInfo& info)
|
||||
{
|
||||
if (PyObject_TypeCheck(obj, reinterpret_cast<PyTypeObject*>(pyopencv_GArrayT_TypePtr)))
|
||||
{
|
||||
auto& array = reinterpret_cast<pyopencv_GArrayT_t*>(obj)->v;
|
||||
try {
|
||||
value = cv::util::get<cv::GArray<T>>(array.arg());
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct PyOpenCV_Converter<cv::GOpaque<T>>
|
||||
{
|
||||
static PyObject* from(const cv::GOpaque<T>& p)
|
||||
{
|
||||
return pyopencv_from(cv::GOpaqueT(p));
|
||||
}
|
||||
static bool to(PyObject *obj, cv::GOpaque<T>& value, const ArgInfo& info)
|
||||
{
|
||||
if (PyObject_TypeCheck(obj, reinterpret_cast<PyTypeObject*>(pyopencv_GOpaqueT_TypePtr)))
|
||||
{
|
||||
auto& opaque = reinterpret_cast<pyopencv_GOpaqueT_t*>(obj)->v;
|
||||
try {
|
||||
value = cv::util::get<cv::GOpaque<T>>(opaque.arg());
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // HAVE_OPENCV_GAPI
|
||||
#endif // OPENCV_GAPI_PYOPENCV_GAPI_HPP
|
||||
|
||||
@@ -119,7 +119,8 @@ public:
|
||||
GAPI_Assert(false);
|
||||
}
|
||||
|
||||
GAPI_WRAP gapi::ArgType type() { return m_type; }
|
||||
GAPI_WRAP gapi::ArgType type() { return m_type; }
|
||||
const Storage& arg() const { return m_arg; }
|
||||
|
||||
private:
|
||||
gapi::ArgType m_type;
|
||||
@@ -156,6 +157,7 @@ public:
|
||||
}
|
||||
|
||||
GAPI_WRAP gapi::ArgType type() { return m_type; }
|
||||
const Storage& arg() const { return m_arg; }
|
||||
|
||||
private:
|
||||
gapi::ArgType m_type;
|
||||
|
||||
@@ -16,11 +16,15 @@ namespace cv
|
||||
class GAPI_EXPORTS_W_SIMPLE GRunArg { };
|
||||
class GAPI_EXPORTS_W_SIMPLE GMetaArg { };
|
||||
|
||||
class GAPI_EXPORTS_W_SIMPLE GArrayP2f { };
|
||||
|
||||
using GProtoInputArgs = GIOProtoArgs<In_Tag>;
|
||||
using GProtoOutputArgs = GIOProtoArgs<Out_Tag>;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
struct GAPI_EXPORTS_W_SIMPLE ExtractArgsCallback { };
|
||||
struct GAPI_EXPORTS_W_SIMPLE ExtractMetaCallback { };
|
||||
} // namespace detail
|
||||
|
||||
namespace gapi
|
||||
{
|
||||
GAPI_EXPORTS_W gapi::GNetPackage networks(const cv::gapi::ie::PyParams& params);
|
||||
|
||||
@@ -128,5 +128,62 @@ class gapi_core_test(NewOpenCVTests):
|
||||
'Failed on ' + pkg_name + ' backend')
|
||||
|
||||
|
||||
def test_kmeans(self):
|
||||
# K-means params
|
||||
count = 100
|
||||
sz = (count, 2)
|
||||
in_mat = np.random.random(sz).astype(np.float32)
|
||||
K = 5
|
||||
flags = cv.KMEANS_RANDOM_CENTERS
|
||||
attempts = 1;
|
||||
criteria = (cv.TERM_CRITERIA_MAX_ITER + cv.TERM_CRITERIA_EPS, 30, 0)
|
||||
|
||||
# G-API
|
||||
g_in = cv.GMat()
|
||||
compactness, out_labels, centers = cv.gapi.kmeans(g_in, K, criteria, attempts, flags)
|
||||
comp = cv.GComputation(cv.GIn(g_in), cv.GOut(compactness, out_labels, centers))
|
||||
|
||||
compact, labels, centers = comp.apply(cv.gin(in_mat))
|
||||
|
||||
# Assert
|
||||
self.assertTrue(compact >= 0)
|
||||
self.assertEqual(sz[0], labels.shape[0])
|
||||
self.assertEqual(1, labels.shape[1])
|
||||
self.assertTrue(labels.size != 0)
|
||||
self.assertEqual(centers.shape[1], sz[1]);
|
||||
self.assertEqual(centers.shape[0], K);
|
||||
self.assertTrue(centers.size != 0);
|
||||
|
||||
def generate_random_points(self, sz):
|
||||
arr = np.random.random(sz).astype(np.float32).T
|
||||
return list(zip(arr[0], arr[1]))
|
||||
|
||||
def test_kmeans_2d(self):
|
||||
# K-means 2D params
|
||||
count = 100
|
||||
sz = (count, 2)
|
||||
amount = sz[0]
|
||||
K = 5
|
||||
flags = cv.KMEANS_RANDOM_CENTERS
|
||||
attempts = 1;
|
||||
criteria = (cv.TERM_CRITERIA_MAX_ITER + cv.TERM_CRITERIA_EPS, 30, 0);
|
||||
in_vector = self.generate_random_points(sz)
|
||||
in_labels = []
|
||||
|
||||
# G-API
|
||||
data = cv.GArrayT(cv.gapi.CV_POINT2F)
|
||||
best_labels = cv.GArrayT(cv.gapi.CV_INT)
|
||||
|
||||
compactness, out_labels, centers = cv.gapi.kmeans(data, K, best_labels, criteria, attempts, flags);
|
||||
comp = cv.GComputation(cv.GIn(data, best_labels), cv.GOut(compactness, out_labels, centers));
|
||||
|
||||
compact, labels, centers = comp.apply(cv.gin(in_vector, in_labels));
|
||||
|
||||
# Assert
|
||||
self.assertTrue(compact >= 0)
|
||||
self.assertEqual(amount, len(labels))
|
||||
self.assertEqual(K, len(centers))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
NewOpenCVTests.bootstrap()
|
||||
|
||||
@@ -50,7 +50,9 @@ class gapi_imgproc_test(NewOpenCVTests):
|
||||
# OpenCV - (num_points, 1, 2)
|
||||
# G-API - (num_points, 2)
|
||||
# Comparison
|
||||
self.assertEqual(0.0, cv.norm(expected.flatten(), actual.flatten(), cv.NORM_INF),
|
||||
self.assertEqual(0.0, cv.norm(expected.flatten(),
|
||||
np.array(actual, dtype=np.float32).flatten(),
|
||||
cv.NORM_INF),
|
||||
'Failed on ' + pkg_name + ' backend')
|
||||
|
||||
|
||||
@@ -75,5 +77,30 @@ class gapi_imgproc_test(NewOpenCVTests):
|
||||
'Failed on ' + pkg_name + ' backend')
|
||||
|
||||
|
||||
def test_bounding_rect(self):
|
||||
sz = 1280
|
||||
fscale = 256
|
||||
|
||||
def sample_value(fscale):
|
||||
return np.random.uniform(0, 255 * fscale) / fscale
|
||||
|
||||
points = np.array([(sample_value(fscale), sample_value(fscale)) for _ in range(1280)], np.float32)
|
||||
|
||||
# OpenCV
|
||||
expected = cv.boundingRect(points)
|
||||
|
||||
# G-API
|
||||
g_in = cv.GMat()
|
||||
g_out = cv.gapi.boundingRect(g_in)
|
||||
|
||||
comp = cv.GComputation(cv.GIn(g_in), cv.GOut(g_out))
|
||||
|
||||
for pkg_name, pkg in pkgs:
|
||||
actual = comp.apply(cv.gin(points), args=cv.compile_args(pkg))
|
||||
# Comparison
|
||||
self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF),
|
||||
'Failed on ' + pkg_name + ' backend')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
NewOpenCVTests.bootstrap()
|
||||
|
||||
@@ -49,8 +49,6 @@ class test_gapi_infer(NewOpenCVTests):
|
||||
comp = cv.GComputation(cv.GIn(g_in), cv.GOut(age_g, gender_g))
|
||||
pp = cv.gapi.ie.params("net", model_path, weights_path, device_id)
|
||||
|
||||
nets = cv.gapi.networks(pp)
|
||||
args = cv.compile_args(nets)
|
||||
gapi_age, gapi_gender = comp.apply(cv.gin(img), args=cv.compile_args(cv.gapi.networks(pp)))
|
||||
|
||||
# Check
|
||||
@@ -58,5 +56,64 @@ class test_gapi_infer(NewOpenCVTests):
|
||||
self.assertEqual(0.0, cv.norm(dnn_age, gapi_age, cv.NORM_INF))
|
||||
|
||||
|
||||
def test_person_detection_retail_0013(self):
|
||||
# NB: Check IE
|
||||
if not cv.dnn.DNN_TARGET_CPU in cv.dnn.getAvailableTargets(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE):
|
||||
return
|
||||
|
||||
root_path = '/omz_intel_models/intel/person-detection-retail-0013/FP32/person-detection-retail-0013'
|
||||
model_path = self.find_file(root_path + '.xml', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
|
||||
weights_path = self.find_file(root_path + '.bin', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
|
||||
img_path = self.find_file('gpu/lbpcascade/er.png', [os.environ.get('OPENCV_TEST_DATA_PATH')])
|
||||
device_id = 'CPU'
|
||||
img = cv.resize(cv.imread(img_path), (544, 320))
|
||||
|
||||
# OpenCV DNN
|
||||
net = cv.dnn.readNetFromModelOptimizer(model_path, weights_path)
|
||||
net.setPreferableBackend(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE)
|
||||
net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)
|
||||
|
||||
blob = cv.dnn.blobFromImage(img)
|
||||
|
||||
def parseSSD(detections, size):
|
||||
h, w = size
|
||||
bboxes = []
|
||||
detections = detections.reshape(-1, 7)
|
||||
for sample_id, class_id, confidence, xmin, ymin, xmax, ymax in detections:
|
||||
if confidence >= 0.5:
|
||||
x = int(xmin * w)
|
||||
y = int(ymin * h)
|
||||
width = int(xmax * w - x)
|
||||
height = int(ymax * h - y)
|
||||
bboxes.append((x, y, width, height))
|
||||
|
||||
return bboxes
|
||||
|
||||
net.setInput(blob)
|
||||
dnn_detections = net.forward()
|
||||
dnn_boxes = parseSSD(np.array(dnn_detections), img.shape[:2])
|
||||
|
||||
# OpenCV G-API
|
||||
g_in = cv.GMat()
|
||||
inputs = cv.GInferInputs()
|
||||
inputs.setInput('data', g_in)
|
||||
|
||||
g_sz = cv.gapi.streaming.size(g_in)
|
||||
outputs = cv.gapi.infer("net", inputs)
|
||||
detections = outputs.at("detection_out")
|
||||
bboxes = cv.gapi.parseSSD(detections, g_sz, 0.5, False, False)
|
||||
|
||||
comp = cv.GComputation(cv.GIn(g_in), cv.GOut(bboxes))
|
||||
pp = cv.gapi.ie.params("net", model_path, weights_path, device_id)
|
||||
|
||||
gapi_boxes = comp.apply(cv.gin(img.astype(np.float32)),
|
||||
args=cv.compile_args(cv.gapi.networks(pp)))
|
||||
|
||||
# Comparison
|
||||
self.assertEqual(0.0, cv.norm(np.array(dnn_boxes).flatten(),
|
||||
np.array(gapi_boxes).flatten(),
|
||||
cv.NORM_INF))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
NewOpenCVTests.bootstrap()
|
||||
|
||||
@@ -19,7 +19,7 @@ class test_gapi_streaming(NewOpenCVTests):
|
||||
g_in = cv.GMat()
|
||||
g_out = cv.gapi.medianBlur(g_in, 3)
|
||||
c = cv.GComputation(g_in, g_out)
|
||||
ccomp = c.compileStreaming(cv.descr_of(cv.gin(in_mat)))
|
||||
ccomp = c.compileStreaming(cv.descr_of(in_mat))
|
||||
ccomp.setSource(cv.gin(in_mat))
|
||||
ccomp.start()
|
||||
|
||||
@@ -191,7 +191,9 @@ class test_gapi_streaming(NewOpenCVTests):
|
||||
# NB: OpenCV & G-API have different output shapes:
|
||||
# OpenCV - (num_points, 1, 2)
|
||||
# G-API - (num_points, 2)
|
||||
self.assertEqual(0.0, cv.norm(e.flatten(), a.flatten(), cv.NORM_INF))
|
||||
self.assertEqual(0.0, cv.norm(e.flatten(),
|
||||
np.array(a, np.float32).flatten(),
|
||||
cv.NORM_INF))
|
||||
|
||||
proc_num_frames += 1
|
||||
if proc_num_frames == max_num_frames:
|
||||
|
||||
Reference in New Issue
Block a user