|
| 1 | +# Copyright (c) OpenMMLab. All rights reserved. |
| 2 | +import os.path as osp |
| 3 | +import tempfile |
| 4 | +import unittest |
| 5 | + |
| 6 | +from mmocr.datasets.preparers.parsers import CTW1500AnnParser |
| 7 | +from mmocr.utils import list_to_file |
| 8 | + |
| 9 | + |
| 10 | +class TestCTW1500AnnParser(unittest.TestCase): |
| 11 | + |
| 12 | + def setUp(self) -> None: |
| 13 | + self.root = tempfile.TemporaryDirectory() |
| 14 | + |
| 15 | + def _create_dummy_ctw1500_det(self): |
| 16 | + fake_train_anno = [ |
| 17 | + '<Annotations>', |
| 18 | + ' <image file="0200.jpg">', |
| 19 | + ' <box height="197" left="131" top="49" width="399">', |
| 20 | + ' <label>OLATHE</label>', |
| 21 | + ' <segs>131,58,208,49,279,56,346,76,412,101,473,141,530,192,510,246,458,210,405,175,350,151,291,137,228,133,165,134</segs>', # noqa: E501 |
| 22 | + ' <pts x="183" y="95" />', |
| 23 | + ' <pts x="251" y="89" />', |
| 24 | + ' <pts x="322" y="107" />', |
| 25 | + ' <pts x="383" y="124" />', |
| 26 | + ' <pts x="441" y="161" />', |
| 27 | + ' <pts x="493" y="201" />', |
| 28 | + ' </box>', |
| 29 | + ' </image>', |
| 30 | + '</Annotations>', |
| 31 | + ] |
| 32 | + train_ann_file = osp.join(self.root.name, 'ctw1500_train.xml') |
| 33 | + list_to_file(train_ann_file, fake_train_anno) |
| 34 | + |
| 35 | + fake_test_anno = [ |
| 36 | + '48,84,61,79,75,73,88,68,102,74,116,79,130,84,135,73,119,67,104,60,89,56,74,61,59,67,45,73,#######', # noqa: E501 |
| 37 | + '51,137,58,137,66,137,74,137,82,137,90,137,98,137,98,119,90,119,82,119,74,119,66,119,58,119,50,119,####E-313', # noqa: E501 |
| 38 | + '41,155,49,155,57,155,65,155,73,155,81,155,89,155,87,136,79,136,71,136,64,136,56,136,48,136,41,137,#######', # noqa: E501 |
| 39 | + '41,193,57,193,74,194,90,194,107,195,123,195,140,196,146,168,128,167,110,167,92,167,74,166,56,166,39,166,####F.D.N.Y.', # noqa: E501 |
| 40 | + ] |
| 41 | + test_ann_file = osp.join(self.root.name, 'ctw1500_test.txt') |
| 42 | + list_to_file(test_ann_file, fake_test_anno) |
| 43 | + return (osp.join(self.root.name, |
| 44 | + 'ctw1500.jpg'), train_ann_file, test_ann_file) |
| 45 | + |
| 46 | + def test_textdet_parsers(self): |
| 47 | + parser = CTW1500AnnParser(split='train') |
| 48 | + img_path, train_file, test_file = self._create_dummy_ctw1500_det() |
| 49 | + img_path, instances = parser.parse_file(img_path, train_file) |
| 50 | + self.assertEqual(img_path, osp.join(self.root.name, 'ctw1500.jpg')) |
| 51 | + self.assertEqual(len(instances), 1) |
| 52 | + self.assertEqual(instances[0]['text'], 'OLATHE') |
| 53 | + self.assertEqual(instances[0]['poly'], [ |
| 54 | + 131, 58, 208, 49, 279, 56, 346, 76, 412, 101, 473, 141, 530, 192, |
| 55 | + 510, 246, 458, 210, 405, 175, 350, 151, 291, 137, 228, 133, 165, |
| 56 | + 134 |
| 57 | + ]) |
| 58 | + self.assertEqual(instances[0]['ignore'], False) |
| 59 | + |
| 60 | + parser = CTW1500AnnParser(split='test') |
| 61 | + img_path, instances = parser.parse_file(img_path, test_file) |
| 62 | + self.assertEqual(img_path, osp.join(self.root.name, 'ctw1500.jpg')) |
| 63 | + self.assertEqual(len(instances), 4) |
| 64 | + self.assertEqual(instances[0]['ignore'], True) |
| 65 | + self.assertEqual(instances[1]['text'], 'E-313') |
| 66 | + self.assertEqual(instances[3]['poly'], [ |
| 67 | + 41, 193, 57, 193, 74, 194, 90, 194, 107, 195, 123, 195, 140, 196, |
| 68 | + 146, 168, 128, 167, 110, 167, 92, 167, 74, 166, 56, 166, 39, 166 |
| 69 | + ]) |
| 70 | + |
| 71 | + def tearDown(self) -> None: |
| 72 | + self.root.cleanup() |
0 commit comments