Exemplo n.º 1
0
 def test_rightmost_coordinate(self):
     # Polygon
     dummy_record = Record([[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
                           ('car', ),
                           confidence=0.9)
     geometry = Geometry(record=dummy_record)
     assert geometry.rightmost_coordinate == (1, 0)
     # Point
     dummy_record = Record([0, 1], ('car', ), confidence=0.9)
     geometry = Geometry(record=dummy_record)
     assert geometry.rightmost_coordinate == (0, 1)
Exemplo n.º 2
0
 def test_coordinates(self):
     # Polygon
     dummy_record = Record([[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
                           ('car', ),
                           confidence=0.9)
     geometry = Geometry(record=dummy_record)
     assert geometry.coordinates == geometry._coordinates
     # Point
     dummy_record = Record([0, 1], ('car', ), confidence=0.9)
     geometry = Geometry(record=dummy_record)
     assert geometry.coordinates == geometry._coordinates
Exemplo n.º 3
0
 def test_min_x(self):
     # Polygon
     dummy_record = Record([[[0, 0], [1, 1], [2, 2], [3, 3], [0, 0]]],
                           ('car', ),
                           confidence=0.9)
     geometry = Geometry(record=dummy_record)
     assert isinstance(geometry.min_x, int)
     assert geometry.min_x == 0
     # Point
     dummy_record = Record([0, 0], ('car', ), confidence=0.9)
     geometry = Geometry(record=dummy_record)
     assert isinstance(geometry.min_x, int)
     assert geometry.min_x == 0
Exemplo n.º 4
0
 def test_centroid(self):
     # Polygon
     dummy_record = Record([[[0, 0], [2, 0], [2, 2], [0, 2], [0, 0]]],
                           ('car', ),
                           confidence=0.9)
     geometry = Geometry(record=dummy_record)
     centroid = geometry.centroid
     assert isinstance(centroid, tuple)
     assert centroid == (1, 1)
     # Point
     dummy_record = Record([1, 2], ('car', ), confidence=0.9)
     geometry = Geometry(record=dummy_record)
     centroid = geometry.centroid
     assert isinstance(centroid, tuple)
     assert centroid == (1, 2)
Exemplo n.º 5
0
    def test_compute_label_starting_point(self):
        # Check
        dummy_record = Record([[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]],
                              ('car', ),
                              confidence=0.9)
        geometry = Geometry(record=dummy_record)
        starting_point = TagPainter._compute_label_starting_point(
            geometry=geometry, position=Position.RIGHT)

        assert isinstance(starting_point, tuple)
        assert starting_point == (1, 0)

        starting_point = TagPainter._compute_label_starting_point(
            geometry=geometry, position=Position.LEFT)

        assert isinstance(starting_point, tuple)
        assert starting_point == (0, 0)
Exemplo n.º 6
0
    def test_compute_label_coordinates(self):
        # Parameters
        _painter = TagPainter(Confidence())

        # Check
        dummy_record = Record([[[0, 5], [20, 5], [20, 25], [0, 25], [0, 5]]],
                              ('car', ),
                              confidence=0.9)
        geometry = Geometry(record=dummy_record)
        text = ''
        font = PIL.ImageFont.load_default()  # size = 11
        text_width, text_height = font.getsize(text)
        tile_width = 50

        label_coordinates, text_coordinates = _painter._compute_label_coordinates(
            geometry=geometry, text=text, font=font, tile_width=tile_width)

        assert isinstance(label_coordinates, list)
        assert len(label_coordinates) == 5
        for coordinate in label_coordinates:
            assert isinstance(coordinate, tuple)
            assert len(coordinate) == 2
        assert isinstance(text_coordinates, tuple)
Exemplo n.º 7
0
    def test_constructor(self):
        dummy_record_list = Record([[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]],
                                   ('car', ),
                                   confidence=0.9)
        dummy_record_tuple = Record(((
            (0, 0),
            (0, 1),
            (1, 1),
            (1, 0),
            (0, 0),
        ), ), ('car', ),
                                    confidence=0.9)

        geometry_list = Geometry(record=dummy_record_list)
        geometry_tuple = Geometry(record=dummy_record_tuple)

        # Check coordinates format
        assert isinstance(geometry_list.coordinates, list)
        for i, coordinate in enumerate(geometry_list.coordinates):
            assert isinstance(coordinate, tuple)
            assert coordinate == tuple(dummy_record_list.coordinates[0][i])

        assert isinstance(geometry_tuple.coordinates, list)
        for i, coordinate in enumerate(geometry_tuple.coordinates):
            assert isinstance(coordinate, tuple)
            assert coordinate == tuple(dummy_record_tuple.coordinates[0][i])

        # Check exceptions
        class InvalidRecord(object):
            def __init__(self, type, coordinates):
                self.type = type
                self.coordinates = coordinates

        with pytest.raises(ValueError):
            Geometry(record=object())
        with pytest.raises(ValueError):
            Geometry(record=InvalidRecord(type='Point', coordinates=[1]))
        with pytest.raises(ValueError):
            Geometry(record=InvalidRecord(type='Point', coordinates=[1, 2, 3]))
        with pytest.raises(ValueError):
            Geometry(record=InvalidRecord(type='Polygon', coordinates=[[]]))
        with pytest.raises(ValueError):
            Geometry(record=InvalidRecord(
                type='Polygon', coordinates=[[[0, 0], [1, 0], [1, 1]]]))
        with pytest.raises(ValueError):
            Geometry(record=InvalidRecord(
                type='Test', coordinates=[[[0, 0], [1, 0], [1, 1]]]))
        with pytest.raises(TypeError):
            Geometry(record=InvalidRecord(type='Point', coordinates='Test'))
        with pytest.raises(TypeError):
            Geometry(
                record=InvalidRecord(type='Polygon', coordinates=['Hello']))

        # Test zoom = 2
        dummy_record = Record([[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]],
                              ('car', ),
                              confidence=0.9)
        expected_coordinates = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
        geometry_list = Geometry(record=dummy_record, zoom=2)

        assert isinstance(geometry_list.coordinates, list)
        for i, coordinate in enumerate(geometry_list.coordinates):
            assert isinstance(coordinate, tuple)
            assert coordinate == expected_coordinates[i]

        # Test zoom = 0.6
        dummy_record = Record([[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]],
                              ('car', ),
                              confidence=0.9)
        expected_coordinates = [(0, 0), (0, 6), (6, 6), (6, 0), (0, 0)]
        geometry_list = Geometry(record=dummy_record, zoom=0.6)

        assert isinstance(geometry_list.coordinates, list)
        for i, coordinate in enumerate(geometry_list.coordinates):
            assert isinstance(coordinate, tuple)
            assert coordinate == expected_coordinates[i]