Ejemplo n.º 1
0
def test_rectangle():

    r = Rectangle(1, 2, 3, 4)
    r.to_interval(axis="x")
    r.to_quadrilateral()
    assert r.pad(left=1, right=5, top=2, bottom=4) == Rectangle(0, 0, 8, 8)
    assert r.shift([1, 2]) == Rectangle(2, 4, 4, 6)
    assert r.shift(1) == Rectangle(2, 3, 4, 5)
    assert r.scale([3, 2]) == Rectangle(3, 4, 9, 8)
    assert r.scale(2) == Rectangle(2, 4, 6, 8)
    assert r.area == 4

    img = np.random.randint(12, 24, (40, 40))
    r.crop_image(img).shape == (2, 2)
Ejemplo n.º 2
0
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()