File size: 3,305 Bytes
2366e36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Copyright (c) OpenMMLab. All rights reserved.
import math
from itertools import chain, permutations

import numpy as np
import pytest

from mmocr.datasets.pipelines.box_utils import sort_vertex, sort_vertex8
from mmocr.datasets.pipelines.crop import box_jitter, crop_img, warp_img


def test_order_vertex():
    dummy_points_x = [20, 20, 120, 120]
    dummy_points_y = [20, 40, 40, 20]

    expect_points_x = [20, 120, 120, 20]
    expect_points_y = [20, 20, 40, 40]

    with pytest.raises(AssertionError):
        sort_vertex([], dummy_points_y)
    with pytest.raises(AssertionError):
        sort_vertex(dummy_points_x, [])

    for perm in set(permutations([0, 1, 2, 3])):
        points_x = [dummy_points_x[i] for i in perm]
        points_y = [dummy_points_y[i] for i in perm]
        ordered_points_x, ordered_points_y = sort_vertex(points_x, points_y)

        assert np.allclose(ordered_points_x, expect_points_x)
        assert np.allclose(ordered_points_y, expect_points_y)


def test_sort_vertex8():
    dummy_points_x = [21, 21, 122, 122]
    dummy_points_y = [21, 39, 39, 21]

    expect_points = [21, 21, 122, 21, 122, 39, 21, 39]

    for perm in set(permutations([0, 1, 2, 3])):
        points_x = [dummy_points_x[i] for i in perm]
        points_y = [dummy_points_y[i] for i in perm]
        points = list(chain.from_iterable(zip(points_x, points_y)))
        ordered_points = sort_vertex8(points)

        assert np.allclose(ordered_points, expect_points)


def test_box_jitter():
    dummy_points_x = [20, 120, 120, 20]
    dummy_points_y = [20, 20, 40, 40]

    kwargs = dict(jitter_ratio_x=0.0, jitter_ratio_y=0.0)

    with pytest.raises(AssertionError):
        box_jitter([], dummy_points_y)
    with pytest.raises(AssertionError):
        box_jitter(dummy_points_x, [])
    with pytest.raises(AssertionError):
        box_jitter(dummy_points_x, dummy_points_y, jitter_ratio_x=1.)
    with pytest.raises(AssertionError):
        box_jitter(dummy_points_x, dummy_points_y, jitter_ratio_y=1.)

    box_jitter(dummy_points_x, dummy_points_y, **kwargs)

    assert np.allclose(dummy_points_x, [20, 120, 120, 20])
    assert np.allclose(dummy_points_y, [20, 20, 40, 40])


def test_opencv_crop():
    dummy_img = np.ones((600, 600, 3), dtype=np.uint8)
    dummy_box = [20, 20, 120, 20, 120, 40, 20, 40]

    cropped_img = warp_img(dummy_img, dummy_box)

    with pytest.raises(AssertionError):
        warp_img(dummy_img, [])
    with pytest.raises(AssertionError):
        warp_img(dummy_img, [20, 40, 40, 20])

    assert math.isclose(cropped_img.shape[0], 20)
    assert math.isclose(cropped_img.shape[1], 100)


def test_min_rect_crop():
    dummy_img = np.ones((600, 600, 3), dtype=np.uint8)
    dummy_box = [20, 20, 120, 20, 120, 40, 20, 40]

    cropped_img = crop_img(
        dummy_img,
        dummy_box,
        0.,
        0.,
    )

    with pytest.raises(AssertionError):
        crop_img(dummy_img, [])
    with pytest.raises(AssertionError):
        crop_img(dummy_img, [20, 40, 40, 20])
    with pytest.raises(AssertionError):
        crop_img(dummy_img, dummy_box, 4, 0.2)
    with pytest.raises(AssertionError):
        crop_img(dummy_img, dummy_box, 0.4, 1.2)

    assert math.isclose(cropped_img.shape[0], 20)
    assert math.isclose(cropped_img.shape[1], 100)