1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-28 23:03:03 +04:00

feat: update NumPy type to Mat type fail message

Output string representation of NumPy array type if it is not
convertible to OpenCV Mat type
This commit is contained in:
Vadim Levin
2023-07-10 18:05:32 +03:00
parent fdc0c12b7f
commit 953de60ff0
3 changed files with 37 additions and 2 deletions
+24 -1
View File
@@ -5,6 +5,7 @@
#include "cv2_convert.hpp"
#include "cv2_numpy.hpp"
#include "cv2_util.hpp"
#include "opencv2/core/utils/logger.hpp"
PyTypeObject* pyopencv_Mat_TypePtr = nullptr;
@@ -24,6 +25,26 @@ static std::string pycv_dumpArray(const T* arr, int n)
return out.str();
}
static inline std::string getArrayTypeName(PyArrayObject* arr)
{
PyArray_Descr* dtype = PyArray_DESCR(arr);
PySafeObject dtype_str(PyObject_Str(reinterpret_cast<PyObject*>(dtype)));
if (!dtype_str)
{
// Fallback to typenum value
return cv::format("%d", PyArray_TYPE(arr));
}
std::string type_name;
if (!getUnicodeString(dtype_str, type_name))
{
// Failed to get string from bytes object - clear set TypeError and
// fallback to typenum value
PyErr_Clear();
return cv::format("%d", PyArray_TYPE(arr));
}
return type_name;
}
//======================================================================================================================
// --- Mat
@@ -102,7 +123,9 @@ bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo& info)
}
else
{
failmsg("%s data type = %d is not supported", info.name, typenum);
const std::string dtype_name = getArrayTypeName(oarr);
failmsg("%s data type = %s is not supported", info.name,
dtype_name.c_str());
return false;
}
}