From b4af9c620a9b068025fc952623c3c34d72a63a63 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Fri, 23 Jul 2010 17:55:37 +0000 Subject: [PATCH] cvarrseq args attempt array conversions first --- modules/python/cv.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/modules/python/cv.cpp b/modules/python/cv.cpp index 5a92ec4cac..b19cfde8ee 100644 --- a/modules/python/cv.cpp +++ b/modules/python/cv.cpp @@ -1718,10 +1718,28 @@ struct cvarrseq { }; }; +static int is_convertible_to_mat(PyObject *o) +{ +#if PYTHON_USE_NUMPY + if (PyObject_HasAttrString(o, "__array_struct__")) { + PyObject *ao = PyObject_GetAttrString(o, "__array_struct__"); + if (ao != NULL && + PyCObject_Check(ao) && + ((PyArrayInterface*)PyCObject_AsVoidPtr(ao))->two == 2) { + return 1; + } + } +#endif + return is_iplimage(o) && is_cvmat(o) && is_cvmatnd(o); +} + static int convert_to_cvarrseq(PyObject *o, cvarrseq *dst, const char *name = "no_name") { if (PyType_IsSubtype(o->ob_type, &cvseq_Type)) { return convert_to_CvSeq(o, &(dst->seq), name); + } else if (is_convertible_to_mat(o)) { + int r = convert_to_CvArr(o, &(dst->mat), name); + return r; } else if (PySequence_Check(o)) { PyObject *fi = PySequence_Fast(o, name); if (fi == NULL) @@ -1747,7 +1765,11 @@ static int convert_to_cvarrseq(PyObject *o, cvarrseq *dst, const char *name = "n assert(fe != NULL); int *pdst = (int*)cvPtr2D(mt, i, 0); for (Py_ssize_t j = 0; j < size; j++) { - *pdst++ = PyInt_AsLong(PySequence_Fast_GET_ITEM(fe, j)); + PyObject *num = PySequence_Fast_GET_ITEM(fe, j); + if (!PyNumber_Check(num)) { + return failmsg("Sequence must contain numbers", name); + } + *pdst++ = PyInt_AsLong(num); } Py_DECREF(fe); } @@ -1755,7 +1777,7 @@ static int convert_to_cvarrseq(PyObject *o, cvarrseq *dst, const char *name = "n dst->mat = mt; return 1; } else { - return convert_to_CvArr(o, &(dst->mat), name); + return failmsg("Argument '%s' must be CvSeq, CvArr, or a sequence of numbers"); } }