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

Merge pull request #23958 from VadimLevin:dev/vlevin/friendly-wrong-npy-type-message

feat: update NumPy type to Mat type fail message
This commit is contained in:
Alexander Smorkalov
2023-07-11 10:42:41 +03:00
committed by GitHub
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;
}
}
+5 -1
View File
@@ -42,7 +42,7 @@ private:
/**
* Light weight RAII wrapper for `PyObject*` owning references.
* In comparisson to C++11 `std::unique_ptr` with custom deleter, it provides
* In comparison 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
@@ -70,6 +70,10 @@ public:
return &obj_;
}
operator bool() {
return static_cast<bool>(obj_);
}
PyObject* release()
{
PyObject* obj = obj_;