Limited API support for Python3
This commit is contained in:
+248
-314
@@ -5,12 +5,36 @@
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable:5033) // 'register' is no longer a supported storage class
|
||||
#endif
|
||||
|
||||
// #define CVPY_DYNAMIC_INIT
|
||||
// #define Py_DEBUG
|
||||
|
||||
#if defined(CVPY_DYNAMIC_INIT) && !defined(Py_DEBUG)
|
||||
# define Py_LIMITED_API 0x03030000
|
||||
#endif
|
||||
|
||||
#include <math.h>
|
||||
#include <Python.h>
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#undef CVPY_DYNAMIC_INIT
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER > 1800)
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#define MODULESTR "cv2"
|
||||
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
||||
|
||||
#include <numpy/ndarrayobject.h>
|
||||
|
||||
#include "pyopencv_generated_include.h"
|
||||
#include "opencv2/core/types_c.h"
|
||||
#include "opencv2/opencv_modules.hpp"
|
||||
#include "pycompat.hpp"
|
||||
#include <map>
|
||||
|
||||
#include <type_traits> // std::enable_if
|
||||
|
||||
template<typename T, class TEnable = void> // TEnable is used for SFINAE checks
|
||||
@@ -26,95 +50,6 @@ bool pyopencv_to(PyObject* obj, T& p, const char* name = "<unknown>") { return P
|
||||
template<typename T> static
|
||||
PyObject* pyopencv_from(const T& src) { return PyOpenCV_Converter<T>::from(src); }
|
||||
|
||||
|
||||
#define CV_PY_FN_WITH_KW_(fn, flags) (PyCFunction)(void*)(PyCFunctionWithKeywords)(fn), (flags) | METH_VARARGS | METH_KEYWORDS
|
||||
#define CV_PY_FN_NOARGS_(fn, flags) (PyCFunction)(fn), (flags) | METH_NOARGS
|
||||
|
||||
#define CV_PY_FN_WITH_KW(fn) CV_PY_FN_WITH_KW_(fn, 0)
|
||||
#define CV_PY_FN_NOARGS(fn) CV_PY_FN_NOARGS_(fn, 0)
|
||||
|
||||
|
||||
#define MODULESTR "cv2"
|
||||
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
||||
#include <numpy/ndarrayobject.h>
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
# define CV_PYTHON_TYPE_HEAD_INIT() PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||
#else
|
||||
# define CV_PYTHON_TYPE_HEAD_INIT() PyObject_HEAD_INIT(&PyType_Type) 0,
|
||||
#endif
|
||||
|
||||
#define CV_PY_TO_CLASS(TYPE) \
|
||||
template<> \
|
||||
bool pyopencv_to(PyObject* dst, TYPE& src, const char* name) \
|
||||
{ \
|
||||
if (!dst || dst == Py_None) \
|
||||
return true; \
|
||||
Ptr<TYPE> ptr; \
|
||||
\
|
||||
if (!pyopencv_to(dst, ptr, name)) return false; \
|
||||
src = *ptr; \
|
||||
return true; \
|
||||
}
|
||||
|
||||
#define CV_PY_FROM_CLASS(TYPE) \
|
||||
template<> \
|
||||
PyObject* pyopencv_from(const TYPE& src) \
|
||||
{ \
|
||||
Ptr<TYPE> ptr(new TYPE()); \
|
||||
\
|
||||
*ptr = src; \
|
||||
return pyopencv_from(ptr); \
|
||||
}
|
||||
|
||||
#define CV_PY_TO_CLASS_PTR(TYPE) \
|
||||
template<> \
|
||||
bool pyopencv_to(PyObject* dst, TYPE*& src, const char* name) \
|
||||
{ \
|
||||
if (!dst || dst == Py_None) \
|
||||
return true; \
|
||||
Ptr<TYPE> ptr; \
|
||||
\
|
||||
if (!pyopencv_to(dst, ptr, name)) return false; \
|
||||
src = ptr; \
|
||||
return true; \
|
||||
}
|
||||
|
||||
#define CV_PY_FROM_CLASS_PTR(TYPE) \
|
||||
static PyObject* pyopencv_from(TYPE*& src) \
|
||||
{ \
|
||||
return pyopencv_from(Ptr<TYPE>(src)); \
|
||||
}
|
||||
|
||||
#define CV_PY_TO_ENUM(TYPE) \
|
||||
template<> \
|
||||
bool pyopencv_to(PyObject* dst, TYPE& src, const char* name) \
|
||||
{ \
|
||||
if (!dst || dst == Py_None) \
|
||||
return true; \
|
||||
int underlying = 0; \
|
||||
\
|
||||
if (!pyopencv_to(dst, underlying, name)) return false; \
|
||||
src = static_cast<TYPE>(underlying); \
|
||||
return true; \
|
||||
}
|
||||
|
||||
#define CV_PY_FROM_ENUM(TYPE) \
|
||||
template<> \
|
||||
PyObject* pyopencv_from(const TYPE& src) \
|
||||
{ \
|
||||
return pyopencv_from(static_cast<int>(src)); \
|
||||
}
|
||||
|
||||
#include "pyopencv_generated_include.h"
|
||||
#include "opencv2/core/types_c.h"
|
||||
|
||||
#include "opencv2/opencv_modules.hpp"
|
||||
|
||||
#include "pycompat.hpp"
|
||||
|
||||
#include <map>
|
||||
|
||||
static PyObject* opencv_error = NULL;
|
||||
|
||||
static int failmsg(const char *fmt, ...)
|
||||
@@ -339,7 +274,7 @@ static bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo info)
|
||||
m = Mat(sz, 1, CV_64F);
|
||||
for( i = 0; i < sz; i++ )
|
||||
{
|
||||
PyObject* oi = PyTuple_GET_ITEM(o, i);
|
||||
PyObject* oi = PyTuple_GetItem(o, i);
|
||||
if( PyInt_Check(oi) )
|
||||
m.at<double>(i) = (double)PyInt_AsLong(oi);
|
||||
else if( PyFloat_Check(oi) )
|
||||
@@ -573,21 +508,26 @@ static PyObject* pyopencv_from(void*& ptr)
|
||||
return PyLong_FromVoidPtr(ptr);
|
||||
}
|
||||
|
||||
struct SafeSeqItem
|
||||
{
|
||||
PyObject * item;
|
||||
SafeSeqItem(PyObject *obj, size_t idx) { item = PySequence_GetItem(obj, idx); }
|
||||
~SafeSeqItem() { Py_XDECREF(item); }
|
||||
};
|
||||
|
||||
static bool pyopencv_to(PyObject *o, Scalar& s, const ArgInfo info)
|
||||
{
|
||||
if(!o || o == Py_None)
|
||||
return true;
|
||||
if (PySequence_Check(o)) {
|
||||
PyObject *fi = PySequence_Fast(o, info.name);
|
||||
if (fi == NULL)
|
||||
return false;
|
||||
if (4 < PySequence_Fast_GET_SIZE(fi))
|
||||
if (4 < PySequence_Size(o))
|
||||
{
|
||||
failmsg("Scalar value for argument '%s' is longer than 4", info.name);
|
||||
return false;
|
||||
}
|
||||
for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(fi); i++) {
|
||||
PyObject *item = PySequence_Fast_GET_ITEM(fi, i);
|
||||
for (Py_ssize_t i = 0; i < PySequence_Size(o); i++) {
|
||||
SafeSeqItem item_wrap(o, i);
|
||||
PyObject *item = item_wrap.item;
|
||||
if (PyFloat_Check(item) || PyInt_Check(item)) {
|
||||
s[(int)i] = PyFloat_AsDouble(item);
|
||||
} else {
|
||||
@@ -595,7 +535,6 @@ static bool pyopencv_to(PyObject *o, Scalar& s, const ArgInfo info)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Py_DECREF(fi);
|
||||
} else {
|
||||
if (PyFloat_Check(o) || PyInt_Check(o)) {
|
||||
s[0] = PyFloat_AsDouble(o);
|
||||
@@ -769,16 +708,18 @@ PyObject* pyopencv_from(const String& value)
|
||||
}
|
||||
|
||||
template<>
|
||||
bool pyopencv_to(PyObject* obj, String& value, const char* name)
|
||||
bool pyopencv_to(PyObject* obj, String &value, const char* name)
|
||||
{
|
||||
CV_UNUSED(name);
|
||||
if(!obj || obj == Py_None)
|
||||
return true;
|
||||
const char* str = PyString_AsString(obj);
|
||||
if(!str)
|
||||
return false;
|
||||
value = String(str);
|
||||
return true;
|
||||
std::string str;
|
||||
if (getUnicodeString(obj, str))
|
||||
{
|
||||
value = str;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template<>
|
||||
@@ -849,36 +790,31 @@ bool pyopencv_to(PyObject* obj, Range& r, const char* name)
|
||||
return true;
|
||||
while (PySequence_Check(obj))
|
||||
{
|
||||
PyObject *fi = PySequence_Fast(obj, name);
|
||||
if (fi == NULL)
|
||||
break;
|
||||
if (2 != PySequence_Fast_GET_SIZE(fi))
|
||||
if (2 != PySequence_Size(obj))
|
||||
{
|
||||
failmsg("Range value for argument '%s' is longer than 2", name);
|
||||
Py_DECREF(fi);
|
||||
return false;
|
||||
}
|
||||
{
|
||||
PyObject *item = PySequence_Fast_GET_ITEM(fi, 0);
|
||||
SafeSeqItem item_wrap(obj, 0);
|
||||
PyObject *item = item_wrap.item;
|
||||
if (PyInt_Check(item)) {
|
||||
r.start = (int)PyInt_AsLong(item);
|
||||
} else {
|
||||
failmsg("Range.start value for argument '%s' is not integer", name);
|
||||
Py_DECREF(fi);
|
||||
break;
|
||||
}
|
||||
}
|
||||
{
|
||||
PyObject *item = PySequence_Fast_GET_ITEM(fi, 1);
|
||||
SafeSeqItem item_wrap(obj, 1);
|
||||
PyObject *item = item_wrap.item;
|
||||
if (PyInt_Check(item)) {
|
||||
r.end = (int)PyInt_AsLong(item);
|
||||
} else {
|
||||
failmsg("Range.end value for argument '%s' is not integer", name);
|
||||
Py_DECREF(fi);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Py_DECREF(fi);
|
||||
return true;
|
||||
}
|
||||
if(PyObject_Size(obj) == 0)
|
||||
@@ -901,11 +837,10 @@ bool pyopencv_to(PyObject* obj, Point& p, const char* name)
|
||||
CV_UNUSED(name);
|
||||
if(!obj || obj == Py_None)
|
||||
return true;
|
||||
if(!!PyComplex_CheckExact(obj))
|
||||
if(PyComplex_Check(obj))
|
||||
{
|
||||
Py_complex c = PyComplex_AsCComplex(obj);
|
||||
p.x = saturate_cast<int>(c.real);
|
||||
p.y = saturate_cast<int>(c.imag);
|
||||
p.x = saturate_cast<int>(PyComplex_RealAsDouble(obj));
|
||||
p.y = saturate_cast<int>(PyComplex_ImagAsDouble(obj));
|
||||
return true;
|
||||
}
|
||||
return PyArg_ParseTuple(obj, "ii", &p.x, &p.y) > 0;
|
||||
@@ -917,11 +852,10 @@ bool pyopencv_to(PyObject* obj, Point2f& p, const char* name)
|
||||
CV_UNUSED(name);
|
||||
if(!obj || obj == Py_None)
|
||||
return true;
|
||||
if(!!PyComplex_CheckExact(obj))
|
||||
if (PyComplex_Check(obj))
|
||||
{
|
||||
Py_complex c = PyComplex_AsCComplex(obj);
|
||||
p.x = saturate_cast<float>(c.real);
|
||||
p.y = saturate_cast<float>(c.imag);
|
||||
p.x = saturate_cast<float>(PyComplex_RealAsDouble(obj));
|
||||
p.y = saturate_cast<float>(PyComplex_ImagAsDouble(obj));
|
||||
return true;
|
||||
}
|
||||
return PyArg_ParseTuple(obj, "ff", &p.x, &p.y) > 0;
|
||||
@@ -933,11 +867,10 @@ bool pyopencv_to(PyObject* obj, Point2d& p, const char* name)
|
||||
CV_UNUSED(name);
|
||||
if(!obj || obj == Py_None)
|
||||
return true;
|
||||
if(!!PyComplex_CheckExact(obj))
|
||||
if(PyComplex_Check(obj))
|
||||
{
|
||||
Py_complex c = PyComplex_AsCComplex(obj);
|
||||
p.x = saturate_cast<double>(c.real);
|
||||
p.y = saturate_cast<double>(c.imag);
|
||||
p.x = PyComplex_RealAsDouble(obj);
|
||||
p.y = PyComplex_ImagAsDouble(obj);
|
||||
return true;
|
||||
}
|
||||
return PyArg_ParseTuple(obj, "dd", &p.x, &p.y) > 0;
|
||||
@@ -1164,9 +1097,41 @@ PyObject* pyopencv_from(const Point3d& p)
|
||||
|
||||
template<typename _Tp> struct pyopencvVecConverter
|
||||
{
|
||||
typedef typename DataType<_Tp>::channel_type _Cp;
|
||||
static inline bool copyOneItem(PyObject *obj, size_t start, int channels, _Cp * data)
|
||||
{
|
||||
for(size_t j = 0; (int)j < channels; j++ )
|
||||
{
|
||||
SafeSeqItem sub_item_wrap(obj, start + j);
|
||||
PyObject* item_ij = sub_item_wrap.item;
|
||||
if( PyInt_Check(item_ij))
|
||||
{
|
||||
int v = (int)PyInt_AsLong(item_ij);
|
||||
if( v == -1 && PyErr_Occurred() )
|
||||
return false;
|
||||
data[j] = saturate_cast<_Cp>(v);
|
||||
}
|
||||
else if( PyLong_Check(item_ij))
|
||||
{
|
||||
int v = (int)PyLong_AsLong(item_ij);
|
||||
if( v == -1 && PyErr_Occurred() )
|
||||
return false;
|
||||
data[j] = saturate_cast<_Cp>(v);
|
||||
}
|
||||
else if( PyFloat_Check(item_ij))
|
||||
{
|
||||
double v = PyFloat_AsDouble(item_ij);
|
||||
if( PyErr_Occurred() )
|
||||
return false;
|
||||
data[j] = saturate_cast<_Cp>(v);
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
static bool to(PyObject* obj, std::vector<_Tp>& value, const ArgInfo info)
|
||||
{
|
||||
typedef typename DataType<_Tp>::channel_type _Cp;
|
||||
if(!obj || obj == Py_None)
|
||||
return true;
|
||||
if (PyArray_Check(obj))
|
||||
@@ -1174,92 +1139,63 @@ template<typename _Tp> struct pyopencvVecConverter
|
||||
Mat m;
|
||||
pyopencv_to(obj, m, info);
|
||||
m.copyTo(value);
|
||||
return true;
|
||||
}
|
||||
if (!PySequence_Check(obj))
|
||||
return false;
|
||||
PyObject *seq = PySequence_Fast(obj, info.name);
|
||||
if (seq == NULL)
|
||||
return false;
|
||||
int i, j, n = (int)PySequence_Fast_GET_SIZE(seq);
|
||||
value.resize(n);
|
||||
|
||||
int type = traits::Type<_Tp>::value;
|
||||
int depth = CV_MAT_DEPTH(type), channels = CV_MAT_CN(type);
|
||||
PyObject** items = PySequence_Fast_ITEMS(seq);
|
||||
|
||||
for( i = 0; i < n; i++ )
|
||||
else if (PySequence_Check(obj))
|
||||
{
|
||||
PyObject* item = items[i];
|
||||
PyObject* seq_i = 0;
|
||||
PyObject** items_i = &item;
|
||||
_Cp* data = (_Cp*)&value[i];
|
||||
|
||||
if( channels == 2 && PyComplex_CheckExact(item) )
|
||||
const int type = traits::Type<_Tp>::value;
|
||||
const int depth = CV_MAT_DEPTH(type), channels = CV_MAT_CN(type);
|
||||
size_t i, n = PySequence_Size(obj);
|
||||
value.resize(n);
|
||||
for (i = 0; i < n; i++ )
|
||||
{
|
||||
Py_complex c = PyComplex_AsCComplex(obj);
|
||||
data[0] = saturate_cast<_Cp>(c.real);
|
||||
data[1] = saturate_cast<_Cp>(c.imag);
|
||||
continue;
|
||||
}
|
||||
if( channels > 1 )
|
||||
{
|
||||
if( PyArray_Check(item))
|
||||
{
|
||||
Mat src;
|
||||
pyopencv_to(item, src, info);
|
||||
if( src.dims != 2 || src.channels() != 1 ||
|
||||
((src.cols != 1 || src.rows != channels) &&
|
||||
(src.cols != channels || src.rows != 1)))
|
||||
break;
|
||||
Mat dst(src.rows, src.cols, depth, data);
|
||||
src.convertTo(dst, type);
|
||||
if( dst.data != (uchar*)data )
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
SafeSeqItem item_wrap(obj, i);
|
||||
PyObject* item = item_wrap.item;
|
||||
_Cp* data = (_Cp*)&value[i];
|
||||
|
||||
seq_i = PySequence_Fast(item, info.name);
|
||||
if( !seq_i || (int)PySequence_Fast_GET_SIZE(seq_i) != channels )
|
||||
if( channels == 2 && PyComplex_Check(item) )
|
||||
{
|
||||
Py_XDECREF(seq_i);
|
||||
break;
|
||||
data[0] = saturate_cast<_Cp>(PyComplex_RealAsDouble(item));
|
||||
data[1] = saturate_cast<_Cp>(PyComplex_ImagAsDouble(item));
|
||||
}
|
||||
items_i = PySequence_Fast_ITEMS(seq_i);
|
||||
}
|
||||
|
||||
for( j = 0; j < channels; j++ )
|
||||
{
|
||||
PyObject* item_ij = items_i[j];
|
||||
if( PyInt_Check(item_ij))
|
||||
else if( channels > 1 )
|
||||
{
|
||||
int v = (int)PyInt_AsLong(item_ij);
|
||||
if( v == -1 && PyErr_Occurred() )
|
||||
if( PyArray_Check(item))
|
||||
{
|
||||
Mat src;
|
||||
pyopencv_to(item, src, info);
|
||||
if( src.dims != 2 || src.channels() != 1 ||
|
||||
((src.cols != 1 || src.rows != channels) &&
|
||||
(src.cols != channels || src.rows != 1)))
|
||||
break;
|
||||
Mat dst(src.rows, src.cols, depth, data);
|
||||
src.convertTo(dst, type);
|
||||
if( dst.data != (uchar*)data )
|
||||
break;
|
||||
}
|
||||
else if (PySequence_Check(item))
|
||||
{
|
||||
if (!copyOneItem(item, 0, channels, data))
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
data[j] = saturate_cast<_Cp>(v);
|
||||
}
|
||||
}
|
||||
else if( PyLong_Check(item_ij))
|
||||
else if (channels == 1)
|
||||
{
|
||||
int v = (int)PyLong_AsLong(item_ij);
|
||||
if( v == -1 && PyErr_Occurred() )
|
||||
if (!copyOneItem(obj, i, channels, data))
|
||||
break;
|
||||
data[j] = saturate_cast<_Cp>(v);
|
||||
}
|
||||
else if( PyFloat_Check(item_ij))
|
||||
{
|
||||
double v = PyFloat_AsDouble(item_ij);
|
||||
if( PyErr_Occurred() )
|
||||
break;
|
||||
data[j] = saturate_cast<_Cp>(v);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
Py_XDECREF(seq_i);
|
||||
if( j < channels )
|
||||
break;
|
||||
return i == n;
|
||||
}
|
||||
Py_DECREF(seq);
|
||||
return i == n;
|
||||
return false;
|
||||
}
|
||||
|
||||
static PyObject* from(const std::vector<_Tp>& value)
|
||||
@@ -1291,22 +1227,15 @@ template<typename _Tp> static inline bool pyopencv_to_generic_vec(PyObject* obj,
|
||||
return true;
|
||||
if (!PySequence_Check(obj))
|
||||
return false;
|
||||
PyObject *seq = PySequence_Fast(obj, info.name);
|
||||
if (seq == NULL)
|
||||
return false;
|
||||
int i, n = (int)PySequence_Fast_GET_SIZE(seq);
|
||||
size_t n = PySequence_Size(obj);
|
||||
value.resize(n);
|
||||
|
||||
PyObject** items = PySequence_Fast_ITEMS(seq);
|
||||
|
||||
for( i = 0; i < n; i++ )
|
||||
for(size_t i = 0; i < n; i++ )
|
||||
{
|
||||
PyObject* item = items[i];
|
||||
if(!pyopencv_to(item, value[i], info))
|
||||
break;
|
||||
SafeSeqItem item_wrap(obj, i);
|
||||
if(!pyopencv_to(item_wrap.item, value[i], info))
|
||||
return false;
|
||||
}
|
||||
Py_DECREF(seq);
|
||||
return i == n;
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename _Tp> static inline PyObject* pyopencv_from_generic_vec(const std::vector<_Tp>& value)
|
||||
@@ -1318,7 +1247,7 @@ template<typename _Tp> static inline PyObject* pyopencv_from_generic_vec(const s
|
||||
PyObject* item = pyopencv_from(value[i]);
|
||||
if(!item)
|
||||
break;
|
||||
PyList_SET_ITEM(seq, i, item);
|
||||
PyList_SetItem(seq, i, item);
|
||||
}
|
||||
if( i < n )
|
||||
{
|
||||
@@ -1697,31 +1626,37 @@ static PyObject *pycvCreateButton(PyObject*, PyObject *args, PyObject *kw)
|
||||
|
||||
static int convert_to_char(PyObject *o, char *dst, const char *name = "no_name")
|
||||
{
|
||||
if (PyString_Check(o) && PyString_Size(o) == 1) {
|
||||
*dst = PyString_AsString(o)[0];
|
||||
return 1;
|
||||
} else {
|
||||
std::string str;
|
||||
if (getUnicodeString(o, str))
|
||||
{
|
||||
*dst = str[0];
|
||||
return 1;
|
||||
}
|
||||
(*dst) = 0;
|
||||
return failmsg("Expected single character string for argument '%s'", name);
|
||||
}
|
||||
}
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
#define MKTYPE2(NAME) pyopencv_##NAME##_specials(); if (!to_ok(&pyopencv_##NAME##_Type)) return NULL;
|
||||
#else
|
||||
#define MKTYPE2(NAME) pyopencv_##NAME##_specials(); if (!to_ok(&pyopencv_##NAME##_Type)) return
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
# pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
||||
#endif
|
||||
|
||||
|
||||
#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)
|
||||
#else
|
||||
#define CVPY_TYPE(NAME, STORAGE, SNAME, _1, _2) CVPY_TYPE_DECLARE(NAME, STORAGE, SNAME)
|
||||
#endif
|
||||
#include "pyopencv_generated_types.h"
|
||||
#undef CVPY_TYPE
|
||||
|
||||
#include "pyopencv_generated_types_content.h"
|
||||
#include "pyopencv_generated_funcs.h"
|
||||
|
||||
|
||||
static PyMethodDef special_methods[] = {
|
||||
{"redirectError", CV_PY_FN_WITH_KW(pycvRedirectError), "redirectError(onError) -> None"},
|
||||
#ifdef HAVE_OPENCV_HIGHGUI
|
||||
@@ -1786,23 +1721,90 @@ static void init_submodule(PyObject * root, const char * name, PyMethodDef * met
|
||||
|
||||
}
|
||||
|
||||
#include "pyopencv_generated_ns_reg.h"
|
||||
#include "pyopencv_generated_modules_content.h"
|
||||
|
||||
static int to_ok(PyTypeObject *to)
|
||||
static bool init_body(PyObject * m)
|
||||
{
|
||||
to->tp_alloc = PyType_GenericAlloc;
|
||||
to->tp_new = PyType_GenericNew;
|
||||
to->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
|
||||
return (PyType_Ready(to) == 0);
|
||||
}
|
||||
#define CVPY_MODULE(NAMESTR, NAME) \
|
||||
init_submodule(m, MODULESTR NAMESTR, methods_##NAME, consts_##NAME)
|
||||
#include "pyopencv_generated_modules.h"
|
||||
#undef CVPY_MODULE
|
||||
|
||||
#ifdef CVPY_DYNAMIC_INIT
|
||||
#define CVPY_TYPE(NAME, _1, _2, BASE, CONSTRUCTOR) CVPY_TYPE_INIT_DYNAMIC(NAME, return false, BASE, CONSTRUCTOR)
|
||||
PyObject * pyopencv_NoBase_TypePtr = NULL;
|
||||
#else
|
||||
#define CVPY_TYPE(NAME, _1, _2, BASE, CONSTRUCTOR) CVPY_TYPE_INIT_STATIC(NAME, return false, BASE, CONSTRUCTOR)
|
||||
PyTypeObject * pyopencv_NoBase_TypePtr = NULL;
|
||||
#endif
|
||||
#include "pyopencv_generated_types.h"
|
||||
#undef CVPY_TYPE
|
||||
|
||||
PyObject* d = PyModule_GetDict(m);
|
||||
|
||||
|
||||
PyDict_SetItemString(d, "__version__", PyString_FromString(CV_VERSION));
|
||||
|
||||
PyObject *opencv_error_dict = PyDict_New();
|
||||
PyDict_SetItemString(opencv_error_dict, "file", Py_None);
|
||||
PyDict_SetItemString(opencv_error_dict, "func", Py_None);
|
||||
PyDict_SetItemString(opencv_error_dict, "line", Py_None);
|
||||
PyDict_SetItemString(opencv_error_dict, "code", Py_None);
|
||||
PyDict_SetItemString(opencv_error_dict, "msg", Py_None);
|
||||
PyDict_SetItemString(opencv_error_dict, "err", Py_None);
|
||||
opencv_error = PyErr_NewException((char*)MODULESTR".error", NULL, opencv_error_dict);
|
||||
Py_DECREF(opencv_error_dict);
|
||||
PyDict_SetItemString(d, "error", opencv_error);
|
||||
|
||||
|
||||
#define PUBLISH(I) PyDict_SetItemString(d, #I, PyInt_FromLong(I))
|
||||
PUBLISH(CV_8U);
|
||||
PUBLISH(CV_8UC1);
|
||||
PUBLISH(CV_8UC2);
|
||||
PUBLISH(CV_8UC3);
|
||||
PUBLISH(CV_8UC4);
|
||||
PUBLISH(CV_8S);
|
||||
PUBLISH(CV_8SC1);
|
||||
PUBLISH(CV_8SC2);
|
||||
PUBLISH(CV_8SC3);
|
||||
PUBLISH(CV_8SC4);
|
||||
PUBLISH(CV_16U);
|
||||
PUBLISH(CV_16UC1);
|
||||
PUBLISH(CV_16UC2);
|
||||
PUBLISH(CV_16UC3);
|
||||
PUBLISH(CV_16UC4);
|
||||
PUBLISH(CV_16S);
|
||||
PUBLISH(CV_16SC1);
|
||||
PUBLISH(CV_16SC2);
|
||||
PUBLISH(CV_16SC3);
|
||||
PUBLISH(CV_16SC4);
|
||||
PUBLISH(CV_32S);
|
||||
PUBLISH(CV_32SC1);
|
||||
PUBLISH(CV_32SC2);
|
||||
PUBLISH(CV_32SC3);
|
||||
PUBLISH(CV_32SC4);
|
||||
PUBLISH(CV_32F);
|
||||
PUBLISH(CV_32FC1);
|
||||
PUBLISH(CV_32FC2);
|
||||
PUBLISH(CV_32FC3);
|
||||
PUBLISH(CV_32FC4);
|
||||
PUBLISH(CV_64F);
|
||||
PUBLISH(CV_64FC1);
|
||||
PUBLISH(CV_64FC2);
|
||||
PUBLISH(CV_64FC3);
|
||||
PUBLISH(CV_64FC4);
|
||||
#undef PUBLISH
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#pragma GCC visibility push(default)
|
||||
#endif
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
PyMODINIT_FUNC PyInit_cv2();
|
||||
// === Python 3
|
||||
|
||||
static struct PyModuleDef cv2_moduledef =
|
||||
{
|
||||
PyModuleDef_HEAD_INIT,
|
||||
@@ -1813,92 +1815,24 @@ static struct PyModuleDef cv2_moduledef =
|
||||
special_methods
|
||||
};
|
||||
|
||||
PyMODINIT_FUNC PyInit_cv2();
|
||||
PyObject* PyInit_cv2()
|
||||
#else
|
||||
PyMODINIT_FUNC initcv2();
|
||||
|
||||
void initcv2()
|
||||
#endif
|
||||
{
|
||||
import_array();
|
||||
|
||||
#include "pyopencv_generated_type_reg.h"
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
PyObject* m = PyModule_Create(&cv2_moduledef);
|
||||
#else
|
||||
PyObject* m = Py_InitModule(MODULESTR, special_methods);
|
||||
#endif
|
||||
init_submodules(m); // from "pyopencv_generated_ns_reg.h"
|
||||
|
||||
PyObject* d = PyModule_GetDict(m);
|
||||
|
||||
PyDict_SetItemString(d, "__version__", PyString_FromString(CV_VERSION));
|
||||
|
||||
PyObject *opencv_error_dict = PyDict_New();
|
||||
PyDict_SetItemString(opencv_error_dict, "file", Py_None);
|
||||
PyDict_SetItemString(opencv_error_dict, "func", Py_None);
|
||||
PyDict_SetItemString(opencv_error_dict, "line", Py_None);
|
||||
PyDict_SetItemString(opencv_error_dict, "code", Py_None);
|
||||
PyDict_SetItemString(opencv_error_dict, "msg", Py_None);
|
||||
PyDict_SetItemString(opencv_error_dict, "err", Py_None);
|
||||
opencv_error = PyErr_NewException((char*)MODULESTR".error", NULL, opencv_error_dict);
|
||||
Py_DECREF(opencv_error_dict);
|
||||
PyDict_SetItemString(d, "error", opencv_error);
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
#define PUBLISH_OBJECT(name, type) Py_INCREF(&type);\
|
||||
PyModule_AddObject(m, name, (PyObject *)&type);
|
||||
#else
|
||||
// Unrolled Py_INCREF(&type) without (PyObject*) cast
|
||||
// due to "warning: dereferencing type-punned pointer will break strict-aliasing rules"
|
||||
#define PUBLISH_OBJECT(name, type) _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA (&type)->ob_refcnt++;\
|
||||
PyModule_AddObject(m, name, (PyObject *)&type);
|
||||
#endif
|
||||
|
||||
#include "pyopencv_generated_type_publish.h"
|
||||
|
||||
#define PUBLISH(I) PyDict_SetItemString(d, #I, PyInt_FromLong(I))
|
||||
//#define PUBLISHU(I) PyDict_SetItemString(d, #I, PyLong_FromUnsignedLong(I))
|
||||
#define PUBLISH2(I, value) PyDict_SetItemString(d, #I, PyLong_FromLong(value))
|
||||
|
||||
PUBLISH(CV_8U);
|
||||
PUBLISH(CV_8UC1);
|
||||
PUBLISH(CV_8UC2);
|
||||
PUBLISH(CV_8UC3);
|
||||
PUBLISH(CV_8UC4);
|
||||
PUBLISH(CV_8S);
|
||||
PUBLISH(CV_8SC1);
|
||||
PUBLISH(CV_8SC2);
|
||||
PUBLISH(CV_8SC3);
|
||||
PUBLISH(CV_8SC4);
|
||||
PUBLISH(CV_16U);
|
||||
PUBLISH(CV_16UC1);
|
||||
PUBLISH(CV_16UC2);
|
||||
PUBLISH(CV_16UC3);
|
||||
PUBLISH(CV_16UC4);
|
||||
PUBLISH(CV_16S);
|
||||
PUBLISH(CV_16SC1);
|
||||
PUBLISH(CV_16SC2);
|
||||
PUBLISH(CV_16SC3);
|
||||
PUBLISH(CV_16SC4);
|
||||
PUBLISH(CV_32S);
|
||||
PUBLISH(CV_32SC1);
|
||||
PUBLISH(CV_32SC2);
|
||||
PUBLISH(CV_32SC3);
|
||||
PUBLISH(CV_32SC4);
|
||||
PUBLISH(CV_32F);
|
||||
PUBLISH(CV_32FC1);
|
||||
PUBLISH(CV_32FC2);
|
||||
PUBLISH(CV_32FC3);
|
||||
PUBLISH(CV_32FC4);
|
||||
PUBLISH(CV_64F);
|
||||
PUBLISH(CV_64FC1);
|
||||
PUBLISH(CV_64FC2);
|
||||
PUBLISH(CV_64FC3);
|
||||
PUBLISH(CV_64FC4);
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
import_array(); // from numpy
|
||||
PyObject* m = PyModule_Create(&cv2_moduledef);
|
||||
if (!init_body(m))
|
||||
return NULL;
|
||||
return m;
|
||||
#endif
|
||||
}
|
||||
|
||||
#else
|
||||
// === Python 2
|
||||
PyMODINIT_FUNC initcv2();
|
||||
void initcv2()
|
||||
{
|
||||
import_array(); // from numpy
|
||||
PyObject* m = Py_InitModule(MODULESTR, special_methods);
|
||||
init_body(m);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user