|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import pytest |
| 4 | +import numpy as np |
| 5 | +import random |
| 6 | + |
| 7 | +current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 8 | +sys.path.append(os.path.abspath(os.path.join(current_dir, ".."))) |
| 9 | + |
| 10 | +from ppocr.data.imaug.iaa_augment import IaaAugment |
| 11 | + |
| 12 | +# Set a fixed random seed for reproducibility |
| 13 | +np.random.seed(42) |
| 14 | +random.seed(42) |
| 15 | + |
| 16 | + |
| 17 | +# Fixtures for common test inputs |
| 18 | +@pytest.fixture |
| 19 | +def sample_image(): |
| 20 | + # Create a dummy image of size 100x100 with 3 channels |
| 21 | + return np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def sample_polys(): |
| 26 | + # Create some dummy polygons |
| 27 | + polys = [ |
| 28 | + np.array([[10, 10], [20, 10], [20, 20], [10, 20]], dtype=np.float32), |
| 29 | + np.array([[30, 30], [40, 30], [40, 40], [30, 40]], dtype=np.float32), |
| 30 | + ] |
| 31 | + return polys |
| 32 | + |
| 33 | + |
| 34 | +# Helper function to create data dictionary |
| 35 | +def create_data(sample_image, sample_polys): |
| 36 | + return { |
| 37 | + "image": sample_image.copy(), |
| 38 | + "polys": [poly.copy() for poly in sample_polys], |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +# Test default augmenter (with default augmenter_args) |
| 43 | +def test_iaa_augment_default(sample_image, sample_polys): |
| 44 | + data = create_data(sample_image, sample_polys) |
| 45 | + augmenter = IaaAugment() |
| 46 | + transformed_data = augmenter(data) |
| 47 | + |
| 48 | + assert isinstance( |
| 49 | + transformed_data["image"], np.ndarray |
| 50 | + ), "Image should be a numpy array" |
| 51 | + assert isinstance(transformed_data["polys"], list), "Polys should be a list" |
| 52 | + assert transformed_data["image"].ndim == 3, "Image should be 3-dimensional" |
| 53 | + |
| 54 | + # Check that the polys have been transformed |
| 55 | + polys_changed = any( |
| 56 | + not np.allclose(orig_poly, trans_poly) |
| 57 | + for orig_poly, trans_poly in zip(sample_polys, transformed_data["polys"]) |
| 58 | + ) |
| 59 | + assert polys_changed, "Polygons should have been transformed" |
| 60 | + |
| 61 | + |
| 62 | +# Test augmenter with empty augmenter_args (no augmentation) |
| 63 | +def test_iaa_augment_none(sample_image, sample_polys): |
| 64 | + data = create_data(sample_image, sample_polys) |
| 65 | + augmenter = IaaAugment(augmenter_args=[]) |
| 66 | + transformed_data = augmenter(data) |
| 67 | + |
| 68 | + assert np.array_equal( |
| 69 | + data["image"], transformed_data["image"] |
| 70 | + ), "Image should be unchanged" |
| 71 | + for orig_poly, transformed_poly in zip(data["polys"], transformed_data["polys"]): |
| 72 | + assert np.array_equal( |
| 73 | + orig_poly, transformed_poly |
| 74 | + ), "Polygons should be unchanged" |
| 75 | + |
| 76 | + |
| 77 | +# Parameterize tests to cover multiple augmenter_args scenarios |
| 78 | +@pytest.mark.parametrize( |
| 79 | + "augmenter_args, expected_shape", |
| 80 | + [ |
| 81 | + ([], (100, 100, 3)), |
| 82 | + ([{"type": "Resize", "args": {"size": [0.5, 0.5]}}], (50, 50, 3)), |
| 83 | + ([{"type": "Resize", "args": {"size": [2.0, 2.0]}}], (200, 200, 3)), |
| 84 | + ], |
| 85 | +) |
| 86 | +def test_iaa_augment_resize(sample_image, sample_polys, augmenter_args, expected_shape): |
| 87 | + data = create_data(sample_image, sample_polys) |
| 88 | + augmenter = IaaAugment(augmenter_args=augmenter_args) |
| 89 | + transformed_data = augmenter(data) |
| 90 | + |
| 91 | + assert ( |
| 92 | + transformed_data["image"].shape == expected_shape |
| 93 | + ), f"Expected image shape {expected_shape}, got {transformed_data['image'].shape}" |
| 94 | + |
| 95 | + |
| 96 | +# Test with custom augmenter_args |
| 97 | +def test_iaa_augment_custom(sample_image, sample_polys): |
| 98 | + data = create_data(sample_image, sample_polys) |
| 99 | + augmenter_args = [ |
| 100 | + {"type": "Affine", "args": {"rotate": [45, 45]}}, # Fixed rotation angle |
| 101 | + {"type": "Resize", "args": {"size": [0.5, 0.5]}}, |
| 102 | + ] |
| 103 | + augmenter = IaaAugment(augmenter_args=augmenter_args) |
| 104 | + transformed_data = augmenter(data) |
| 105 | + |
| 106 | + expected_height = int(sample_image.shape[0] * 0.5) |
| 107 | + expected_width = int(sample_image.shape[1] * 0.5) |
| 108 | + |
| 109 | + assert ( |
| 110 | + transformed_data["image"].shape[0] == expected_height |
| 111 | + ), "Image height should be scaled by 0.5" |
| 112 | + assert ( |
| 113 | + transformed_data["image"].shape[1] == expected_width |
| 114 | + ), "Image width should be scaled by 0.5" |
| 115 | + |
| 116 | + # Check that the polys have been transformed |
| 117 | + polys_changed = any( |
| 118 | + not np.allclose(orig_poly, trans_poly) |
| 119 | + for orig_poly, trans_poly in zip(sample_polys, transformed_data["polys"]) |
| 120 | + ) |
| 121 | + assert polys_changed, "Polygons should have been transformed" |
| 122 | + |
| 123 | + |
| 124 | +# Test unknown transform type raises NotImplementedError |
| 125 | +def test_iaa_augment_unknown_transform(): |
| 126 | + augmenter_args = [{"type": "UnknownTransform", "args": {}}] |
| 127 | + with pytest.raises(NotImplementedError): |
| 128 | + IaaAugment(augmenter_args=augmenter_args) |
| 129 | + |
| 130 | + |
| 131 | +# Test invalid size parameter raises ValueError |
| 132 | +def test_iaa_augment_invalid_resize_size(): |
| 133 | + augmenter_args = [{"type": "Resize", "args": {"size": "invalid_size"}}] |
| 134 | + with pytest.raises(ValueError): |
| 135 | + IaaAugment(augmenter_args=augmenter_args) |
| 136 | + |
| 137 | + |
| 138 | +# Test that polys are transformed appropriately |
| 139 | +def test_iaa_augment_polys_transformation(sample_image, sample_polys): |
| 140 | + data = create_data(sample_image, sample_polys) |
| 141 | + augmenter_args = [ |
| 142 | + {"type": "Affine", "args": {"rotate": [90, 90]}}, # Fixed rotation angle |
| 143 | + ] |
| 144 | + augmenter = IaaAugment(augmenter_args=augmenter_args) |
| 145 | + transformed_data = augmenter(data) |
| 146 | + |
| 147 | + # Check that the polygons have changed |
| 148 | + polys_changed = any( |
| 149 | + not np.allclose(orig_poly, trans_poly) |
| 150 | + for orig_poly, trans_poly in zip(sample_polys, transformed_data["polys"]) |
| 151 | + ) |
| 152 | + assert polys_changed, "Polygons should have been transformed" |
| 153 | + |
| 154 | + |
| 155 | +# Test with multiple transforms in augmenter_args |
| 156 | +def test_iaa_augment_multiple_transforms(sample_image, sample_polys): |
| 157 | + augmenter_args = [ |
| 158 | + {"type": "Fliplr", "args": {"p": 1.0}}, # Always flip |
| 159 | + {"type": "Affine", "args": {"shear": 10}}, |
| 160 | + ] |
| 161 | + data = create_data(sample_image, sample_polys) |
| 162 | + augmenter = IaaAugment(augmenter_args=augmenter_args) |
| 163 | + transformed_data = augmenter(data) |
| 164 | + |
| 165 | + # Check that the image has been transformed |
| 166 | + images_different = not np.array_equal(transformed_data["image"], sample_image) |
| 167 | + assert images_different, "Image should be transformed" |
| 168 | + |
| 169 | + # Check that the polys have been transformed |
| 170 | + polys_changed = any( |
| 171 | + not np.allclose(orig_poly, trans_poly) |
| 172 | + for orig_poly, trans_poly in zip(sample_polys, transformed_data["polys"]) |
| 173 | + ) |
| 174 | + assert polys_changed, "Polygons should have been transformed" |
0 commit comments