feat: common fixed size sequence conversion for Python bindings
This commit is contained in:
@@ -3,6 +3,7 @@ from __future__ import print_function
|
||||
|
||||
import ctypes
|
||||
from functools import partial
|
||||
from collections import namedtuple
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
@@ -378,6 +379,84 @@ class Arguments(NewOpenCVTests):
|
||||
with self.assertRaises((TypeError), msg=get_no_exception_msg(not_convertible)):
|
||||
_ = cv.utils.dumpString(not_convertible)
|
||||
|
||||
def test_parse_to_rect_convertible(self):
|
||||
Rect = namedtuple('Rect', ('x', 'y', 'w', 'h'))
|
||||
try_to_convert = partial(self._try_to_convert, cv.utils.dumpRect)
|
||||
for convertible in ((1, 2, 4, 5), [5, 3, 10, 20], np.array([10, 20, 23, 10]),
|
||||
Rect(10, 30, 40, 55), tuple(np.array([40, 20, 24, 20])),
|
||||
list(np.array([20, 40, 30, 35]))):
|
||||
expected = 'rect: (x={}, y={}, w={}, h={})'.format(*convertible)
|
||||
actual = try_to_convert(convertible)
|
||||
self.assertEqual(expected, actual,
|
||||
msg=get_conversion_error_msg(convertible, expected, actual))
|
||||
|
||||
def test_parse_to_rect_not_convertible(self):
|
||||
for not_convertible in (np.empty(shape=(4, 1)), (), [], np.array([]), (12, ),
|
||||
[3, 4, 5, 10, 123], {1: 2, 3:4, 5:10, 6:30},
|
||||
'1234', np.array([1, 2, 3, 4], dtype=np.float32),
|
||||
np.array([[1, 2], [3, 4], [5, 6], [6, 8]]), (1, 2, 5, 1.5)):
|
||||
with self.assertRaises((TypeError), msg=get_no_exception_msg(not_convertible)):
|
||||
_ = cv.utils.dumpRect(not_convertible)
|
||||
|
||||
def test_parse_to_rotated_rect_convertible(self):
|
||||
RotatedRect = namedtuple('RotatedRect', ('center', 'size', 'angle'))
|
||||
try_to_convert = partial(self._try_to_convert, cv.utils.dumpRotatedRect)
|
||||
for convertible in (((2.5, 2.5), (10., 20.), 12.5), [[1.5, 10.5], (12.5, 51.5), 10],
|
||||
RotatedRect((10, 40), np.array([10.5, 20.5]), 5),
|
||||
np.array([[10, 6], [50, 50], 5.5], dtype=object)):
|
||||
center, size, angle = convertible
|
||||
expected = 'rotated_rect: (c_x={:.6f}, c_y={:.6f}, w={:.6f},' \
|
||||
' h={:.6f}, a={:.6f})'.format(center[0], center[1],
|
||||
size[0], size[1], angle)
|
||||
actual = try_to_convert(convertible)
|
||||
self.assertEqual(expected, actual,
|
||||
msg=get_conversion_error_msg(convertible, expected, actual))
|
||||
|
||||
def test_parse_to_rotated_rect_not_convertible(self):
|
||||
for not_convertible in ([], (), np.array([]), (123, (45, 34), 1), {1: 2, 3: 4}, 123,
|
||||
np.array([[123, 123, 14], [1, 3], 56], dtype=object), '123'):
|
||||
with self.assertRaises((TypeError), msg=get_no_exception_msg(not_convertible)):
|
||||
_ = cv.utils.dumpRotatedRect(not_convertible)
|
||||
|
||||
def test_parse_to_term_criteria_convertible(self):
|
||||
TermCriteria = namedtuple('TermCriteria', ('type', 'max_count', 'epsilon'))
|
||||
try_to_convert = partial(self._try_to_convert, cv.utils.dumpTermCriteria)
|
||||
for convertible in ((1, 10, 1e-3), [2, 30, 1e-1], np.array([10, 20, 0.5], dtype=object),
|
||||
TermCriteria(0, 5, 0.1)):
|
||||
expected = 'term_criteria: (type={}, max_count={}, epsilon={:.6f}'.format(*convertible)
|
||||
actual = try_to_convert(convertible)
|
||||
self.assertEqual(expected, actual,
|
||||
msg=get_conversion_error_msg(convertible, expected, actual))
|
||||
|
||||
def test_parse_to_term_criteria_not_convertible(self):
|
||||
for not_convertible in ([], (), np.array([]), [1, 4], (10,), (1.5, 34, 0.1),
|
||||
{1: 5, 3: 5, 10: 10}, '145'):
|
||||
with self.assertRaises((TypeError), msg=get_no_exception_msg(not_convertible)):
|
||||
_ = cv.utils.dumpTermCriteria(not_convertible)
|
||||
|
||||
def test_parse_to_range_convertible_to_all(self):
|
||||
try_to_convert = partial(self._try_to_convert, cv.utils.dumpRange)
|
||||
for convertible in ((), [], np.array([])):
|
||||
expected = 'range: all'
|
||||
actual = try_to_convert(convertible)
|
||||
self.assertEqual(expected, actual,
|
||||
msg=get_conversion_error_msg(convertible, expected, actual))
|
||||
|
||||
def test_parse_to_range_convertible(self):
|
||||
Range = namedtuple('Range', ('start', 'end'))
|
||||
try_to_convert = partial(self._try_to_convert, cv.utils.dumpRange)
|
||||
for convertible in ((10, 20), [-1, 3], np.array([10, 24]), Range(-4, 6)):
|
||||
expected = 'range: (s={}, e={})'.format(*convertible)
|
||||
actual = try_to_convert(convertible)
|
||||
self.assertEqual(expected, actual,
|
||||
msg=get_conversion_error_msg(convertible, expected, actual))
|
||||
|
||||
def test_parse_to_range_not_convertible(self):
|
||||
for not_convertible in ((1, ), [40, ], np.array([1, 4, 6]), {'a': 1, 'b': 40},
|
||||
(1.5, 13.5), [3, 6.7], np.array([6.3, 2.1]), '14, 4'):
|
||||
with self.assertRaises((TypeError), msg=get_no_exception_msg(not_convertible)):
|
||||
_ = cv.utils.dumpRange(not_convertible)
|
||||
|
||||
|
||||
class SamplesFindFile(NewOpenCVTests):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user