Spaces:
Runtime error
Runtime error
File size: 3,819 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# Copyright (c) OpenMMLab. All rights reserved.
import math
import unittest.mock as mock
import numpy as np
import torch
import torchvision.transforms.functional as TF
from PIL import Image
import mmocr.datasets.pipelines.ocr_transforms as transforms
def test_resize_ocr():
input_img = np.ones((64, 256, 3), dtype=np.uint8)
rci = transforms.ResizeOCR(
32, min_width=32, max_width=160, keep_aspect_ratio=True)
results = {'img_shape': input_img.shape, 'img': input_img}
# test call
results = rci(results)
assert np.allclose([32, 160, 3], results['pad_shape'])
assert np.allclose([32, 160, 3], results['img'].shape)
assert 'valid_ratio' in results
assert math.isclose(results['valid_ratio'], 0.8)
assert math.isclose(np.sum(results['img'][:, 129:, :]), 0)
rci = transforms.ResizeOCR(
32, min_width=32, max_width=160, keep_aspect_ratio=False)
results = {'img_shape': input_img.shape, 'img': input_img}
results = rci(results)
assert math.isclose(results['valid_ratio'], 1)
def test_to_tensor():
input_img = np.ones((64, 256, 3), dtype=np.uint8)
expect_output = TF.to_tensor(input_img)
rci = transforms.ToTensorOCR()
results = {'img': input_img}
results = rci(results)
assert np.allclose(results['img'].numpy(), expect_output.numpy())
def test_normalize():
inputs = torch.zeros(3, 10, 10)
expect_output = torch.ones_like(inputs) * (-1)
rci = transforms.NormalizeOCR(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
results = {'img': inputs}
results = rci(results)
assert np.allclose(results['img'].numpy(), expect_output.numpy())
@mock.patch('%s.transforms.np.random.random' % __name__)
def test_online_crop(mock_random):
kwargs = dict(
box_keys=['x1', 'y1', 'x2', 'y2', 'x3', 'y3', 'x4', 'y4'],
jitter_prob=0.5,
max_jitter_ratio_x=0.05,
max_jitter_ratio_y=0.02)
mock_random.side_effect = [0.1, 1, 1, 1]
src_img = np.ones((100, 100, 3), dtype=np.uint8)
results = {
'img': src_img,
'img_info': {
'x1': '20',
'y1': '20',
'x2': '40',
'y2': '20',
'x3': '40',
'y3': '40',
'x4': '20',
'y4': '40'
}
}
rci = transforms.OnlineCropOCR(**kwargs)
results = rci(results)
assert np.allclose(results['img_shape'], [20, 20, 3])
# test not crop
mock_random.side_effect = [0.1, 1, 1, 1]
results['img_info'] = {}
results['img'] = src_img
results = rci(results)
assert np.allclose(results['img'].shape, [100, 100, 3])
def test_fancy_pca():
input_tensor = torch.rand(3, 32, 100)
rci = transforms.FancyPCA()
results = {'img': input_tensor}
results = rci(results)
assert results['img'].shape == torch.Size([3, 32, 100])
@mock.patch('%s.transforms.np.random.uniform' % __name__)
def test_random_padding(mock_random):
kwargs = dict(max_ratio=[0.0, 0.0, 0.0, 0.0], box_type=None)
mock_random.side_effect = [1, 1, 1, 1]
src_img = np.ones((32, 100, 3), dtype=np.uint8)
results = {'img': src_img, 'img_shape': (32, 100, 3)}
rci = transforms.RandomPaddingOCR(**kwargs)
results = rci(results)
print(results['img'].shape)
assert np.allclose(results['img_shape'], [96, 300, 3])
def test_opencv2pil():
src_img = np.ones((32, 100, 3), dtype=np.uint8)
results = {'img': src_img}
rci = transforms.OpencvToPil()
results = rci(results)
assert np.allclose(results['img'].size, (100, 32))
def test_pil2opencv():
src_img = Image.new('RGB', (100, 32), color=(255, 255, 255))
results = {'img': src_img}
rci = transforms.PilToOpencv()
results = rci(results)
assert np.allclose(results['img'].shape, (32, 100, 3))
|