diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index c3f930aa81..ee074814ee 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -137,7 +137,8 @@ static PyMethodDef special_methods[] = { {"redirectError", CV_PY_FN_WITH_KW(pycvRedirectError), "redirectError(onError) -> None"}, {"setUseExactNumpyDtypes", (PyCFunction)(pycvSetUseExactNumpyDtypes), METH_O, "setUseExactNumpyDtypes(flag) -> None\n. Keep output bfloat16 / float8 arrays at their exact dtype " - "(registered by the ml_dtypes package, which must be imported) instead of widening them to float32. Off by default. Input arrays are accepted regardless of this flag. " + "(registered by the ml_dtypes package, which must be imported) instead of widening them " + "to float32. Off by default. Input arrays are accepted regardless of this flag. " "The flag is process-wide: set it in the application, libraries should not change it."}, {"useExactNumpyDtypes", (PyCFunction)(pycvUseExactNumpyDtypes), METH_NOARGS, "useExactNumpyDtypes() -> retval"}, diff --git a/modules/python/src2/cv2_convert.cpp b/modules/python/src2/cv2_convert.cpp index 8f617e4bd9..fd071bc0b3 100644 --- a/modules/python/src2/cv2_convert.cpp +++ b/modules/python/src2/cv2_convert.cpp @@ -372,7 +372,8 @@ PyObject* pyopencv_from(const cv::Mat& m) } if( useExactNumpyDtypes() ) CV_LOG_ONCE_WARNING(NULL, "Python bindings: setUseExactNumpyDtypes(True) is set, " - "but NumPy does not know bfloat16 / float8 and they are returned as float32. " + "but NumPy does not know bfloat16 / float8 and they are " + "returned as float32. " "Import ml_dtypes to keep the exact dtype."); // NumPy alone cannot name these; widen to float32 (lossless). cv::Mat m32f; diff --git a/modules/python/src2/cv2_numpy.cpp b/modules/python/src2/cv2_numpy.cpp index 92023d4fd9..2895c17e64 100644 --- a/modules/python/src2/cv2_numpy.cpp +++ b/modules/python/src2/cv2_numpy.cpp @@ -53,10 +53,12 @@ UMatData* NumpyAllocator::allocate(int dims0, const int* sizes, int type, void* CV_Error_(Error::StsError, ("The numpy array of depth=%d, ndims=%d can not be created", depth, dims)); return allocate(eo, dims0, sizes, type, step); } - // Mat::create() falls back to the default allocator on this error; pyopencv_from() then widens to float32. + // Mat::create() falls back to the default allocator on this error; + // pyopencv_from() then widens the result to float32. if( isNonConventionalDepth(depth) ) CV_Error(Error::StsNotImplemented, useExactNumpyDtypes() - ? "bfloat16 / float8 arrays cannot be allocated through the NumPy allocator: import ml_dtypes first" + ? "bfloat16 / float8 arrays cannot be allocated through the NumPy allocator: " + "import ml_dtypes first" : "bfloat16 / float8 arrays cannot be allocated through the NumPy allocator: " "call cv.setUseExactNumpyDtypes(True) first"); diff --git a/modules/python/src2/cv2_util.cpp b/modules/python/src2/cv2_util.cpp index 9680b78607..c810ffb850 100644 --- a/modules/python/src2/cv2_util.cpp +++ b/modules/python/src2/cv2_util.cpp @@ -53,7 +53,8 @@ const NonConventionalDtype kNonConventionalDtypes[] = { { CV_8F_E4M3FN, "float8_e4m3fn", 1 }, { CV_8F_E4M3FNUZ, "float8_e4m3fnuz", 1 }, }; -const int kNumNonConventionalDtypes = sizeof(kNonConventionalDtypes) / sizeof(kNonConventionalDtypes[0]); +const int kNumNonConventionalDtypes = + sizeof(kNonConventionalDtypes) / sizeof(kNonConventionalDtypes[0]); bool g_useExactNumpyDtypes = false; diff --git a/modules/python/test/test_misc.py b/modules/python/test/test_misc.py index 984bd8ef47..41e38ee04e 100644 --- a/modules/python/test/test_misc.py +++ b/modules/python/test/test_misc.py @@ -243,7 +243,8 @@ class Bindings(NewOpenCVTests): def test_type_constants_for_all_depths(self): # Same depth list as export_matrix_type_constants() in api_refinement.py. depths = ("CV_8U", "CV_8S", "CV_16U", "CV_16S", "CV_32S", "CV_32F", "CV_64F", "CV_16F", - "CV_16BF", "CV_Bool", "CV_64U", "CV_64S", "CV_32U", "CV_8F_E4M3FN", "CV_8F_E4M3FNUZ") + "CV_16BF", "CV_Bool", "CV_64U", "CV_64S", "CV_32U", + "CV_8F_E4M3FN", "CV_8F_E4M3FNUZ") for name in depths: depth = getattr(cv, name) make_type = getattr(cv, name + "C") diff --git a/modules/python/test/test_ml_dtypes.py b/modules/python/test/test_ml_dtypes.py index 94028c890d..6edbb0d411 100644 --- a/modules/python/test/test_ml_dtypes.py +++ b/modules/python/test/test_ml_dtypes.py @@ -99,9 +99,9 @@ class ml_dtypes_test(NewOpenCVTests): values = np.arange(channels, dtype=np.float32) - 1 operand = values.astype(dt) expected = cv.add(src, tuple(float(v) for v in values)) + reversed_expected = cv.add(src, tuple(float(v) for v in values[::-1])) np.testing.assert_array_equal(cv.add(src, operand), expected) - np.testing.assert_array_equal(cv.add(src, operand[::-1]), - cv.add(src, tuple(float(v) for v in values[::-1]))) + np.testing.assert_array_equal(cv.add(src, operand[::-1]), reversed_expected) img = np.zeros((2, 3, 3), dtype=ml_dtypes.bfloat16) operand = np.array([1, 2, 3], dtype=ml_dtypes.bfloat16) self.assertSameBits(cv.add(img, operand), img + operand)