예제 #1
0
def test_interval():

    i = Interval(1, 2, axis="y", canvas_height=30, canvas_width=400)
    i.to_rectangle()
    i.to_quadrilateral()
    assert i.shift(1) == Interval(2,
                                  3,
                                  axis="y",
                                  canvas_height=30,
                                  canvas_width=400)
    assert i.area == 1 * 400

    i = Interval(1, 2, axis="x")
    assert i.shift([1, 2]) == Interval(2, 3, axis="x")
    assert i.scale([2, 1]) == Interval(2, 4, axis="x")
    assert i.pad(left=10, right=20) == Interval(0, 22,
                                                axis="x")  # Test the safe_mode
    assert i.pad(left=10, right=20, safe_mode=False) == Interval(-9,
                                                                 22,
                                                                 axis="x")
    assert i.area == 0

    img = np.random.randint(12, 24, (40, 40))
    img[:, 10:20] = 0
    i = Interval(5, 11, axis="x")
    assert np.unique(i.crop_image(img)[:, -1]) == np.array([0])
예제 #2
0
def test_layout():
    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")

    l = Layout([i, q, r])
    l.get_texts()
    l.condition_on(i)
    l.relative_to(q)
    l.filter_by(t)
    l.is_in(r)
    assert l.get_homogeneous_blocks() == [
        i.to_quadrilateral(), q, r.to_quadrilateral()
    ]

    i2 = TextBlock(i, id=1, type=2, text="12")
    r2 = TextBlock(r, id=1, type=2, parent="a")
    q2 = TextBlock(q, id=1, type=2, next="a")
    l2 = Layout([i2, r2, q2], page_data={"width": 200, "height": 200})

    l2.get_texts()
    l2.get_info("next")
    l2.condition_on(i)
    l2.relative_to(q)
    l2.filter_by(t)
    l2.is_in(r)

    l2.scale(4)
    l2.shift(4)
    l2.pad(left=2)

    # Test slicing function
    homogeneous_blocks = l2[:2].get_homogeneous_blocks()
    assert homogeneous_blocks[0].block == i.to_rectangle()
    assert homogeneous_blocks[1].block == r

    # Test appending and extending
    assert l + [i2] == Layout([i, q, r, i2])
    assert l + l == Layout([i, q, r] * 2)
    l.append(i)
    assert l == Layout([i, q, r, i])
    l2.extend([q])
    assert l2 == Layout([i2, r2, q2, q],
                        page_data={
                            "width": 200,
                            "height": 200
                        })

    # Test addition
    l + l2
    with pytest.raises(ValueError):
        l.page_data = {"width": 200, "height": 400}
        l + l2
예제 #3
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()