def test_quadrilateral(): points = np.array([[2, 2], [6, 2], [6, 7], [2, 6]]) q = Quadrilateral(points) q.to_interval() q.to_rectangle() assert q.shift(1) == Quadrilateral(points + 1) assert q.shift([1, 2]) == Quadrilateral(points + np.array([1, 2])) assert q.scale(2) == Quadrilateral(points * 2) assert q.scale([3, 2]) == Quadrilateral(points * np.array([3, 2])) assert q.pad(left=1, top=2, bottom=4) == Quadrilateral( np.array([[1, 0], [6, 0], [6, 11], [1, 10]])) assert (q.mapped_rectangle_points == np.array([[0, 0], [4, 0], [4, 5], [0, 5]])).all() points = np.array([[2, 2], [6, 2], [6, 5], [2, 5]]) q = Quadrilateral(points) img = np.random.randint(2, 24, (30, 20)).astype("uint8") img[2:5, 2:6] = 0 assert np.unique(q.crop_image(img)) == np.array([0]) q = Quadrilateral(np.array([[-2, 0], [0, 2], [2, 0], [0, -2]])) assert q.area == 8.0 q = Quadrilateral([1, 2, 3, 4, 5, 6, 7, 8]) assert (q.points == np.array([[1, 2], [3, 4], [5, 6], [7, 8]])).all() with pytest.raises(ValueError): Quadrilateral([1, 2, 3, 4, 5, 6, 7]) # Incompatible list length with pytest.raises(ValueError): Quadrilateral(np.array([[2, 2], [6, 2], [6, 5]])) # Incompatible ndarray shape
def test_quadrilateral(): points = np.array([[2, 2], [6, 2], [6, 7], [2, 6]]) q = Quadrilateral(points) q.to_interval() q.to_rectangle() assert q.shift(1) == Quadrilateral(points + 1) assert q.shift([1, 2]) == Quadrilateral(points + np.array([1, 2])) assert q.scale(2) == Quadrilateral(points * 2) assert q.scale([3, 2]) == Quadrilateral(points * np.array([3, 2])) assert q.pad(left=1, top=2, bottom=4) == Quadrilateral( np.array([[1, 0], [6, 0], [6, 11], [1, 10]])) assert (q.mapped_rectangle_points == np.array([[0, 0], [4, 0], [4, 5], [0, 5]])).all() points = np.array([[2, 2], [6, 2], [6, 5], [2, 5]]) q = Quadrilateral(points) img = np.random.randint(2, 24, (30, 20)).astype('uint8') img[2:5, 2:6] = 0 assert np.unique(q.crop_image(img)) == np.array([0]) q = Quadrilateral(np.array([[-2, 0], [0, 2], [2, 0], [0, -2]])) assert q.area == 8.
def test_rectangle_relations(): i = Interval(4, 5, axis="y") q = Quadrilateral(np.array([[2, 2], [6, 2], [6, 7], [2, 5]])) r = Rectangle(3, 3, 5, 6) assert not r.is_in(q) assert r.is_in(q, soft_margin={"bottom": 1}) assert r.is_in(q.to_rectangle()) assert r.is_in(q.to_interval()) # convert to absolute then convert back to relative assert r.condition_on(i).relative_to(i) == r assert r.condition_on(r).relative_to(r) == r assert r.condition_on(q).relative_to(q) == r.to_quadrilateral() # convert to relative then convert back to absolute assert r.relative_to(i).condition_on(i) == r assert r.relative_to(r).condition_on(r) == r assert r.relative_to(q).condition_on(q) == r.to_quadrilateral()
def test_textblock(): i = Interval(4, 5, axis="y") q = Quadrilateral(np.array([[2, 2], [6, 2], [6, 7], [2, 5]])) r = Rectangle(3, 3, 5, 6) t = TextBlock(i, id=1, type=2, text="12") assert (t.relative_to(q).condition_on(q).block == i.put_on_canvas( q).to_quadrilateral()) t.area t = TextBlock(r, id=1, type=2, parent="a") assert t.relative_to(i).condition_on(i).block == r t.area t = TextBlock(q, id=1, type=2, parent="a") assert t.relative_to(r).condition_on(r).block == q t.area # Ensure the operations did not change the object itself assert t == TextBlock(q, id=1, type=2, parent="a") t1 = TextBlock(q, id=1, type=2, parent="a") t2 = TextBlock(i, id=1, type=2, text="12") t1.relative_to(t2) assert t2.is_in(t1) t = TextBlock(q, score=0.2) # Additional test for shape conversion assert TextBlock(i, id=1, type=2, text="12").to_interval() == TextBlock(i, id=1, type=2, text="12") assert TextBlock(i, id=1, type=2, text="12").to_rectangle() == TextBlock(i.to_rectangle(), id=1, type=2, text="12") assert TextBlock(i, id=1, type=2, text="12").to_quadrilateral() == TextBlock( i.to_quadrilateral(), id=1, type=2, text="12") assert TextBlock(r, id=1, type=2, parent="a").to_interval(axis="x") == TextBlock( r.to_interval(axis="x"), id=1, type=2, parent="a") assert TextBlock(r, id=1, type=2, parent="a").to_interval(axis="y") == TextBlock( r.to_interval(axis="y"), id=1, type=2, parent="a") assert TextBlock(r, id=1, type=2, parent="a").to_rectangle() == TextBlock(r, id=1, type=2, parent="a") assert TextBlock(r, id=1, type=2, parent="a").to_quadrilateral() == TextBlock( r.to_quadrilateral(), id=1, type=2, parent="a") assert TextBlock(q, id=1, type=2, parent="a").to_interval(axis="x") == TextBlock( q.to_interval(axis="x"), id=1, type=2, parent="a") assert TextBlock(q, id=1, type=2, parent="a").to_interval(axis="y") == TextBlock( q.to_interval(axis="y"), id=1, type=2, parent="a") assert TextBlock(q, id=1, type=2, parent="a").to_rectangle() == TextBlock(q.to_rectangle(), id=1, type=2, parent="a") assert TextBlock(q, id=1, type=2, parent="a").to_quadrilateral() == TextBlock(q, id=1, type=2, parent="a") with pytest.raises(ValueError): TextBlock(q, id=1, type=2, parent="a").to_interval() TextBlock(r, id=1, type=2, parent="a").to_interval()