Merge pull request #21182 from mshabunin:split-cv2cpp
Split cv2.cpp * split cv2.cpp: util, numpy * split cv2.cpp: convert * split cv2.cpp: highgui, more utils * split cv2.cpp: fix numpy import
This commit is contained in:
parent
676a724491
commit
973e1acb67
@ -19,7 +19,17 @@ if(NOT WIN32 AND NOT APPLE AND NOT OPENCV_PYTHON_SKIP_LINKER_EXCLUDE_LIBS)
|
||||
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--exclude-libs=ALL")
|
||||
endif()
|
||||
|
||||
ocv_add_library(${the_module} MODULE ${PYTHON_SOURCE_DIR}/src2/cv2.cpp ${cv2_generated_hdrs} ${opencv_userdef_hdrs} ${cv2_custom_hdr})
|
||||
ocv_add_library(${the_module} MODULE
|
||||
${PYTHON_SOURCE_DIR}/src2/cv2.cpp
|
||||
${PYTHON_SOURCE_DIR}/src2/cv2_util.cpp
|
||||
${PYTHON_SOURCE_DIR}/src2/cv2_numpy.cpp
|
||||
${PYTHON_SOURCE_DIR}/src2/cv2_convert.cpp
|
||||
${PYTHON_SOURCE_DIR}/src2/cv2_highgui.cpp
|
||||
${cv2_generated_hdrs}
|
||||
${opencv_userdef_hdrs}
|
||||
${cv2_custom_hdr}
|
||||
)
|
||||
|
||||
if(TARGET gen_opencv_python_source)
|
||||
add_dependencies(${the_module} gen_opencv_python_source)
|
||||
endif()
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
55
modules/python/src2/cv2.hpp
Normal file
55
modules/python/src2/cv2.hpp
Normal file
@ -0,0 +1,55 @@
|
||||
#ifndef CV2_HPP
|
||||
#define CV2_HPP
|
||||
|
||||
//warning number '5033' not a valid compiler warning in vc12
|
||||
#if defined(_MSC_VER) && (_MSC_VER > 1800)
|
||||
// eliminating duplicated round() declaration
|
||||
#define HAVE_ROUND 1
|
||||
#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 <cmath>
|
||||
#include <Python.h>
|
||||
#include <limits>
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#undef CVPY_DYNAMIC_INIT
|
||||
#else
|
||||
#define CV_PYTHON_3 1
|
||||
#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 "pycompat.hpp"
|
||||
|
||||
class ArgInfo
|
||||
{
|
||||
public:
|
||||
const char* name;
|
||||
bool outputarg;
|
||||
// more fields may be added if necessary
|
||||
|
||||
ArgInfo(const char* name_, bool outputarg_) : name(name_), outputarg(outputarg_) {}
|
||||
|
||||
private:
|
||||
ArgInfo(const ArgInfo&) = delete;
|
||||
ArgInfo& operator=(const ArgInfo&) = delete;
|
||||
};
|
||||
|
||||
|
||||
#endif // CV2_HPP
|
||||
1052
modules/python/src2/cv2_convert.cpp
Normal file
1052
modules/python/src2/cv2_convert.cpp
Normal file
File diff suppressed because it is too large
Load Diff
529
modules/python/src2/cv2_convert.hpp
Normal file
529
modules/python/src2/cv2_convert.hpp
Normal file
@ -0,0 +1,529 @@
|
||||
#ifndef CV2_CONVERT_HPP
|
||||
#define CV2_CONVERT_HPP
|
||||
|
||||
#include "cv2.hpp"
|
||||
#include "cv2_util.hpp"
|
||||
#include "cv2_numpy.hpp"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <type_traits> // std::enable_if
|
||||
|
||||
extern PyTypeObject* pyopencv_Mat_TypePtr;
|
||||
|
||||
#define CV_HAS_CONVERSION_ERROR(x) (((x) == -1) && PyErr_Occurred())
|
||||
|
||||
inline bool isBool(PyObject* obj) CV_NOEXCEPT
|
||||
{
|
||||
return PyArray_IsScalar(obj, Bool) || PyBool_Check(obj);
|
||||
}
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
|
||||
// exception-safe pyopencv_to
|
||||
template<typename _Tp> static
|
||||
bool pyopencv_to_safe(PyObject* obj, _Tp& value, const ArgInfo& info)
|
||||
{
|
||||
try
|
||||
{
|
||||
return pyopencv_to(obj, value, info);
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
PyErr_SetString(opencv_error, cv::format("Conversion error: %s, what: %s", info.name, e.what()).c_str());
|
||||
return false;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
PyErr_SetString(opencv_error, cv::format("Conversion error: %s", info.name).c_str());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
template<typename T, class TEnable = void> // TEnable is used for SFINAE checks
|
||||
struct PyOpenCV_Converter
|
||||
{
|
||||
//static inline bool to(PyObject* obj, T& p, const ArgInfo& info);
|
||||
//static inline PyObject* from(const T& src);
|
||||
};
|
||||
|
||||
// --- Generic
|
||||
|
||||
template<typename T>
|
||||
bool pyopencv_to(PyObject* obj, T& p, const ArgInfo& info) { return PyOpenCV_Converter<T>::to(obj, p, info); }
|
||||
|
||||
template<typename T>
|
||||
PyObject* pyopencv_from(const T& src) { return PyOpenCV_Converter<T>::from(src); }
|
||||
|
||||
// --- Matx
|
||||
|
||||
template<typename _Tp, int m, int n>
|
||||
bool pyopencv_to(PyObject* o, cv::Matx<_Tp, m, n>& mx, const ArgInfo& info)
|
||||
{
|
||||
cv::Mat tmp;
|
||||
if (!pyopencv_to(o, tmp, info)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
tmp.copyTo(mx);
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename _Tp, int m, int n>
|
||||
PyObject* pyopencv_from(const cv::Matx<_Tp, m, n>& matx)
|
||||
{
|
||||
return pyopencv_from(cv::Mat(matx));
|
||||
}
|
||||
|
||||
// --- bool
|
||||
template<> bool pyopencv_to(PyObject* obj, bool& value, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const bool& value);
|
||||
|
||||
// --- Mat
|
||||
template<> bool pyopencv_to(PyObject* o, cv::Mat& m, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::Mat& m);
|
||||
|
||||
// --- Ptr
|
||||
template<typename T>
|
||||
struct PyOpenCV_Converter< cv::Ptr<T> >
|
||||
{
|
||||
static PyObject* from(const cv::Ptr<T>& p)
|
||||
{
|
||||
if (!p)
|
||||
Py_RETURN_NONE;
|
||||
return pyopencv_from(*p);
|
||||
}
|
||||
static bool to(PyObject *o, cv::Ptr<T>& p, const ArgInfo& info)
|
||||
{
|
||||
if (!o || o == Py_None)
|
||||
return true;
|
||||
p = cv::makePtr<T>();
|
||||
return pyopencv_to(o, *p, info);
|
||||
}
|
||||
};
|
||||
|
||||
// --- ptr
|
||||
template<> bool pyopencv_to(PyObject* obj, void*& ptr, const ArgInfo& info);
|
||||
PyObject* pyopencv_from(void*& ptr);
|
||||
|
||||
// --- Scalar
|
||||
template<> bool pyopencv_to(PyObject *o, cv::Scalar& s, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::Scalar& src);
|
||||
|
||||
// --- size_t
|
||||
template<> bool pyopencv_to(PyObject* obj, size_t& value, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const size_t& value);
|
||||
|
||||
// --- int
|
||||
template<> bool pyopencv_to(PyObject* obj, int& value, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const int& value);
|
||||
|
||||
// --- int64
|
||||
template<> PyObject* pyopencv_from(const int64& value);
|
||||
|
||||
// There is conflict between "size_t" and "unsigned int".
|
||||
// They are the same type on some 32-bit platforms.
|
||||
template<typename T>
|
||||
struct PyOpenCV_Converter
|
||||
< T, typename std::enable_if< std::is_same<unsigned int, T>::value && !std::is_same<unsigned int, size_t>::value >::type >
|
||||
{
|
||||
static inline PyObject* from(const unsigned int& value)
|
||||
{
|
||||
return PyLong_FromUnsignedLong(value);
|
||||
}
|
||||
|
||||
static inline bool to(PyObject* obj, unsigned int& value, const ArgInfo& info)
|
||||
{
|
||||
CV_UNUSED(info);
|
||||
if(!obj || obj == Py_None)
|
||||
return true;
|
||||
if(PyInt_Check(obj))
|
||||
value = (unsigned int)PyInt_AsLong(obj);
|
||||
else if(PyLong_Check(obj))
|
||||
value = (unsigned int)PyLong_AsLong(obj);
|
||||
else
|
||||
return false;
|
||||
return value != (unsigned int)-1 || !PyErr_Occurred();
|
||||
}
|
||||
};
|
||||
|
||||
// --- uchar
|
||||
template<> bool pyopencv_to(PyObject* obj, uchar& value, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const uchar& value);
|
||||
|
||||
// --- char
|
||||
template<> bool pyopencv_to(PyObject* obj, char& value, const ArgInfo& info);
|
||||
|
||||
// --- double
|
||||
template<> bool pyopencv_to(PyObject* obj, double& value, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const double& value);
|
||||
|
||||
// --- float
|
||||
template<> bool pyopencv_to(PyObject* obj, float& value, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const float& value);
|
||||
|
||||
// --- string
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::String &value, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::String& value);
|
||||
#if CV_VERSION_MAJOR == 3
|
||||
template<> PyObject* pyopencv_from(const std::string& value);
|
||||
#endif
|
||||
|
||||
// --- Size
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::Size& sz, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::Size& sz);
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::Size_<float>& sz, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::Size_<float>& sz);
|
||||
|
||||
// --- Rect
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::Rect& r, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::Rect& r);
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::Rect2d& r, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::Rect2d& r);
|
||||
|
||||
// --- RotatedRect
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::RotatedRect& dst, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::RotatedRect& src);
|
||||
|
||||
// --- Range
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::Range& r, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::Range& r);
|
||||
|
||||
// --- Point
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::Point& p, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::Point& p);
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::Point2f& p, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::Point2f& p);
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::Point2d& p, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::Point2d& p);
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::Point3f& p, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::Point3f& p);
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::Point3d& p, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::Point3d& p);
|
||||
|
||||
// --- Vec
|
||||
template<typename _Tp, int cn>
|
||||
bool pyopencv_to(PyObject* o, cv::Vec<_Tp, cn>& vec, const ArgInfo& info)
|
||||
{
|
||||
return pyopencv_to(o, (cv::Matx<_Tp, cn, 1>&)vec, info);
|
||||
}
|
||||
bool pyopencv_to(PyObject* obj, cv::Vec4d& v, ArgInfo& info);
|
||||
PyObject* pyopencv_from(const cv::Vec4d& v);
|
||||
bool pyopencv_to(PyObject* obj, cv::Vec4f& v, ArgInfo& info);
|
||||
PyObject* pyopencv_from(const cv::Vec4f& v);
|
||||
bool pyopencv_to(PyObject* obj, cv::Vec4i& v, ArgInfo& info);
|
||||
PyObject* pyopencv_from(const cv::Vec4i& v);
|
||||
bool pyopencv_to(PyObject* obj, cv::Vec3d& v, ArgInfo& info);
|
||||
PyObject* pyopencv_from(const cv::Vec3d& v);
|
||||
bool pyopencv_to(PyObject* obj, cv::Vec3f& v, ArgInfo& info);
|
||||
PyObject* pyopencv_from(const cv::Vec3f& v);
|
||||
bool pyopencv_to(PyObject* obj, cv::Vec3i& v, ArgInfo& info);
|
||||
PyObject* pyopencv_from(const cv::Vec3i& v);
|
||||
bool pyopencv_to(PyObject* obj, cv::Vec2d& v, ArgInfo& info);
|
||||
PyObject* pyopencv_from(const cv::Vec2d& v);
|
||||
bool pyopencv_to(PyObject* obj, cv::Vec2f& v, ArgInfo& info);
|
||||
PyObject* pyopencv_from(const cv::Vec2f& v);
|
||||
bool pyopencv_to(PyObject* obj, cv::Vec2i& v, ArgInfo& info);
|
||||
PyObject* pyopencv_from(const cv::Vec2i& v);
|
||||
|
||||
// --- TermCriteria
|
||||
template<> bool pyopencv_to(PyObject* obj, cv::TermCriteria& dst, const ArgInfo& info);
|
||||
template<> PyObject* pyopencv_from(const cv::TermCriteria& src);
|
||||
|
||||
// --- Moments
|
||||
template<> PyObject* pyopencv_from(const cv::Moments& m);
|
||||
|
||||
// --- pair
|
||||
template<> PyObject* pyopencv_from(const std::pair<int, double>& src);
|
||||
|
||||
// --- vector
|
||||
template <typename Tp>
|
||||
struct pyopencvVecConverter;
|
||||
|
||||
template <typename Tp>
|
||||
bool pyopencv_to(PyObject* obj, std::vector<Tp>& value, const ArgInfo& info)
|
||||
{
|
||||
if (!obj || obj == Py_None)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return pyopencvVecConverter<Tp>::to(obj, value, info);
|
||||
}
|
||||
|
||||
template <typename Tp>
|
||||
PyObject* pyopencv_from(const std::vector<Tp>& value)
|
||||
{
|
||||
return pyopencvVecConverter<Tp>::from(value);
|
||||
}
|
||||
|
||||
template <typename Tp>
|
||||
static bool pyopencv_to_generic_vec(PyObject* obj, std::vector<Tp>& value, const ArgInfo& info)
|
||||
{
|
||||
if (!obj || obj == Py_None)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!PySequence_Check(obj))
|
||||
{
|
||||
failmsg("Can't parse '%s'. Input argument doesn't provide sequence protocol", info.name);
|
||||
return false;
|
||||
}
|
||||
const size_t n = static_cast<size_t>(PySequence_Size(obj));
|
||||
value.resize(n);
|
||||
for (size_t i = 0; i < n; i++)
|
||||
{
|
||||
SafeSeqItem item_wrap(obj, i);
|
||||
if (!pyopencv_to(item_wrap.item, value[i], info))
|
||||
{
|
||||
failmsg("Can't parse '%s'. Sequence item with index %lu has a wrong type", info.name, i);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template<> inline bool pyopencv_to_generic_vec(PyObject* obj, std::vector<bool>& value, const ArgInfo& info)
|
||||
{
|
||||
if (!obj || obj == Py_None)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!PySequence_Check(obj))
|
||||
{
|
||||
failmsg("Can't parse '%s'. Input argument doesn't provide sequence protocol", info.name);
|
||||
return false;
|
||||
}
|
||||
const size_t n = static_cast<size_t>(PySequence_Size(obj));
|
||||
value.resize(n);
|
||||
for (size_t i = 0; i < n; i++)
|
||||
{
|
||||
SafeSeqItem item_wrap(obj, i);
|
||||
bool elem{};
|
||||
if (!pyopencv_to(item_wrap.item, elem, info))
|
||||
{
|
||||
failmsg("Can't parse '%s'. Sequence item with index %lu has a wrong type", info.name, i);
|
||||
return false;
|
||||
}
|
||||
value[i] = elem;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Tp>
|
||||
static PyObject* pyopencv_from_generic_vec(const std::vector<Tp>& value)
|
||||
{
|
||||
Py_ssize_t n = static_cast<Py_ssize_t>(value.size());
|
||||
PySafeObject seq(PyTuple_New(n));
|
||||
for (Py_ssize_t i = 0; i < n; i++)
|
||||
{
|
||||
PyObject* item = pyopencv_from(value[i]);
|
||||
// If item can't be assigned - PyTuple_SetItem raises exception and returns -1.
|
||||
if (!item || PyTuple_SetItem(seq, i, item) == -1)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return seq.release();
|
||||
}
|
||||
|
||||
template<> inline PyObject* pyopencv_from_generic_vec(const std::vector<bool>& value)
|
||||
{
|
||||
Py_ssize_t n = static_cast<Py_ssize_t>(value.size());
|
||||
PySafeObject seq(PyTuple_New(n));
|
||||
for (Py_ssize_t i = 0; i < n; i++)
|
||||
{
|
||||
bool elem = value[i];
|
||||
PyObject* item = pyopencv_from(elem);
|
||||
// If item can't be assigned - PyTuple_SetItem raises exception and returns -1.
|
||||
if (!item || PyTuple_SetItem(seq, i, item) == -1)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return seq.release();
|
||||
}
|
||||
|
||||
namespace traits {
|
||||
|
||||
template <bool Value>
|
||||
struct BooleanConstant
|
||||
{
|
||||
static const bool value = Value;
|
||||
typedef BooleanConstant<Value> type;
|
||||
};
|
||||
|
||||
typedef BooleanConstant<true> TrueType;
|
||||
typedef BooleanConstant<false> FalseType;
|
||||
|
||||
template <class T>
|
||||
struct VoidType {
|
||||
typedef void type;
|
||||
};
|
||||
|
||||
template <class T, class DType = void>
|
||||
struct IsRepresentableAsMatDataType : FalseType
|
||||
{
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct IsRepresentableAsMatDataType<T, typename VoidType<typename cv::DataType<T>::channel_type>::type> : TrueType
|
||||
{
|
||||
};
|
||||
|
||||
} // namespace traits
|
||||
|
||||
template <typename Tp>
|
||||
struct pyopencvVecConverter
|
||||
{
|
||||
typedef typename std::vector<Tp>::iterator VecIt;
|
||||
|
||||
static bool to(PyObject* obj, std::vector<Tp>& value, const ArgInfo& info)
|
||||
{
|
||||
if (!PyArray_Check(obj))
|
||||
{
|
||||
return pyopencv_to_generic_vec(obj, value, info);
|
||||
}
|
||||
// If user passed an array it is possible to make faster conversions in several cases
|
||||
PyArrayObject* array_obj = reinterpret_cast<PyArrayObject*>(obj);
|
||||
const NPY_TYPES target_type = asNumpyType<Tp>();
|
||||
const NPY_TYPES source_type = static_cast<NPY_TYPES>(PyArray_TYPE(array_obj));
|
||||
if (target_type == NPY_OBJECT)
|
||||
{
|
||||
// Non-planar arrays representing objects (e.g. array of N Rect is an array of shape Nx4) have NPY_OBJECT
|
||||
// as their target type.
|
||||
return pyopencv_to_generic_vec(obj, value, info);
|
||||
}
|
||||
if (PyArray_NDIM(array_obj) > 1)
|
||||
{
|
||||
failmsg("Can't parse %dD array as '%s' vector argument", PyArray_NDIM(array_obj), info.name);
|
||||
return false;
|
||||
}
|
||||
if (target_type != source_type)
|
||||
{
|
||||
// Source type requires conversion
|
||||
// Allowed conversions for target type is handled in the corresponding pyopencv_to function
|
||||
return pyopencv_to_generic_vec(obj, value, info);
|
||||
}
|
||||
// For all other cases, all array data can be directly copied to std::vector data
|
||||
// Simple `memcpy` is not possible because NumPy array can reference a slice of the bigger array:
|
||||
// ```
|
||||
// arr = np.ones((8, 4, 5), dtype=np.int32)
|
||||
// convertible_to_vector_of_int = arr[:, 0, 1]
|
||||
// ```
|
||||
value.resize(static_cast<size_t>(PyArray_SIZE(array_obj)));
|
||||
const npy_intp item_step = PyArray_STRIDE(array_obj, 0) / PyArray_ITEMSIZE(array_obj);
|
||||
const Tp* data_ptr = static_cast<Tp*>(PyArray_DATA(array_obj));
|
||||
for (VecIt it = value.begin(); it != value.end(); ++it, data_ptr += item_step) {
|
||||
*it = *data_ptr;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static PyObject* from(const std::vector<Tp>& value)
|
||||
{
|
||||
if (value.empty())
|
||||
{
|
||||
return PyTuple_New(0);
|
||||
}
|
||||
return from(value, ::traits::IsRepresentableAsMatDataType<Tp>());
|
||||
}
|
||||
|
||||
private:
|
||||
static PyObject* from(const std::vector<Tp>& value, ::traits::FalseType)
|
||||
{
|
||||
// Underlying type is not representable as Mat Data Type
|
||||
return pyopencv_from_generic_vec(value);
|
||||
}
|
||||
|
||||
static PyObject* from(const std::vector<Tp>& value, ::traits::TrueType)
|
||||
{
|
||||
// Underlying type is representable as Mat Data Type, so faster return type is available
|
||||
typedef cv::DataType<Tp> DType;
|
||||
typedef typename DType::channel_type UnderlyingArrayType;
|
||||
|
||||
// If Mat is always exposed as NumPy array this code path can be reduced to the following snipped:
|
||||
// Mat src(value);
|
||||
// PyObject* array = pyopencv_from(src);
|
||||
// return PyArray_Squeeze(reinterpret_cast<PyArrayObject*>(array));
|
||||
// This puts unnecessary restrictions on Mat object those might be avoided without losing the performance.
|
||||
// Moreover, this version is a bit faster, because it doesn't create temporary objects with reference counting.
|
||||
|
||||
const NPY_TYPES target_type = asNumpyType<UnderlyingArrayType>();
|
||||
const int cols = DType::channels;
|
||||
PyObject* array = NULL;
|
||||
if (cols == 1)
|
||||
{
|
||||
npy_intp dims = static_cast<npy_intp>(value.size());
|
||||
array = PyArray_SimpleNew(1, &dims, target_type);
|
||||
}
|
||||
else
|
||||
{
|
||||
npy_intp dims[2] = {static_cast<npy_intp>(value.size()), cols};
|
||||
array = PyArray_SimpleNew(2, dims, target_type);
|
||||
}
|
||||
if(!array)
|
||||
{
|
||||
// NumPy arrays with shape (N, 1) and (N) are not equal, so correct error message should distinguish
|
||||
// them too.
|
||||
cv::String shape;
|
||||
if (cols > 1)
|
||||
{
|
||||
shape = cv::format("(%d x %d)", static_cast<int>(value.size()), cols);
|
||||
}
|
||||
else
|
||||
{
|
||||
shape = cv::format("(%d)", static_cast<int>(value.size()));
|
||||
}
|
||||
const cv::String error_message = cv::format("Can't allocate NumPy array for vector with dtype=%d and shape=%s",
|
||||
static_cast<int>(target_type), shape.c_str());
|
||||
emit_failmsg(PyExc_MemoryError, error_message.c_str());
|
||||
return array;
|
||||
}
|
||||
// Fill the array
|
||||
PyArrayObject* array_obj = reinterpret_cast<PyArrayObject*>(array);
|
||||
UnderlyingArrayType* array_data = static_cast<UnderlyingArrayType*>(PyArray_DATA(array_obj));
|
||||
// if Tp is representable as Mat DataType, so the following cast is pretty safe...
|
||||
const UnderlyingArrayType* value_data = reinterpret_cast<const UnderlyingArrayType*>(value.data());
|
||||
memcpy(array_data, value_data, sizeof(UnderlyingArrayType) * value.size() * static_cast<size_t>(cols));
|
||||
return array;
|
||||
}
|
||||
};
|
||||
|
||||
// --- tuple
|
||||
template<std::size_t I = 0, typename... Tp>
|
||||
inline typename std::enable_if<I == sizeof...(Tp), void>::type
|
||||
convert_to_python_tuple(const std::tuple<Tp...>&, PyObject*) { }
|
||||
|
||||
template<std::size_t I = 0, typename... Tp>
|
||||
inline typename std::enable_if<I < sizeof...(Tp), void>::type
|
||||
convert_to_python_tuple(const std::tuple<Tp...>& cpp_tuple, PyObject* py_tuple)
|
||||
{
|
||||
PyObject* item = pyopencv_from(std::get<I>(cpp_tuple));
|
||||
|
||||
if (!item)
|
||||
return;
|
||||
|
||||
PyTuple_SetItem(py_tuple, I, item);
|
||||
convert_to_python_tuple<I + 1, Tp...>(cpp_tuple, py_tuple);
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
PyObject* pyopencv_from(const std::tuple<Ts...>& cpp_tuple)
|
||||
{
|
||||
size_t size = sizeof...(Ts);
|
||||
PyObject* py_tuple = PyTuple_New(size);
|
||||
convert_to_python_tuple(cpp_tuple, py_tuple);
|
||||
size_t actual_size = PyTuple_Size(py_tuple);
|
||||
|
||||
if (actual_size < size)
|
||||
{
|
||||
Py_DECREF(py_tuple);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return py_tuple;
|
||||
}
|
||||
|
||||
#endif // CV2_CONVERT_HPP
|
||||
184
modules/python/src2/cv2_highgui.cpp
Normal file
184
modules/python/src2/cv2_highgui.cpp
Normal file
@ -0,0 +1,184 @@
|
||||
#include "cv2_highgui.hpp"
|
||||
|
||||
#ifdef HAVE_OPENCV_HIGHGUI
|
||||
|
||||
#include "cv2_util.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include <map>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
static void OnMouse(int event, int x, int y, int flags, void* param)
|
||||
{
|
||||
PyGILState_STATE gstate;
|
||||
gstate = PyGILState_Ensure();
|
||||
|
||||
PyObject *o = (PyObject*)param;
|
||||
PyObject *args = Py_BuildValue("iiiiO", event, x, y, flags, PyTuple_GetItem(o, 1));
|
||||
|
||||
PyObject *r = PyObject_Call(PyTuple_GetItem(o, 0), args, NULL);
|
||||
if (r == NULL)
|
||||
PyErr_Print();
|
||||
else
|
||||
Py_DECREF(r);
|
||||
Py_DECREF(args);
|
||||
PyGILState_Release(gstate);
|
||||
}
|
||||
|
||||
PyObject *pycvSetMouseCallback(PyObject*, PyObject *args, PyObject *kw)
|
||||
{
|
||||
const char *keywords[] = { "window_name", "on_mouse", "param", NULL };
|
||||
char* name;
|
||||
PyObject *on_mouse;
|
||||
PyObject *param = NULL;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|O", (char**)keywords, &name, &on_mouse, ¶m))
|
||||
return NULL;
|
||||
if (!PyCallable_Check(on_mouse)) {
|
||||
PyErr_SetString(PyExc_TypeError, "on_mouse must be callable");
|
||||
return NULL;
|
||||
}
|
||||
if (param == NULL) {
|
||||
param = Py_None;
|
||||
}
|
||||
PyObject* py_callback_info = Py_BuildValue("OO", on_mouse, param);
|
||||
static std::map<std::string, PyObject*> registered_callbacks;
|
||||
std::map<std::string, PyObject*>::iterator i = registered_callbacks.find(name);
|
||||
if (i != registered_callbacks.end())
|
||||
{
|
||||
Py_DECREF(i->second);
|
||||
i->second = py_callback_info;
|
||||
}
|
||||
else
|
||||
{
|
||||
registered_callbacks.insert(std::pair<std::string, PyObject*>(std::string(name), py_callback_info));
|
||||
}
|
||||
ERRWRAP2(setMouseCallback(name, OnMouse, py_callback_info));
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
static void OnChange(int pos, void *param)
|
||||
{
|
||||
PyGILState_STATE gstate;
|
||||
gstate = PyGILState_Ensure();
|
||||
|
||||
PyObject *o = (PyObject*)param;
|
||||
PyObject *args = Py_BuildValue("(i)", pos);
|
||||
PyObject *r = PyObject_Call(PyTuple_GetItem(o, 0), args, NULL);
|
||||
if (r == NULL)
|
||||
PyErr_Print();
|
||||
else
|
||||
Py_DECREF(r);
|
||||
Py_DECREF(args);
|
||||
PyGILState_Release(gstate);
|
||||
}
|
||||
|
||||
// workaround for #20408, use nullptr, set value later
|
||||
static int _createTrackbar(const String &trackbar_name, const String &window_name, int value, int count,
|
||||
TrackbarCallback onChange, PyObject* py_callback_info)
|
||||
{
|
||||
int n = createTrackbar(trackbar_name, window_name, NULL, count, onChange, py_callback_info);
|
||||
setTrackbarPos(trackbar_name, window_name, value);
|
||||
return n;
|
||||
}
|
||||
|
||||
PyObject *pycvCreateTrackbar(PyObject*, PyObject *args)
|
||||
{
|
||||
PyObject *on_change;
|
||||
char* trackbar_name;
|
||||
char* window_name;
|
||||
int value;
|
||||
int count;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "ssiiO", &trackbar_name, &window_name, &value, &count, &on_change))
|
||||
return NULL;
|
||||
if (!PyCallable_Check(on_change)) {
|
||||
PyErr_SetString(PyExc_TypeError, "on_change must be callable");
|
||||
return NULL;
|
||||
}
|
||||
PyObject* py_callback_info = Py_BuildValue("OO", on_change, Py_None);
|
||||
std::string name = std::string(window_name) + ":" + std::string(trackbar_name);
|
||||
static std::map<std::string, PyObject*> registered_callbacks;
|
||||
std::map<std::string, PyObject*>::iterator i = registered_callbacks.find(name);
|
||||
if (i != registered_callbacks.end())
|
||||
{
|
||||
Py_DECREF(i->second);
|
||||
i->second = py_callback_info;
|
||||
}
|
||||
else
|
||||
{
|
||||
registered_callbacks.insert(std::pair<std::string, PyObject*>(name, py_callback_info));
|
||||
}
|
||||
ERRWRAP2(_createTrackbar(trackbar_name, window_name, value, count, OnChange, py_callback_info));
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
static void OnButtonChange(int state, void *param)
|
||||
{
|
||||
PyGILState_STATE gstate;
|
||||
gstate = PyGILState_Ensure();
|
||||
|
||||
PyObject *o = (PyObject*)param;
|
||||
PyObject *args;
|
||||
if(PyTuple_GetItem(o, 1) != NULL)
|
||||
{
|
||||
args = Py_BuildValue("(iO)", state, PyTuple_GetItem(o,1));
|
||||
}
|
||||
else
|
||||
{
|
||||
args = Py_BuildValue("(i)", state);
|
||||
}
|
||||
|
||||
PyObject *r = PyObject_Call(PyTuple_GetItem(o, 0), args, NULL);
|
||||
if (r == NULL)
|
||||
PyErr_Print();
|
||||
else
|
||||
Py_DECREF(r);
|
||||
Py_DECREF(args);
|
||||
PyGILState_Release(gstate);
|
||||
}
|
||||
|
||||
PyObject *pycvCreateButton(PyObject*, PyObject *args, PyObject *kw)
|
||||
{
|
||||
const char* keywords[] = {"buttonName", "onChange", "userData", "buttonType", "initialButtonState", NULL};
|
||||
PyObject *on_change;
|
||||
PyObject *userdata = NULL;
|
||||
char* button_name;
|
||||
int button_type = 0;
|
||||
int initial_button_state = 0;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|Oii", (char**)keywords, &button_name, &on_change, &userdata, &button_type, &initial_button_state))
|
||||
return NULL;
|
||||
if (!PyCallable_Check(on_change)) {
|
||||
PyErr_SetString(PyExc_TypeError, "onChange must be callable");
|
||||
return NULL;
|
||||
}
|
||||
if (userdata == NULL) {
|
||||
userdata = Py_None;
|
||||
}
|
||||
|
||||
PyObject* py_callback_info = Py_BuildValue("OO", on_change, userdata);
|
||||
std::string name(button_name);
|
||||
|
||||
static std::map<std::string, PyObject*> registered_callbacks;
|
||||
std::map<std::string, PyObject*>::iterator i = registered_callbacks.find(name);
|
||||
if (i != registered_callbacks.end())
|
||||
{
|
||||
Py_DECREF(i->second);
|
||||
i->second = py_callback_info;
|
||||
}
|
||||
else
|
||||
{
|
||||
registered_callbacks.insert(std::pair<std::string, PyObject*>(name, py_callback_info));
|
||||
}
|
||||
ERRWRAP2(createButton(button_name, OnButtonChange, py_callback_info, button_type, initial_button_state != 0));
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
#endif // HAVE_OPENCV_HIGHGUI
|
||||
14
modules/python/src2/cv2_highgui.hpp
Normal file
14
modules/python/src2/cv2_highgui.hpp
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef CV2_HIGHGUI_HPP
|
||||
#define CV2_HIGHGUI_HPP
|
||||
|
||||
#include "cv2.hpp"
|
||||
#include "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifdef HAVE_OPENCV_HIGHGUI
|
||||
PyObject *pycvSetMouseCallback(PyObject*, PyObject *args, PyObject *kw);
|
||||
// workaround for #20408, use nullptr, set value later
|
||||
PyObject *pycvCreateTrackbar(PyObject*, PyObject *args);
|
||||
PyObject *pycvCreateButton(PyObject*, PyObject *args, PyObject *kw);
|
||||
#endif
|
||||
|
||||
#endif // CV2_HIGHGUI_HPP
|
||||
73
modules/python/src2/cv2_numpy.cpp
Normal file
73
modules/python/src2/cv2_numpy.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
// must be defined before importing numpy headers
|
||||
// https://numpy.org/doc/1.17/reference/c-api.array.html#importing-the-api
|
||||
#define NO_IMPORT_ARRAY
|
||||
#define PY_ARRAY_UNIQUE_SYMBOL opencv_ARRAY_API
|
||||
|
||||
#include "cv2_numpy.hpp"
|
||||
#include "cv2_util.hpp"
|
||||
|
||||
NumpyAllocator g_numpyAllocator;
|
||||
|
||||
using namespace cv;
|
||||
|
||||
UMatData* NumpyAllocator::allocate(PyObject* o, int dims, const int* sizes, int type, size_t* step) const
|
||||
{
|
||||
UMatData* u = new UMatData(this);
|
||||
u->data = u->origdata = (uchar*)PyArray_DATA((PyArrayObject*) o);
|
||||
npy_intp* _strides = PyArray_STRIDES((PyArrayObject*) o);
|
||||
for( int i = 0; i < dims - 1; i++ )
|
||||
step[i] = (size_t)_strides[i];
|
||||
step[dims-1] = CV_ELEM_SIZE(type);
|
||||
u->size = sizes[0]*step[0];
|
||||
u->userdata = o;
|
||||
return u;
|
||||
}
|
||||
|
||||
UMatData* NumpyAllocator::allocate(int dims0, const int* sizes, int type, void* data, size_t* step, AccessFlag flags, UMatUsageFlags usageFlags) const
|
||||
{
|
||||
if( data != 0 )
|
||||
{
|
||||
// issue #6969: CV_Error(Error::StsAssert, "The data should normally be NULL!");
|
||||
// probably this is safe to do in such extreme case
|
||||
return stdAllocator->allocate(dims0, sizes, type, data, step, flags, usageFlags);
|
||||
}
|
||||
PyEnsureGIL gil;
|
||||
|
||||
int depth = CV_MAT_DEPTH(type);
|
||||
int cn = CV_MAT_CN(type);
|
||||
const int f = (int)(sizeof(size_t)/8);
|
||||
int typenum = depth == CV_8U ? NPY_UBYTE : depth == CV_8S ? NPY_BYTE :
|
||||
depth == CV_16U ? NPY_USHORT : depth == CV_16S ? NPY_SHORT :
|
||||
depth == CV_32S ? NPY_INT : depth == CV_32F ? NPY_FLOAT :
|
||||
depth == CV_64F ? NPY_DOUBLE : f*NPY_ULONGLONG + (f^1)*NPY_UINT;
|
||||
int i, dims = dims0;
|
||||
cv::AutoBuffer<npy_intp> _sizes(dims + 1);
|
||||
for( i = 0; i < dims; i++ )
|
||||
_sizes[i] = sizes[i];
|
||||
if( cn > 1 )
|
||||
_sizes[dims++] = cn;
|
||||
PyObject* o = PyArray_SimpleNew(dims, _sizes.data(), typenum);
|
||||
if(!o)
|
||||
CV_Error_(Error::StsError, ("The numpy array of typenum=%d, ndims=%d can not be created", typenum, dims));
|
||||
return allocate(o, dims0, sizes, type, step);
|
||||
}
|
||||
|
||||
bool NumpyAllocator::allocate(UMatData* u, AccessFlag accessFlags, UMatUsageFlags usageFlags) const
|
||||
{
|
||||
return stdAllocator->allocate(u, accessFlags, usageFlags);
|
||||
}
|
||||
|
||||
void NumpyAllocator::deallocate(UMatData* u) const
|
||||
{
|
||||
if(!u)
|
||||
return;
|
||||
PyEnsureGIL gil;
|
||||
CV_Assert(u->urefcount >= 0);
|
||||
CV_Assert(u->refcount >= 0);
|
||||
if(u->refcount == 0)
|
||||
{
|
||||
PyObject* o = (PyObject*)u->userdata;
|
||||
Py_XDECREF(o);
|
||||
delete u;
|
||||
}
|
||||
}
|
||||
217
modules/python/src2/cv2_numpy.hpp
Normal file
217
modules/python/src2/cv2_numpy.hpp
Normal file
@ -0,0 +1,217 @@
|
||||
#ifndef CV2_NUMPY_HPP
|
||||
#define CV2_NUMPY_HPP
|
||||
|
||||
#include "cv2.hpp"
|
||||
#include "opencv2/core.hpp"
|
||||
|
||||
class NumpyAllocator : public cv::MatAllocator
|
||||
{
|
||||
public:
|
||||
NumpyAllocator() { stdAllocator = cv::Mat::getStdAllocator(); }
|
||||
~NumpyAllocator() {}
|
||||
|
||||
cv::UMatData* allocate(PyObject* o, int dims, const int* sizes, int type, size_t* step) const;
|
||||
cv::UMatData* allocate(int dims0, const int* sizes, int type, void* data, size_t* step, cv::AccessFlag flags, cv::UMatUsageFlags usageFlags) const CV_OVERRIDE;
|
||||
bool allocate(cv::UMatData* u, cv::AccessFlag accessFlags, cv::UMatUsageFlags usageFlags) const CV_OVERRIDE;
|
||||
void deallocate(cv::UMatData* u) const CV_OVERRIDE;
|
||||
|
||||
const cv::MatAllocator* stdAllocator;
|
||||
};
|
||||
|
||||
extern NumpyAllocator g_numpyAllocator;
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
// HACK(?): function from cv2_util.hpp
|
||||
extern int failmsg(const char *fmt, ...);
|
||||
|
||||
namespace {
|
||||
|
||||
template<class T>
|
||||
NPY_TYPES asNumpyType()
|
||||
{
|
||||
return NPY_OBJECT;
|
||||
}
|
||||
|
||||
template<>
|
||||
NPY_TYPES asNumpyType<bool>()
|
||||
{
|
||||
return NPY_BOOL;
|
||||
}
|
||||
|
||||
#define CV_GENERATE_INTEGRAL_TYPE_NPY_CONVERSION(src, dst) \
|
||||
template<> \
|
||||
NPY_TYPES asNumpyType<src>() \
|
||||
{ \
|
||||
return NPY_##dst; \
|
||||
} \
|
||||
template<> \
|
||||
NPY_TYPES asNumpyType<u##src>() \
|
||||
{ \
|
||||
return NPY_U##dst; \
|
||||
}
|
||||
|
||||
CV_GENERATE_INTEGRAL_TYPE_NPY_CONVERSION(int8_t, INT8);
|
||||
|
||||
CV_GENERATE_INTEGRAL_TYPE_NPY_CONVERSION(int16_t, INT16);
|
||||
|
||||
CV_GENERATE_INTEGRAL_TYPE_NPY_CONVERSION(int32_t, INT32);
|
||||
|
||||
CV_GENERATE_INTEGRAL_TYPE_NPY_CONVERSION(int64_t, INT64);
|
||||
|
||||
#undef CV_GENERATE_INTEGRAL_TYPE_NPY_CONVERSION
|
||||
|
||||
template<>
|
||||
NPY_TYPES asNumpyType<float>()
|
||||
{
|
||||
return NPY_FLOAT;
|
||||
}
|
||||
|
||||
template<>
|
||||
NPY_TYPES asNumpyType<double>()
|
||||
{
|
||||
return NPY_DOUBLE;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
PyArray_Descr* getNumpyTypeDescriptor()
|
||||
{
|
||||
return PyArray_DescrFromType(asNumpyType<T>());
|
||||
}
|
||||
|
||||
template <>
|
||||
PyArray_Descr* getNumpyTypeDescriptor<size_t>()
|
||||
{
|
||||
#if SIZE_MAX == ULONG_MAX
|
||||
return PyArray_DescrFromType(NPY_ULONG);
|
||||
#elif SIZE_MAX == ULLONG_MAX
|
||||
return PyArray_DescrFromType(NPY_ULONGLONG);
|
||||
#else
|
||||
return PyArray_DescrFromType(NPY_UINT);
|
||||
#endif
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
bool isRepresentable(U value) {
|
||||
return (std::numeric_limits<T>::min() <= value) && (value <= std::numeric_limits<T>::max());
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool canBeSafelyCasted(PyObject* obj, PyArray_Descr* to)
|
||||
{
|
||||
return PyArray_CanCastTo(PyArray_DescrFromScalar(obj), to) != 0;
|
||||
}
|
||||
|
||||
|
||||
template<>
|
||||
bool canBeSafelyCasted<size_t>(PyObject* obj, PyArray_Descr* to)
|
||||
{
|
||||
PyArray_Descr* from = PyArray_DescrFromScalar(obj);
|
||||
if (PyArray_CanCastTo(from, to))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// False negative scenarios:
|
||||
// - Signed input is positive so it can be safely cast to unsigned output
|
||||
// - Input has wider limits but value is representable within output limits
|
||||
// - All the above
|
||||
if (PyDataType_ISSIGNED(from))
|
||||
{
|
||||
int64_t input = 0;
|
||||
PyArray_CastScalarToCtype(obj, &input, getNumpyTypeDescriptor<int64_t>());
|
||||
return (input >= 0) && isRepresentable<size_t>(static_cast<uint64_t>(input));
|
||||
}
|
||||
else
|
||||
{
|
||||
uint64_t input = 0;
|
||||
PyArray_CastScalarToCtype(obj, &input, getNumpyTypeDescriptor<uint64_t>());
|
||||
return isRepresentable<size_t>(input);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
bool parseNumpyScalar(PyObject* obj, T& value)
|
||||
{
|
||||
if (PyArray_CheckScalar(obj))
|
||||
{
|
||||
// According to the numpy documentation:
|
||||
// There are 21 statically-defined PyArray_Descr objects for the built-in data-types
|
||||
// So descriptor pointer is not owning.
|
||||
PyArray_Descr* to = getNumpyTypeDescriptor<T>();
|
||||
if (canBeSafelyCasted<T>(obj, to))
|
||||
{
|
||||
PyArray_CastScalarToCtype(obj, &value, to);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
struct SafeSeqItem
|
||||
{
|
||||
PyObject * item;
|
||||
SafeSeqItem(PyObject *obj, size_t idx) { item = PySequence_GetItem(obj, idx); }
|
||||
~SafeSeqItem() { Py_XDECREF(item); }
|
||||
|
||||
private:
|
||||
SafeSeqItem(const SafeSeqItem&); // = delete
|
||||
SafeSeqItem& operator=(const SafeSeqItem&); // = delete
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class RefWrapper
|
||||
{
|
||||
public:
|
||||
RefWrapper(T& item) : item_(item) {}
|
||||
|
||||
T& get() CV_NOEXCEPT { return item_; }
|
||||
|
||||
private:
|
||||
T& item_;
|
||||
};
|
||||
|
||||
// In order to support this conversion on 3.x branch - use custom reference_wrapper
|
||||
// and C-style array instead of std::array<T, N>
|
||||
template <class T, std::size_t N>
|
||||
bool parseSequence(PyObject* obj, RefWrapper<T> (&value)[N], const ArgInfo& info)
|
||||
{
|
||||
if (!obj || obj == Py_None)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!PySequence_Check(obj))
|
||||
{
|
||||
failmsg("Can't parse '%s'. Input argument doesn't provide sequence "
|
||||
"protocol", info.name);
|
||||
return false;
|
||||
}
|
||||
const std::size_t sequenceSize = PySequence_Size(obj);
|
||||
if (sequenceSize != N)
|
||||
{
|
||||
failmsg("Can't parse '%s'. Expected sequence length %lu, got %lu",
|
||||
info.name, N, sequenceSize);
|
||||
return false;
|
||||
}
|
||||
for (std::size_t i = 0; i < N; ++i)
|
||||
{
|
||||
SafeSeqItem seqItem(obj, i);
|
||||
if (!pyopencv_to(seqItem.item, value[i].get(), info))
|
||||
{
|
||||
failmsg("Can't parse '%s'. Sequence item with index %lu has a "
|
||||
"wrong type", info.name, i);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
#endif // CV2_NUMPY_HPP
|
||||
178
modules/python/src2/cv2_util.cpp
Normal file
178
modules/python/src2/cv2_util.cpp
Normal file
@ -0,0 +1,178 @@
|
||||
#include "cv2_util.hpp"
|
||||
#include "opencv2/core.hpp"
|
||||
#include "opencv2/core/utils/configuration.private.hpp"
|
||||
#include "opencv2/core/utils/logger.hpp"
|
||||
|
||||
PyObject* opencv_error = NULL;
|
||||
cv::TLSData<std::vector<std::string> > conversionErrorsTLS;
|
||||
|
||||
using namespace cv;
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
bool isPythonBindingsDebugEnabled()
|
||||
{
|
||||
static bool param_debug = cv::utils::getConfigurationParameterBool("OPENCV_PYTHON_DEBUG", false);
|
||||
return param_debug;
|
||||
}
|
||||
|
||||
void emit_failmsg(PyObject * exc, const char *msg)
|
||||
{
|
||||
static bool param_debug = isPythonBindingsDebugEnabled();
|
||||
if (param_debug)
|
||||
{
|
||||
CV_LOG_WARNING(NULL, "Bindings conversion failed: " << msg);
|
||||
}
|
||||
PyErr_SetString(exc, msg);
|
||||
}
|
||||
|
||||
int failmsg(const char *fmt, ...)
|
||||
{
|
||||
char str[1000];
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(str, sizeof(str), fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
emit_failmsg(PyExc_TypeError, str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject* failmsgp(const char *fmt, ...)
|
||||
{
|
||||
char str[1000];
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(str, sizeof(str), fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
emit_failmsg(PyExc_TypeError, str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void pyRaiseCVException(const cv::Exception &e)
|
||||
{
|
||||
PyObject_SetAttrString(opencv_error, "file", PyString_FromString(e.file.c_str()));
|
||||
PyObject_SetAttrString(opencv_error, "func", PyString_FromString(e.func.c_str()));
|
||||
PyObject_SetAttrString(opencv_error, "line", PyInt_FromLong(e.line));
|
||||
PyObject_SetAttrString(opencv_error, "code", PyInt_FromLong(e.code));
|
||||
PyObject_SetAttrString(opencv_error, "msg", PyString_FromString(e.msg.c_str()));
|
||||
PyObject_SetAttrString(opencv_error, "err", PyString_FromString(e.err.c_str()));
|
||||
PyErr_SetString(opencv_error, e.what());
|
||||
}
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
void pyRaiseCVOverloadException(const std::string& functionName)
|
||||
{
|
||||
const std::vector<std::string>& conversionErrors = conversionErrorsTLS.getRef();
|
||||
const std::size_t conversionErrorsCount = conversionErrors.size();
|
||||
if (conversionErrorsCount > 0)
|
||||
{
|
||||
// In modern std libraries small string optimization is used = no dynamic memory allocations,
|
||||
// but it can be applied only for string with length < 18 symbols (in GCC)
|
||||
const std::string bullet = "\n - ";
|
||||
|
||||
// Estimate required buffer size - save dynamic memory allocations = faster
|
||||
std::size_t requiredBufferSize = bullet.size() * conversionErrorsCount;
|
||||
for (std::size_t i = 0; i < conversionErrorsCount; ++i)
|
||||
{
|
||||
requiredBufferSize += conversionErrors[i].size();
|
||||
}
|
||||
|
||||
// Only string concatenation is required so std::string is way faster than
|
||||
// std::ostringstream
|
||||
std::string errorMessage("Overload resolution failed:");
|
||||
errorMessage.reserve(errorMessage.size() + requiredBufferSize);
|
||||
for (std::size_t i = 0; i < conversionErrorsCount; ++i)
|
||||
{
|
||||
errorMessage += bullet;
|
||||
errorMessage += conversionErrors[i];
|
||||
}
|
||||
cv::Exception exception(Error::StsBadArg, errorMessage, functionName, "", -1);
|
||||
pyRaiseCVException(exception);
|
||||
}
|
||||
else
|
||||
{
|
||||
cv::Exception exception(Error::StsInternal, "Overload resolution failed, but no errors reported",
|
||||
functionName, "", -1);
|
||||
pyRaiseCVException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
void pyPopulateArgumentConversionErrors()
|
||||
{
|
||||
if (PyErr_Occurred())
|
||||
{
|
||||
PySafeObject exception_type;
|
||||
PySafeObject exception_value;
|
||||
PySafeObject exception_traceback;
|
||||
PyErr_Fetch(exception_type, exception_value, exception_traceback);
|
||||
PyErr_NormalizeException(exception_type, exception_value,
|
||||
exception_traceback);
|
||||
|
||||
PySafeObject exception_message(PyObject_Str(exception_value));
|
||||
std::string message;
|
||||
getUnicodeString(exception_message, message);
|
||||
#ifdef CV_CXX11
|
||||
conversionErrorsTLS.getRef().push_back(std::move(message));
|
||||
#else
|
||||
conversionErrorsTLS.getRef().push_back(message);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
static int OnError(int status, const char *func_name, const char *err_msg, const char *file_name, int line, void *userdata)
|
||||
{
|
||||
PyGILState_STATE gstate;
|
||||
gstate = PyGILState_Ensure();
|
||||
|
||||
PyObject *on_error = (PyObject*)userdata;
|
||||
PyObject *args = Py_BuildValue("isssi", status, func_name, err_msg, file_name, line);
|
||||
|
||||
PyObject *r = PyObject_Call(on_error, args, NULL);
|
||||
if (r == NULL) {
|
||||
PyErr_Print();
|
||||
} else {
|
||||
Py_DECREF(r);
|
||||
}
|
||||
|
||||
Py_DECREF(args);
|
||||
PyGILState_Release(gstate);
|
||||
|
||||
return 0; // The return value isn't used
|
||||
}
|
||||
|
||||
PyObject *pycvRedirectError(PyObject*, PyObject *args, PyObject *kw)
|
||||
{
|
||||
const char *keywords[] = { "on_error", NULL };
|
||||
PyObject *on_error;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "O", (char**)keywords, &on_error))
|
||||
return NULL;
|
||||
|
||||
if ((on_error != Py_None) && !PyCallable_Check(on_error)) {
|
||||
PyErr_SetString(PyExc_TypeError, "on_error must be callable");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Keep track of the previous handler parameter, so we can decref it when no longer used
|
||||
static PyObject* last_on_error = NULL;
|
||||
if (last_on_error) {
|
||||
Py_DECREF(last_on_error);
|
||||
last_on_error = NULL;
|
||||
}
|
||||
|
||||
if (on_error == Py_None) {
|
||||
ERRWRAP2(redirectError(NULL));
|
||||
} else {
|
||||
last_on_error = on_error;
|
||||
Py_INCREF(last_on_error);
|
||||
ERRWRAP2(redirectError(OnError, last_on_error));
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
134
modules/python/src2/cv2_util.hpp
Normal file
134
modules/python/src2/cv2_util.hpp
Normal file
@ -0,0 +1,134 @@
|
||||
#ifndef CV2_UTIL_HPP
|
||||
#define CV2_UTIL_HPP
|
||||
|
||||
#include "cv2.hpp"
|
||||
#include "opencv2/core.hpp"
|
||||
#include "opencv2/core/utils/tls.hpp"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
bool isPythonBindingsDebugEnabled();
|
||||
void emit_failmsg(PyObject * exc, const char *msg);
|
||||
int failmsg(const char *fmt, ...);
|
||||
PyObject* failmsgp(const char *fmt, ...);;
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
class PyAllowThreads
|
||||
{
|
||||
public:
|
||||
PyAllowThreads() : _state(PyEval_SaveThread()) {}
|
||||
~PyAllowThreads()
|
||||
{
|
||||
PyEval_RestoreThread(_state);
|
||||
}
|
||||
private:
|
||||
PyThreadState* _state;
|
||||
};
|
||||
|
||||
class PyEnsureGIL
|
||||
{
|
||||
public:
|
||||
PyEnsureGIL() : _state(PyGILState_Ensure()) {}
|
||||
~PyEnsureGIL()
|
||||
{
|
||||
PyGILState_Release(_state);
|
||||
}
|
||||
private:
|
||||
PyGILState_STATE _state;
|
||||
};
|
||||
|
||||
/**
|
||||
* Light weight RAII wrapper for `PyObject*` owning references.
|
||||
* In comparisson to C++11 `std::unique_ptr` with custom deleter, it provides
|
||||
* implicit conversion functions that might be useful to initialize it with
|
||||
* Python functions those returns owning references through the `PyObject**`
|
||||
* e.g. `PyErr_Fetch` or directly pass it to functions those want to borrow
|
||||
* reference to object (doesn't extend object lifetime) e.g. `PyObject_Str`.
|
||||
*/
|
||||
class PySafeObject
|
||||
{
|
||||
public:
|
||||
PySafeObject() : obj_(NULL) {}
|
||||
|
||||
explicit PySafeObject(PyObject* obj) : obj_(obj) {}
|
||||
|
||||
~PySafeObject()
|
||||
{
|
||||
Py_CLEAR(obj_);
|
||||
}
|
||||
|
||||
operator PyObject*()
|
||||
{
|
||||
return obj_;
|
||||
}
|
||||
|
||||
operator PyObject**()
|
||||
{
|
||||
return &obj_;
|
||||
}
|
||||
|
||||
PyObject* release()
|
||||
{
|
||||
PyObject* obj = obj_;
|
||||
obj_ = NULL;
|
||||
return obj;
|
||||
}
|
||||
|
||||
private:
|
||||
PyObject* obj_;
|
||||
|
||||
// Explicitly disable copy operations
|
||||
PySafeObject(const PySafeObject*); // = delete
|
||||
PySafeObject& operator=(const PySafeObject&); // = delete
|
||||
};
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
extern PyObject* opencv_error;
|
||||
|
||||
void pyRaiseCVException(const cv::Exception &e);
|
||||
|
||||
#define ERRWRAP2(expr) \
|
||||
try \
|
||||
{ \
|
||||
PyAllowThreads allowThreads; \
|
||||
expr; \
|
||||
} \
|
||||
catch (const cv::Exception &e) \
|
||||
{ \
|
||||
pyRaiseCVException(e); \
|
||||
return 0; \
|
||||
} \
|
||||
catch (const std::exception &e) \
|
||||
{ \
|
||||
PyErr_SetString(opencv_error, e.what()); \
|
||||
return 0; \
|
||||
} \
|
||||
catch (...) \
|
||||
{ \
|
||||
PyErr_SetString(opencv_error, "Unknown C++ exception from OpenCV code"); \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
extern cv::TLSData<std::vector<std::string> > conversionErrorsTLS;
|
||||
|
||||
inline void pyPrepareArgumentConversionErrorsStorage(std::size_t size)
|
||||
{
|
||||
std::vector<std::string>& conversionErrors = conversionErrorsTLS.getRef();
|
||||
conversionErrors.clear();
|
||||
conversionErrors.reserve(size);
|
||||
}
|
||||
|
||||
void pyRaiseCVOverloadException(const std::string& functionName);
|
||||
void pyPopulateArgumentConversionErrors();
|
||||
|
||||
//======================================================================================================================
|
||||
|
||||
PyObject *pycvRedirectError(PyObject*, PyObject *args, PyObject *kw);
|
||||
|
||||
#endif // CV2_UTIL_HPP
|
||||
@ -44,6 +44,8 @@
|
||||
#ifndef __PYCOMPAT_HPP__
|
||||
#define __PYCOMPAT_HPP__
|
||||
|
||||
#include <string>
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
|
||||
// Python3 treats all ints as longs, PyInt_X functions have been removed.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user