Merge pull request #19982 from TolyaTalamanov:at/new-python-operation-api
G-API: New python operations API * Reimplement test using decorators * Custom python operation API * Remove wip status * python: support Python code in bindings (through loader only) * cleanup, skip tests for Python 2.x (not supported) * python 2.x can't skip unittest modules * Clean up * Clean up * Fix segfault python3.9 Co-authored-by: Alexander Alekhin <alexander.a.alekhin@gmail.com>
This commit is contained in:
parent
0f11b1fc0d
commit
c4df8989e9
@ -1490,7 +1490,7 @@ enlarge an image, it will generally look best with cv::INTER_CUBIC (slow) or cv:
|
||||
|
||||
@sa warpAffine, warpPerspective, remap, resizeP
|
||||
*/
|
||||
GAPI_EXPORTS GMat resize(const GMat& src, const Size& dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR);
|
||||
GAPI_EXPORTS_W GMat resize(const GMat& src, const Size& dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR);
|
||||
|
||||
/** @brief Resizes a planar image.
|
||||
|
||||
|
||||
@ -120,7 +120,7 @@ struct GAPI_EXPORTS_W_SIMPLE GMatDesc
|
||||
// Meta combinator: return a new GMatDesc which differs in size by delta
|
||||
// (all other fields are taken unchanged from this GMatDesc)
|
||||
// FIXME: a better name?
|
||||
GMatDesc withSizeDelta(cv::Size delta) const
|
||||
GAPI_WRAP GMatDesc withSizeDelta(cv::Size delta) const
|
||||
{
|
||||
GMatDesc desc(*this);
|
||||
desc.size += delta;
|
||||
@ -130,12 +130,12 @@ struct GAPI_EXPORTS_W_SIMPLE GMatDesc
|
||||
// (all other fields are taken unchanged from this GMatDesc)
|
||||
//
|
||||
// This is an overload.
|
||||
GMatDesc withSizeDelta(int dx, int dy) const
|
||||
GAPI_WRAP GMatDesc withSizeDelta(int dx, int dy) const
|
||||
{
|
||||
return withSizeDelta(cv::Size{dx,dy});
|
||||
}
|
||||
|
||||
GMatDesc withSize(cv::Size sz) const
|
||||
GAPI_WRAP GMatDesc withSize(cv::Size sz) const
|
||||
{
|
||||
GMatDesc desc(*this);
|
||||
desc.size = sz;
|
||||
@ -144,7 +144,7 @@ struct GAPI_EXPORTS_W_SIMPLE GMatDesc
|
||||
|
||||
// Meta combinator: return a new GMatDesc with specified data depth.
|
||||
// (all other fields are taken unchanged from this GMatDesc)
|
||||
GMatDesc withDepth(int ddepth) const
|
||||
GAPI_WRAP GMatDesc withDepth(int ddepth) const
|
||||
{
|
||||
GAPI_Assert(CV_MAT_CN(ddepth) == 1 || ddepth == -1);
|
||||
GMatDesc desc(*this);
|
||||
@ -166,7 +166,7 @@ struct GAPI_EXPORTS_W_SIMPLE GMatDesc
|
||||
// Meta combinator: return a new GMatDesc with planar flag set
|
||||
// (no size changes are performed, only channel interpretation is changed
|
||||
// (interleaved -> planar)
|
||||
GMatDesc asPlanar() const
|
||||
GAPI_WRAP GMatDesc asPlanar() const
|
||||
{
|
||||
GAPI_Assert(planar == false);
|
||||
GMatDesc desc(*this);
|
||||
@ -177,7 +177,7 @@ struct GAPI_EXPORTS_W_SIMPLE GMatDesc
|
||||
// Meta combinator: return a new GMatDesc
|
||||
// reinterpreting 1-channel input as planar image
|
||||
// (size height is divided by plane number)
|
||||
GMatDesc asPlanar(int planes) const
|
||||
GAPI_WRAP GMatDesc asPlanar(int planes) const
|
||||
{
|
||||
GAPI_Assert(planar == false);
|
||||
GAPI_Assert(chan == 1);
|
||||
@ -192,7 +192,7 @@ struct GAPI_EXPORTS_W_SIMPLE GMatDesc
|
||||
// Meta combinator: return a new GMatDesc with planar flag set to false
|
||||
// (no size changes are performed, only channel interpretation is changed
|
||||
// (planar -> interleaved)
|
||||
GMatDesc asInterleaved() const
|
||||
GAPI_WRAP GMatDesc asInterleaved() const
|
||||
{
|
||||
GAPI_Assert(planar == true);
|
||||
GMatDesc desc(*this);
|
||||
|
||||
@ -1341,7 +1341,7 @@ Output image is 8-bit unsigned 3-channel image @ref CV_8UC3.
|
||||
@param src input image: 8-bit unsigned 3-channel image @ref CV_8UC3.
|
||||
@sa RGB2BGR
|
||||
*/
|
||||
GAPI_EXPORTS GMat BGR2RGB(const GMat& src);
|
||||
GAPI_EXPORTS_W GMat BGR2RGB(const GMat& src);
|
||||
|
||||
/** @brief Converts an image from RGB color space to gray-scaled.
|
||||
|
||||
|
||||
246
modules/gapi/misc/python/package/gapi/__init__.py
Normal file
246
modules/gapi/misc/python/package/gapi/__init__.py
Normal file
@ -0,0 +1,246 @@
|
||||
__all__ = ['op', 'kernel']
|
||||
|
||||
import sys
|
||||
import cv2 as cv
|
||||
|
||||
# NB: Register function in specific module
|
||||
def register(mname):
|
||||
def parameterized(func):
|
||||
sys.modules[mname].__dict__[func.__name__] = func
|
||||
return func
|
||||
return parameterized
|
||||
|
||||
|
||||
@register('cv2')
|
||||
class GOpaque():
|
||||
# NB: Inheritance from c++ class cause segfault.
|
||||
# So just aggregate cv.GOpaqueT instead of inheritance
|
||||
def __new__(cls, argtype):
|
||||
return cv.GOpaqueT(argtype)
|
||||
|
||||
class Bool():
|
||||
def __new__(self):
|
||||
return cv.GOpaqueT(cv.gapi.CV_BOOL)
|
||||
|
||||
class Int():
|
||||
def __new__(self):
|
||||
return cv.GOpaqueT(cv.gapi.CV_INT)
|
||||
|
||||
class Double():
|
||||
def __new__(self):
|
||||
return cv.GOpaqueT(cv.gapi.CV_DOUBLE)
|
||||
|
||||
class Float():
|
||||
def __new__(self):
|
||||
return cv.GOpaqueT(cv.gapi.CV_FLOAT)
|
||||
|
||||
class String():
|
||||
def __new__(self):
|
||||
return cv.GOpaqueT(cv.gapi.CV_STRING)
|
||||
|
||||
class Point():
|
||||
def __new__(self):
|
||||
return cv.GOpaqueT(cv.gapi.CV_POINT)
|
||||
|
||||
class Point2f():
|
||||
def __new__(self):
|
||||
return cv.GOpaqueT(cv.gapi.CV_POINT2F)
|
||||
|
||||
class Size():
|
||||
def __new__(self):
|
||||
return cv.GOpaqueT(cv.gapi.CV_SIZE)
|
||||
|
||||
class Rect():
|
||||
def __new__(self):
|
||||
return cv.GOpaqueT(cv.gapi.CV_RECT)
|
||||
|
||||
class Any():
|
||||
def __new__(self):
|
||||
return cv.GOpaqueT(cv.gapi.CV_ANY)
|
||||
|
||||
@register('cv2')
|
||||
class GArray():
|
||||
# NB: Inheritance from c++ class cause segfault.
|
||||
# So just aggregate cv.GArrayT instead of inheritance
|
||||
def __new__(cls, argtype):
|
||||
return cv.GArrayT(argtype)
|
||||
|
||||
class Bool():
|
||||
def __new__(self):
|
||||
return cv.GArrayT(cv.gapi.CV_BOOL)
|
||||
|
||||
class Int():
|
||||
def __new__(self):
|
||||
return cv.GArrayT(cv.gapi.CV_INT)
|
||||
|
||||
class Double():
|
||||
def __new__(self):
|
||||
return cv.GArrayT(cv.gapi.CV_DOUBLE)
|
||||
|
||||
class Float():
|
||||
def __new__(self):
|
||||
return cv.GArrayT(cv.gapi.CV_FLOAT)
|
||||
|
||||
class String():
|
||||
def __new__(self):
|
||||
return cv.GArrayT(cv.gapi.CV_STRING)
|
||||
|
||||
class Point():
|
||||
def __new__(self):
|
||||
return cv.GArrayT(cv.gapi.CV_POINT)
|
||||
|
||||
class Point2f():
|
||||
def __new__(self):
|
||||
return cv.GArrayT(cv.gapi.CV_POINT2F)
|
||||
|
||||
class Size():
|
||||
def __new__(self):
|
||||
return cv.GArrayT(cv.gapi.CV_SIZE)
|
||||
|
||||
class Rect():
|
||||
def __new__(self):
|
||||
return cv.GArrayT(cv.gapi.CV_RECT)
|
||||
|
||||
class Scalar():
|
||||
def __new__(self):
|
||||
return cv.GArrayT(cv.gapi.CV_SCALAR)
|
||||
|
||||
class Mat():
|
||||
def __new__(self):
|
||||
return cv.GArrayT(cv.gapi.CV_MAT)
|
||||
|
||||
class GMat():
|
||||
def __new__(self):
|
||||
return cv.GArrayT(cv.gapi.CV_GMAT)
|
||||
|
||||
class Any():
|
||||
def __new__(self):
|
||||
return cv.GArray(cv.gapi.CV_ANY)
|
||||
|
||||
|
||||
# NB: Top lvl decorator takes arguments
|
||||
def op(op_id, in_types, out_types):
|
||||
|
||||
garray_types= {
|
||||
cv.GArray.Bool: cv.gapi.CV_BOOL,
|
||||
cv.GArray.Int: cv.gapi.CV_INT,
|
||||
cv.GArray.Double: cv.gapi.CV_DOUBLE,
|
||||
cv.GArray.Float: cv.gapi.CV_FLOAT,
|
||||
cv.GArray.String: cv.gapi.CV_STRING,
|
||||
cv.GArray.Point: cv.gapi.CV_POINT,
|
||||
cv.GArray.Point2f: cv.gapi.CV_POINT2F,
|
||||
cv.GArray.Size: cv.gapi.CV_SIZE,
|
||||
cv.GArray.Rect: cv.gapi.CV_RECT,
|
||||
cv.GArray.Scalar: cv.gapi.CV_SCALAR,
|
||||
cv.GArray.Mat: cv.gapi.CV_MAT,
|
||||
cv.GArray.GMat: cv.gapi.CV_GMAT,
|
||||
cv.GArray.Any: cv.gapi.CV_ANY
|
||||
}
|
||||
|
||||
gopaque_types= {
|
||||
cv.GOpaque.Size: cv.gapi.CV_SIZE,
|
||||
cv.GOpaque.Rect: cv.gapi.CV_RECT,
|
||||
cv.GOpaque.Bool: cv.gapi.CV_BOOL,
|
||||
cv.GOpaque.Int: cv.gapi.CV_INT,
|
||||
cv.GOpaque.Double: cv.gapi.CV_DOUBLE,
|
||||
cv.GOpaque.Float: cv.gapi.CV_FLOAT,
|
||||
cv.GOpaque.String: cv.gapi.CV_STRING,
|
||||
cv.GOpaque.Point: cv.gapi.CV_POINT,
|
||||
cv.GOpaque.Point2f: cv.gapi.CV_POINT2F,
|
||||
cv.GOpaque.Size: cv.gapi.CV_SIZE,
|
||||
cv.GOpaque.Rect: cv.gapi.CV_RECT,
|
||||
cv.GOpaque.Any: cv.gapi.CV_ANY
|
||||
}
|
||||
|
||||
type2str = {
|
||||
cv.gapi.CV_BOOL: 'cv.gapi.CV_BOOL' ,
|
||||
cv.gapi.CV_INT: 'cv.gapi.CV_INT' ,
|
||||
cv.gapi.CV_DOUBLE: 'cv.gapi.CV_DOUBLE' ,
|
||||
cv.gapi.CV_FLOAT: 'cv.gapi.CV_FLOAT' ,
|
||||
cv.gapi.CV_STRING: 'cv.gapi.CV_STRING' ,
|
||||
cv.gapi.CV_POINT: 'cv.gapi.CV_POINT' ,
|
||||
cv.gapi.CV_POINT2F: 'cv.gapi.CV_POINT2F' ,
|
||||
cv.gapi.CV_SIZE: 'cv.gapi.CV_SIZE',
|
||||
cv.gapi.CV_RECT: 'cv.gapi.CV_RECT',
|
||||
cv.gapi.CV_SCALAR: 'cv.gapi.CV_SCALAR',
|
||||
cv.gapi.CV_MAT: 'cv.gapi.CV_MAT',
|
||||
cv.gapi.CV_GMAT: 'cv.gapi.CV_GMAT'
|
||||
}
|
||||
|
||||
# NB: Second lvl decorator takes class to decorate
|
||||
def op_with_params(cls):
|
||||
if not in_types:
|
||||
raise Exception('{} operation should have at least one input!'.format(cls.__name__))
|
||||
|
||||
if not out_types:
|
||||
raise Exception('{} operation should have at least one output!'.format(cls.__name__))
|
||||
|
||||
for i, t in enumerate(out_types):
|
||||
if t not in [cv.GMat, cv.GScalar, *garray_types, *gopaque_types]:
|
||||
raise Exception('{} unsupported output type: {} in possition: {}'
|
||||
.format(cls.__name__, t.__name__, i))
|
||||
|
||||
def on(*args):
|
||||
if len(in_types) != len(args):
|
||||
raise Exception('Invalid number of input elements!\nExpected: {}, Actual: {}'
|
||||
.format(len(in_types), len(args)))
|
||||
|
||||
for i, (t, a) in enumerate(zip(in_types, args)):
|
||||
if t in garray_types:
|
||||
if not isinstance(a, cv.GArrayT):
|
||||
raise Exception("{} invalid type for argument {}.\nExpected: {}, Actual: {}"
|
||||
.format(cls.__name__, i, cv.GArrayT.__name__, type(a).__name__))
|
||||
|
||||
elif a.type() != garray_types[t]:
|
||||
raise Exception("{} invalid GArrayT type for argument {}.\nExpected: {}, Actual: {}"
|
||||
.format(cls.__name__, i, type2str[garray_types[t]], type2str[a.type()]))
|
||||
|
||||
elif t in gopaque_types:
|
||||
if not isinstance(a, cv.GOpaqueT):
|
||||
raise Exception("{} invalid type for argument {}.\nExpected: {}, Actual: {}"
|
||||
.format(cls.__name__, i, cv.GOpaqueT.__name__, type(a).__name__))
|
||||
|
||||
elif a.type() != gopaque_types[t]:
|
||||
raise Exception("{} invalid GOpaque type for argument {}.\nExpected: {}, Actual: {}"
|
||||
.format(cls.__name__, i, type2str[gopaque_types[t]], type2str[a.type()]))
|
||||
|
||||
else:
|
||||
if t != type(a):
|
||||
raise Exception('{} invalid input type for argument {}.\nExpected: {}, Actual: {}'
|
||||
.format(cls.__name__, i, t.__name__, type(a).__name__))
|
||||
|
||||
op = cv.gapi.__op(op_id, cls.outMeta, *args)
|
||||
|
||||
out_protos = []
|
||||
for i, out_type in enumerate(out_types):
|
||||
if out_type == cv.GMat:
|
||||
out_protos.append(op.getGMat())
|
||||
elif out_type == cv.GScalar:
|
||||
out_protos.append(op.getGScalar())
|
||||
elif out_type in gopaque_types:
|
||||
out_protos.append(op.getGOpaque(gopaque_types[out_type]))
|
||||
elif out_type in garray_types:
|
||||
out_protos.append(op.getGArray(garray_types[out_type]))
|
||||
else:
|
||||
raise Exception("""In {}: G-API operation can't produce the output with type: {} in position: {}"""
|
||||
.format(cls.__name__, out_type.__name__, i))
|
||||
|
||||
return tuple(out_protos) if len(out_protos) != 1 else out_protos[0]
|
||||
|
||||
# NB: Extend operation class
|
||||
cls.id = op_id
|
||||
cls.on = staticmethod(on)
|
||||
return cls
|
||||
|
||||
return op_with_params
|
||||
|
||||
|
||||
def kernel(op_cls):
|
||||
# NB: Second lvl decorator takes class to decorate
|
||||
def kernel_with_params(cls):
|
||||
# NB: Add new members to kernel class
|
||||
cls.id = op_cls.id
|
||||
cls.outMeta = op_cls.outMeta
|
||||
return cls
|
||||
|
||||
return kernel_with_params
|
||||
@ -5,7 +5,6 @@
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable: 4503) // "decorated name length exceeded"
|
||||
// on empty_meta(const cv::GMetaArgs&, const cv::GArgs&)
|
||||
#endif
|
||||
|
||||
#include <opencv2/gapi/cpu/gcpukernel.hpp>
|
||||
@ -49,6 +48,121 @@ using GArray_GMat = cv::GArray<cv::GMat>;
|
||||
// WA: Create using
|
||||
using std::string;
|
||||
|
||||
namespace cv
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class PyObjectHolder
|
||||
{
|
||||
public:
|
||||
PyObjectHolder(PyObject* o, bool owner = true);
|
||||
PyObject* get() const;
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
std::shared_ptr<Impl> m_impl;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace cv
|
||||
|
||||
class cv::detail::PyObjectHolder::Impl
|
||||
{
|
||||
public:
|
||||
Impl(PyObject* object, bool owner);
|
||||
PyObject* get() const;
|
||||
~Impl();
|
||||
|
||||
private:
|
||||
PyObject* m_object;
|
||||
};
|
||||
|
||||
cv::detail::PyObjectHolder::Impl::Impl(PyObject* object, bool owner)
|
||||
: m_object(object)
|
||||
{
|
||||
// NB: Become an owner of that PyObject.
|
||||
// Need to store this and get access
|
||||
// after the caller which provide the object is out of range.
|
||||
if (owner)
|
||||
{
|
||||
// NB: Impossible take ownership if object is NULL.
|
||||
GAPI_Assert(object);
|
||||
Py_INCREF(m_object);
|
||||
}
|
||||
}
|
||||
|
||||
cv::detail::PyObjectHolder::Impl::~Impl()
|
||||
{
|
||||
// NB: If NULL was set, don't decrease counter.
|
||||
if (m_object)
|
||||
{
|
||||
Py_DECREF(m_object);
|
||||
}
|
||||
}
|
||||
|
||||
PyObject* cv::detail::PyObjectHolder::Impl::get() const
|
||||
{
|
||||
return m_object;
|
||||
}
|
||||
|
||||
cv::detail::PyObjectHolder::PyObjectHolder(PyObject* object, bool owner)
|
||||
: m_impl(new cv::detail::PyObjectHolder::Impl{object, owner})
|
||||
{
|
||||
}
|
||||
|
||||
PyObject* cv::detail::PyObjectHolder::get() const
|
||||
{
|
||||
return m_impl->get();
|
||||
}
|
||||
|
||||
template<>
|
||||
PyObject* pyopencv_from(const cv::detail::PyObjectHolder& v)
|
||||
{
|
||||
PyObject* o = cv::util::any_cast<cv::detail::PyObjectHolder>(v).get();
|
||||
Py_INCREF(o);
|
||||
return o;
|
||||
}
|
||||
|
||||
template<>
|
||||
PyObject* pyopencv_from(const cv::GArg& value)
|
||||
{
|
||||
GAPI_Assert(value.kind != cv::detail::ArgKind::GOBJREF);
|
||||
#define HANDLE_CASE(T, O) case cv::detail::OpaqueKind::CV_##T: \
|
||||
{ \
|
||||
return pyopencv_from(value.get<O>()); \
|
||||
}
|
||||
|
||||
#define UNSUPPORTED(T) case cv::detail::OpaqueKind::CV_##T: break
|
||||
switch (value.opaque_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);
|
||||
HANDLE_CASE(UNKNOWN, cv::detail::PyObjectHolder);
|
||||
UNSUPPORTED(UINT64);
|
||||
UNSUPPORTED(DRAW_PRIM);
|
||||
#undef HANDLE_CASE
|
||||
#undef UNSUPPORTED
|
||||
}
|
||||
util::throw_error(std::logic_error("Unsupported kernel input type"));
|
||||
}
|
||||
|
||||
template<>
|
||||
bool pyopencv_to(PyObject* obj, cv::GArg& value, const ArgInfo& info)
|
||||
{
|
||||
value = cv::GArg(cv::detail::PyObjectHolder(obj));
|
||||
return true;
|
||||
}
|
||||
|
||||
template <>
|
||||
bool pyopencv_to(PyObject* obj, std::vector<GCompileArg>& value, const ArgInfo& info)
|
||||
{
|
||||
@ -81,7 +195,7 @@ PyObject* pyopencv_from(const cv::detail::OpaqueRef& o)
|
||||
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_UNKNOWN : return pyopencv_from(o.rref<cv::GArg>());
|
||||
case cv::detail::OpaqueKind::CV_UINT64 : break;
|
||||
case cv::detail::OpaqueKind::CV_SCALAR : break;
|
||||
case cv::detail::OpaqueKind::CV_MAT : break;
|
||||
@ -108,7 +222,7 @@ PyObject* pyopencv_from(const cv::detail::VectorRef& v)
|
||||
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_UNKNOWN : return pyopencv_from_generic_vec(v.rref<cv::GArg>());
|
||||
case cv::detail::OpaqueKind::CV_UINT64 : break;
|
||||
case cv::detail::OpaqueKind::CV_DRAW_PRIM : break;
|
||||
}
|
||||
@ -270,7 +384,7 @@ static cv::detail::OpaqueRef extract_opaque_ref(PyObject* from, cv::detail::Opaq
|
||||
HANDLE_CASE(POINT2F, cv::Point2f);
|
||||
HANDLE_CASE(SIZE, cv::Size);
|
||||
HANDLE_CASE(RECT, cv::Rect);
|
||||
UNSUPPORTED(UNKNOWN);
|
||||
HANDLE_CASE(UNKNOWN, cv::GArg);
|
||||
UNSUPPORTED(UINT64);
|
||||
UNSUPPORTED(SCALAR);
|
||||
UNSUPPORTED(MAT);
|
||||
@ -303,7 +417,7 @@ static cv::detail::VectorRef extract_vector_ref(PyObject* from, cv::detail::Opaq
|
||||
HANDLE_CASE(RECT, cv::Rect);
|
||||
HANDLE_CASE(SCALAR, cv::Scalar);
|
||||
HANDLE_CASE(MAT, cv::Mat);
|
||||
UNSUPPORTED(UNKNOWN);
|
||||
HANDLE_CASE(UNKNOWN, cv::GArg);
|
||||
UNSUPPORTED(UINT64);
|
||||
UNSUPPORTED(DRAW_PRIM);
|
||||
#undef HANDLE_CASE
|
||||
@ -415,38 +529,7 @@ static cv::GMetaArgs extract_meta_args(const cv::GTypesInfo& info, PyObject* py_
|
||||
return metas;
|
||||
}
|
||||
|
||||
inline PyObject* extract_opaque_value(const cv::GArg& value)
|
||||
{
|
||||
GAPI_Assert(value.kind != cv::detail::ArgKind::GOBJREF);
|
||||
#define HANDLE_CASE(T, O) case cv::detail::OpaqueKind::CV_##T: \
|
||||
{ \
|
||||
return pyopencv_from(value.get<O>()); \
|
||||
}
|
||||
|
||||
#define UNSUPPORTED(T) case cv::detail::OpaqueKind::CV_##T: break
|
||||
switch (value.opaque_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 kernel input type"));
|
||||
}
|
||||
|
||||
static cv::GRunArgs run_py_kernel(PyObject* kernel,
|
||||
static cv::GRunArgs run_py_kernel(cv::detail::PyObjectHolder kernel,
|
||||
const cv::gapi::python::GPythonContext &ctx)
|
||||
{
|
||||
const auto& ins = ctx.ins;
|
||||
@ -460,33 +543,32 @@ static cv::GRunArgs run_py_kernel(PyObject* kernel,
|
||||
try
|
||||
{
|
||||
int in_idx = 0;
|
||||
PyObject* args = PyTuple_New(ins.size());
|
||||
// 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);
|
||||
for (size_t i = 0; i < ins.size(); ++i)
|
||||
{
|
||||
// NB: If meta is monostate then object isn't associated with G-TYPE, so in case it
|
||||
// kind matches with supported types do conversion from c++ to python, if not (CV_UNKNOWN)
|
||||
// obtain PyObject* and pass as-is.
|
||||
// NB: If meta is monostate then object isn't associated with G-TYPE.
|
||||
if (cv::util::holds_alternative<cv::util::monostate>(in_metas[i]))
|
||||
{
|
||||
PyTuple_SetItem(args, i,
|
||||
ins[i].opaque_kind != cv::detail::OpaqueKind::CV_UNKNOWN ? extract_opaque_value(ins[i])
|
||||
: ins[i].get<PyObject*>());
|
||||
PyTuple_SetItem(args.get(), i, pyopencv_from(ins[i]));
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (in_metas[i].index())
|
||||
{
|
||||
case cv::GMetaArg::index_of<cv::GMatDesc>():
|
||||
PyTuple_SetItem(args, i, pyopencv_from(ins[i].get<cv::Mat>()));
|
||||
PyTuple_SetItem(args.get(), i, pyopencv_from(ins[i].get<cv::Mat>()));
|
||||
break;
|
||||
case cv::GMetaArg::index_of<cv::GScalarDesc>():
|
||||
PyTuple_SetItem(args, i, pyopencv_from(ins[i].get<cv::Scalar>()));
|
||||
PyTuple_SetItem(args.get(), i, pyopencv_from(ins[i].get<cv::Scalar>()));
|
||||
break;
|
||||
case cv::GMetaArg::index_of<cv::GOpaqueDesc>():
|
||||
PyTuple_SetItem(args, i, pyopencv_from(ins[i].get<cv::detail::OpaqueRef>()));
|
||||
PyTuple_SetItem(args.get(), i, pyopencv_from(ins[i].get<cv::detail::OpaqueRef>()));
|
||||
break;
|
||||
case cv::GMetaArg::index_of<cv::GArrayDesc>():
|
||||
PyTuple_SetItem(args, i, pyopencv_from(ins[i].get<cv::detail::VectorRef>()));
|
||||
PyTuple_SetItem(args.get(), i, pyopencv_from(ins[i].get<cv::detail::VectorRef>()));
|
||||
break;
|
||||
case cv::GMetaArg::index_of<cv::GFrameDesc>():
|
||||
util::throw_error(std::logic_error("GFrame isn't supported for custom operation"));
|
||||
@ -494,11 +576,21 @@ static cv::GRunArgs run_py_kernel(PyObject* kernel,
|
||||
}
|
||||
++in_idx;
|
||||
}
|
||||
// NB: Doesn't increase reference counter (false).
|
||||
// In case PyObject_CallObject return NULL, do nothing in destructor.
|
||||
cv::detail::PyObjectHolder result(
|
||||
PyObject_CallObject(kernel.get(), args.get()), false);
|
||||
|
||||
PyObject* result = PyObject_CallObject(kernel, args);
|
||||
if (PyErr_Occurred()) {
|
||||
PyErr_PrintEx(0);
|
||||
PyErr_Clear();
|
||||
throw std::logic_error("Python kernel failed with error!");
|
||||
}
|
||||
// NB: In fact it's impossible situation, becase errors were handled above.
|
||||
GAPI_Assert(result.get() && "Python kernel returned NULL!");
|
||||
|
||||
outs = out_info.size() == 1 ? cv::GRunArgs{extract_run_arg(out_info[0], result)}
|
||||
: extract_run_args(out_info, result);
|
||||
outs = out_info.size() == 1 ? cv::GRunArgs{extract_run_arg(out_info[0], result.get())}
|
||||
: extract_run_args(out_info, result.get());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
@ -510,12 +602,6 @@ static cv::GRunArgs run_py_kernel(PyObject* kernel,
|
||||
return outs;
|
||||
}
|
||||
|
||||
// FIXME: Now it's impossible to obtain meta function from operation,
|
||||
// because kernel connects to operation only by id (string).
|
||||
static cv::GMetaArgs empty_meta(const cv::GMetaArgs &, const cv::GArgs &) {
|
||||
return {};
|
||||
}
|
||||
|
||||
static GMetaArg get_meta_arg(PyObject* obj)
|
||||
{
|
||||
if (PyObject_TypeCheck(obj,
|
||||
@ -558,33 +644,38 @@ static cv::GMetaArgs get_meta_args(PyObject* tuple)
|
||||
return metas;
|
||||
}
|
||||
|
||||
static GMetaArgs python_meta(PyObject* outMeta, const cv::GMetaArgs &meta, const cv::GArgs &gargs) {
|
||||
static GMetaArgs run_py_meta(cv::detail::PyObjectHolder out_meta,
|
||||
const cv::GMetaArgs &meta,
|
||||
const cv::GArgs &gargs) {
|
||||
PyGILState_STATE gstate;
|
||||
gstate = PyGILState_Ensure();
|
||||
|
||||
cv::GMetaArgs out_metas;
|
||||
try
|
||||
{
|
||||
PyObject* args = PyTuple_New(meta.size());
|
||||
// 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, idx, pyopencv_from(cv::util::get<cv::GMatDesc>(m)));
|
||||
PyTuple_SetItem(args.get(), idx, pyopencv_from(cv::util::get<cv::GMatDesc>(m)));
|
||||
break;
|
||||
case cv::GMetaArg::index_of<cv::GScalarDesc>():
|
||||
PyTuple_SetItem(args, idx, pyopencv_from(cv::util::get<cv::GScalarDesc>(m)));
|
||||
PyTuple_SetItem(args.get(), idx, pyopencv_from(cv::util::get<cv::GScalarDesc>(m)));
|
||||
break;
|
||||
case cv::GMetaArg::index_of<cv::GArrayDesc>():
|
||||
PyTuple_SetItem(args, idx, pyopencv_from(cv::util::get<cv::GArrayDesc>(m)));
|
||||
PyTuple_SetItem(args.get(), idx, pyopencv_from(cv::util::get<cv::GArrayDesc>(m)));
|
||||
break;
|
||||
case cv::GMetaArg::index_of<cv::GOpaqueDesc>():
|
||||
PyTuple_SetItem(args, idx, pyopencv_from(cv::util::get<cv::GOpaqueDesc>(m)));
|
||||
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, idx, gargs[idx].get<PyObject*>());
|
||||
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"));
|
||||
@ -592,9 +683,21 @@ static GMetaArgs python_meta(PyObject* outMeta, const cv::GMetaArgs &meta, const
|
||||
}
|
||||
++idx;
|
||||
}
|
||||
PyObject* result = PyObject_CallObject(outMeta, args);
|
||||
out_metas = PyTuple_Check(result) ? get_meta_args(result)
|
||||
: cv::GMetaArgs{get_meta_arg(result)};
|
||||
// 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 outMeta failed with error!");
|
||||
}
|
||||
// NB: In fact it's impossible situation, becase errors were handled above.
|
||||
GAPI_Assert(result.get() && "Python outMeta returned NULL!");
|
||||
|
||||
out_metas = PyTuple_Check(result.get()) ? get_meta_args(result.get())
|
||||
: cv::GMetaArgs{get_meta_arg(result.get())};
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
@ -611,23 +714,43 @@ static PyObject* pyopencv_cv_gapi_kernels(PyObject* , PyObject* py_args, PyObjec
|
||||
using namespace cv;
|
||||
gapi::GKernelPackage pkg;
|
||||
Py_ssize_t size = PyTuple_Size(py_args);
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
PyObject* pair = PyTuple_GetItem(py_args, i);
|
||||
PyObject* kernel = PyTuple_GetItem(pair, 0);
|
||||
PyObject* user_kernel = PyTuple_GetItem(py_args, i);
|
||||
|
||||
std::string id;
|
||||
if (!pyopencv_to(PyTuple_GetItem(pair, 1), id, ArgInfo("id", false)))
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError, "Failed to obtain: kernel id must be a string");
|
||||
PyObject* id_obj = PyObject_GetAttrString(user_kernel, "id");
|
||||
if (!id_obj) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"Python kernel should contain id, please use cv.gapi.kernel to define kernel");
|
||||
return NULL;
|
||||
}
|
||||
Py_INCREF(kernel);
|
||||
|
||||
PyObject* out_meta = PyObject_GetAttrString(user_kernel, "outMeta");
|
||||
if (!out_meta) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"Python kernel should contain outMeta, please use cv.gapi.kernel to define kernel");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject* run = PyObject_GetAttrString(user_kernel, "run");
|
||||
if (!run) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"Python kernel should contain run, please use cv.gapi.kernel to define kernel");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
std::string id;
|
||||
if (!pyopencv_to(id_obj, id, ArgInfo("id", false)))
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError, "Failed to obtain string");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
using namespace std::placeholders;
|
||||
gapi::python::GPythonFunctor f(id.c_str(),
|
||||
empty_meta,
|
||||
std::bind(run_py_kernel,
|
||||
kernel,
|
||||
std::placeholders::_1));
|
||||
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);
|
||||
@ -644,7 +767,6 @@ static PyObject* pyopencv_cv_gapi_op(PyObject* , PyObject* py_args, PyObject*)
|
||||
return NULL;
|
||||
}
|
||||
PyObject* outMeta = PyTuple_GetItem(py_args, 1);
|
||||
Py_INCREF(outMeta);
|
||||
|
||||
cv::GArgs args;
|
||||
for (int i = 2; i < size; i++)
|
||||
@ -684,13 +806,12 @@ static PyObject* pyopencv_cv_gapi_op(PyObject* , PyObject* py_args, PyObject*)
|
||||
}
|
||||
else
|
||||
{
|
||||
Py_INCREF(item);
|
||||
args.emplace_back(cv::GArg(item));
|
||||
args.emplace_back(cv::GArg(cv::detail::PyObjectHolder{item}));
|
||||
}
|
||||
}
|
||||
|
||||
cv::GKernel::M outMetaWrapper = std::bind(python_meta,
|
||||
outMeta,
|
||||
cv::GKernel::M outMetaWrapper = std::bind(run_py_meta,
|
||||
cv::detail::PyObjectHolder{outMeta},
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2);
|
||||
return pyopencv_from(cv::gapi::wip::op(id, outMetaWrapper, std::move(args)));
|
||||
@ -698,7 +819,7 @@ static PyObject* pyopencv_cv_gapi_op(PyObject* , PyObject* py_args, PyObject*)
|
||||
|
||||
static PyObject* pyopencv_cv_gin(PyObject*, PyObject* py_args, PyObject*)
|
||||
{
|
||||
Py_INCREF(py_args);
|
||||
cv::detail::PyObjectHolder holder{py_args};
|
||||
auto callback = cv::detail::ExtractArgsCallback{[=](const cv::GTypesInfo& info)
|
||||
{
|
||||
PyGILState_STATE gstate;
|
||||
@ -707,7 +828,7 @@ static PyObject* pyopencv_cv_gin(PyObject*, PyObject* py_args, PyObject*)
|
||||
cv::GRunArgs args;
|
||||
try
|
||||
{
|
||||
args = extract_run_args(info, py_args);
|
||||
args = extract_run_args(info, holder.get());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
@ -792,10 +913,10 @@ struct PyOpenCV_Converter<cv::GOpaque<T>>
|
||||
};
|
||||
|
||||
|
||||
// extend cv.gapi.wip. methods
|
||||
#define PYOPENCV_EXTRA_METHODS_GAPI_WIP \
|
||||
// extend cv.gapi methods
|
||||
#define PYOPENCV_EXTRA_METHODS_GAPI \
|
||||
{"kernels", CV_PY_FN_WITH_KW(pyopencv_cv_gapi_kernels), "kernels(...) -> GKernelPackage"}, \
|
||||
{"op", CV_PY_FN_WITH_KW_(pyopencv_cv_gapi_op, 0), "kernels(...) -> retval\n"}, \
|
||||
{"__op", CV_PY_FN_WITH_KW(pyopencv_cv_gapi_op), "__op(...) -> retval\n"},
|
||||
|
||||
|
||||
#endif // HAVE_OPENCV_GAPI
|
||||
|
||||
@ -25,29 +25,31 @@
|
||||
}
|
||||
|
||||
#define GARRAY_TYPE_LIST_G(G, G2) \
|
||||
WRAP_ARGS(bool , cv::gapi::ArgType::CV_BOOL, G) \
|
||||
WRAP_ARGS(int , cv::gapi::ArgType::CV_INT, G) \
|
||||
WRAP_ARGS(double , cv::gapi::ArgType::CV_DOUBLE, G) \
|
||||
WRAP_ARGS(float , cv::gapi::ArgType::CV_FLOAT, G) \
|
||||
WRAP_ARGS(std::string , cv::gapi::ArgType::CV_STRING, G) \
|
||||
WRAP_ARGS(cv::Point , cv::gapi::ArgType::CV_POINT, G) \
|
||||
WRAP_ARGS(cv::Point2f , cv::gapi::ArgType::CV_POINT2F, G) \
|
||||
WRAP_ARGS(cv::Size , cv::gapi::ArgType::CV_SIZE, G) \
|
||||
WRAP_ARGS(cv::Rect , cv::gapi::ArgType::CV_RECT, G) \
|
||||
WRAP_ARGS(cv::Scalar , cv::gapi::ArgType::CV_SCALAR, G) \
|
||||
WRAP_ARGS(cv::Mat , cv::gapi::ArgType::CV_MAT, G) \
|
||||
WRAP_ARGS(cv::GMat , cv::gapi::ArgType::CV_GMAT, G2)
|
||||
WRAP_ARGS(bool , cv::gapi::ArgType::CV_BOOL, G) \
|
||||
WRAP_ARGS(int , cv::gapi::ArgType::CV_INT, G) \
|
||||
WRAP_ARGS(double , cv::gapi::ArgType::CV_DOUBLE, G) \
|
||||
WRAP_ARGS(float , cv::gapi::ArgType::CV_FLOAT, G) \
|
||||
WRAP_ARGS(std::string , cv::gapi::ArgType::CV_STRING, G) \
|
||||
WRAP_ARGS(cv::Point , cv::gapi::ArgType::CV_POINT, G) \
|
||||
WRAP_ARGS(cv::Point2f , cv::gapi::ArgType::CV_POINT2F, G) \
|
||||
WRAP_ARGS(cv::Size , cv::gapi::ArgType::CV_SIZE, G) \
|
||||
WRAP_ARGS(cv::Rect , cv::gapi::ArgType::CV_RECT, G) \
|
||||
WRAP_ARGS(cv::Scalar , cv::gapi::ArgType::CV_SCALAR, G) \
|
||||
WRAP_ARGS(cv::Mat , cv::gapi::ArgType::CV_MAT, G) \
|
||||
WRAP_ARGS(cv::GArg , cv::gapi::ArgType::CV_ANY, G) \
|
||||
WRAP_ARGS(cv::GMat , cv::gapi::ArgType::CV_GMAT, G2) \
|
||||
|
||||
#define GOPAQUE_TYPE_LIST_G(G, G2) \
|
||||
WRAP_ARGS(bool , cv::gapi::ArgType::CV_BOOL, G) \
|
||||
WRAP_ARGS(int , cv::gapi::ArgType::CV_INT, G) \
|
||||
WRAP_ARGS(double , cv::gapi::ArgType::CV_DOUBLE, G) \
|
||||
WRAP_ARGS(float , cv::gapi::ArgType::CV_FLOAT, G) \
|
||||
WRAP_ARGS(std::string , cv::gapi::ArgType::CV_STRING, G) \
|
||||
WRAP_ARGS(cv::Point , cv::gapi::ArgType::CV_POINT, G) \
|
||||
WRAP_ARGS(cv::Point2f , cv::gapi::ArgType::CV_POINT2F, G) \
|
||||
WRAP_ARGS(cv::Size , cv::gapi::ArgType::CV_SIZE, G) \
|
||||
WRAP_ARGS(cv::Rect , cv::gapi::ArgType::CV_RECT, G2) \
|
||||
WRAP_ARGS(bool , cv::gapi::ArgType::CV_BOOL, G) \
|
||||
WRAP_ARGS(int , cv::gapi::ArgType::CV_INT, G) \
|
||||
WRAP_ARGS(double , cv::gapi::ArgType::CV_DOUBLE, G) \
|
||||
WRAP_ARGS(float , cv::gapi::ArgType::CV_FLOAT, G) \
|
||||
WRAP_ARGS(std::string , cv::gapi::ArgType::CV_STRING, G) \
|
||||
WRAP_ARGS(cv::Point , cv::gapi::ArgType::CV_POINT, G) \
|
||||
WRAP_ARGS(cv::Point2f , cv::gapi::ArgType::CV_POINT2F, G) \
|
||||
WRAP_ARGS(cv::Size , cv::gapi::ArgType::CV_SIZE, G) \
|
||||
WRAP_ARGS(cv::GArg , cv::gapi::ArgType::CV_ANY, G) \
|
||||
WRAP_ARGS(cv::Rect , cv::gapi::ArgType::CV_RECT, G2) \
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
@ -66,6 +68,7 @@ enum ArgType {
|
||||
CV_SCALAR,
|
||||
CV_MAT,
|
||||
CV_GMAT,
|
||||
CV_ANY,
|
||||
};
|
||||
|
||||
GAPI_EXPORTS_W inline cv::GInferOutputs infer(const String& name, const cv::GInferInputs& inputs)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -218,6 +218,28 @@ if(NOT OPENCV_SKIP_PYTHON_LOADER)
|
||||
endif()
|
||||
configure_file("${PYTHON_SOURCE_DIR}/package/template/config-x.y.py.in" "${__python_loader_install_tmp_path}/cv2/${__target_config}" @ONLY)
|
||||
install(FILES "${__python_loader_install_tmp_path}/cv2/${__target_config}" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/" COMPONENT python)
|
||||
|
||||
# handle Python extra code
|
||||
foreach(m ${OPENCV_MODULES_BUILD})
|
||||
if (";${OPENCV_MODULE_${m}_WRAPPERS};" MATCHES ";python;" AND HAVE_${m}
|
||||
AND EXISTS "${OPENCV_MODULE_${m}_LOCATION}/misc/python/package"
|
||||
)
|
||||
set(__base "${OPENCV_MODULE_${m}_LOCATION}/misc/python/package")
|
||||
file(GLOB_RECURSE extra_py_files
|
||||
RELATIVE "${__base}"
|
||||
"${__base}/**/*.py"
|
||||
)
|
||||
if(extra_py_files)
|
||||
list(SORT extra_py_files)
|
||||
foreach(f ${extra_py_files})
|
||||
configure_file("${__base}/${f}" "${__loader_path}/cv2/_extra_py_code/${f}" COPYONLY)
|
||||
install(FILES "${__base}/${f}" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/_extra_py_code/${f}" COMPONENT python)
|
||||
endforeach()
|
||||
else()
|
||||
message(WARNING "Module ${m} has no .py files in misc/python/package")
|
||||
endif()
|
||||
endif()
|
||||
endforeach(m)
|
||||
endif() # NOT OPENCV_SKIP_PYTHON_LOADER
|
||||
|
||||
unset(PYTHON_SRC_DIR)
|
||||
|
||||
@ -4,6 +4,8 @@ OpenCV Python binary extension loader
|
||||
import os
|
||||
import sys
|
||||
|
||||
__all__ = []
|
||||
|
||||
try:
|
||||
import numpy
|
||||
import numpy.core.multiarray
|
||||
@ -13,6 +15,14 @@ except ImportError:
|
||||
print(' pip install numpy')
|
||||
raise
|
||||
|
||||
|
||||
py_code_loader = None
|
||||
if sys.version_info[:2] >= (3, 0):
|
||||
try:
|
||||
from . import _extra_py_code as py_code_loader
|
||||
except:
|
||||
pass
|
||||
|
||||
# TODO
|
||||
# is_x64 = sys.maxsize > 2**32
|
||||
|
||||
@ -97,6 +107,11 @@ def bootstrap():
|
||||
except:
|
||||
pass
|
||||
|
||||
if DEBUG: print('OpenCV loader: binary extension... OK')
|
||||
|
||||
if py_code_loader:
|
||||
py_code_loader.init('cv2')
|
||||
|
||||
if DEBUG: print('OpenCV loader: DONE')
|
||||
|
||||
bootstrap()
|
||||
|
||||
53
modules/python/package/cv2/_extra_py_code/__init__.py
Normal file
53
modules/python/package/cv2/_extra_py_code/__init__.py
Normal file
@ -0,0 +1,53 @@
|
||||
import sys
|
||||
import importlib
|
||||
|
||||
__all__ = ['init']
|
||||
|
||||
|
||||
DEBUG = False
|
||||
if hasattr(sys, 'OpenCV_LOADER_DEBUG'):
|
||||
DEBUG = True
|
||||
|
||||
|
||||
def _load_py_code(base, name):
|
||||
try:
|
||||
m = importlib.import_module(__name__ + name)
|
||||
except ImportError:
|
||||
return # extension doesn't exist?
|
||||
|
||||
if DEBUG: print('OpenCV loader: added python code extension for: ' + name)
|
||||
|
||||
if hasattr(m, '__all__'):
|
||||
export_members = { k : getattr(m, k) for k in m.__all__ }
|
||||
else:
|
||||
export_members = m.__dict__
|
||||
|
||||
for k, v in export_members.items():
|
||||
if k.startswith('_'): # skip internals
|
||||
continue
|
||||
if isinstance(v, type(sys)): # don't bring modules
|
||||
continue
|
||||
if DEBUG: print(' symbol: {} = {}'.format(k, v))
|
||||
setattr(sys.modules[base + name ], k, v)
|
||||
|
||||
del sys.modules[__name__ + name]
|
||||
|
||||
|
||||
# TODO: listdir
|
||||
def init(base):
|
||||
_load_py_code(base, '.cv2') # special case
|
||||
prefix = base
|
||||
prefix_len = len(prefix)
|
||||
|
||||
modules = [ m for m in sys.modules.keys() if m.startswith(prefix) ]
|
||||
for m in modules:
|
||||
m2 = m[prefix_len:] # strip prefix
|
||||
if len(m2) == 0:
|
||||
continue
|
||||
if m2.startswith('._'): # skip internals
|
||||
continue
|
||||
if m2.startswith('.load_config_'): # skip helper files
|
||||
continue
|
||||
_load_py_code(base, m2)
|
||||
|
||||
del sys.modules[__name__]
|
||||
@ -25,10 +25,12 @@ endif()
|
||||
set(PYTHON_LOADER_FILES
|
||||
"setup.py" "cv2/__init__.py"
|
||||
"cv2/load_config_py2.py" "cv2/load_config_py3.py"
|
||||
"cv2/_extra_py_code/__init__.py"
|
||||
)
|
||||
foreach(fname ${PYTHON_LOADER_FILES})
|
||||
get_filename_component(__dir "${fname}" DIRECTORY)
|
||||
file(COPY "${PYTHON_SOURCE_DIR}/package/${fname}" DESTINATION "${__loader_path}/${__dir}")
|
||||
# avoid using of file(COPY) to rerun CMake on changes
|
||||
configure_file("${PYTHON_SOURCE_DIR}/package/${fname}" "${__loader_path}/${fname}" COPYONLY)
|
||||
if(fname STREQUAL "setup.py")
|
||||
if(OPENCV_PYTHON_SETUP_PY_INSTALL_PATH)
|
||||
install(FILES "${PYTHON_SOURCE_DIR}/package/${fname}" DESTINATION "${OPENCV_PYTHON_SETUP_PY_INSTALL_PATH}" COMPONENT python)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user