File size: 4,333 Bytes
0b4516f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
# Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp
import tempfile
from unittest import TestCase

import mmengine

from mmocr.datasets.icdar_dataset import IcdarDataset


class TestIcdarDataset(TestCase):

    def _create_dummy_icdar_json(self, json_name):
        image_1 = {
            'id': 0,
            'width': 640,
            'height': 640,
            'file_name': 'fake_name.jpg',
        }
        image_2 = {
            'id': 1,
            'width': 640,
            'height': 640,
            'file_name': 'fake_name1.jpg',
        }

        annotation_1 = {
            'id': 1,
            'image_id': 0,
            'category_id': 0,
            'area': 400,
            'bbox': [50, 60, 20, 20],
            'iscrowd': 0,
            'segmentation': [[50, 60, 70, 60, 70, 80, 50, 80]]
        }

        annotation_2 = {
            'id': 2,
            'image_id': 0,
            'category_id': 0,
            'area': 900,
            'bbox': [100, 120, 30, 30],
            'iscrowd': 0,
            'segmentation': [[100, 120, 130, 120, 120, 150, 100, 150]]
        }

        annotation_3 = {
            'id': 3,
            'image_id': 0,
            'category_id': 0,
            'area': 1600,
            'bbox': [150, 160, 40, 40],
            'iscrowd': 1,
            'segmentation': [[150, 160, 190, 160, 190, 200, 150, 200]]
        }

        annotation_4 = {
            'id': 4,
            'image_id': 0,
            'category_id': 0,
            'area': 10000,
            'bbox': [250, 260, 100, 100],
            'iscrowd': 1,
            'segmentation': [[250, 260, 350, 260, 350, 360, 250, 360]]
        }
        annotation_5 = {
            'id': 5,
            'image_id': 1,
            'category_id': 0,
            'area': 10000,
            'bbox': [250, 260, 100, 100],
            'iscrowd': 1,
            'segmentation': [[250, 260, 350, 260, 350, 360, 250, 360]]
        }
        annotation_6 = {
            'id': 6,
            'image_id': 1,
            'category_id': 0,
            'area': 0,
            'bbox': [0, 0, 0, 0],
            'iscrowd': 1,
            'segmentation': [[250, 260, 350, 260, 350, 360, 250, 360]]
        }
        annotation_7 = {
            'id': 7,
            'image_id': 1,
            'category_id': 2,
            'area': 10000,
            'bbox': [250, 260, 100, 100],
            'iscrowd': 1,
            'segmentation': [[250, 260, 350, 260, 350, 360, 250, 360]]
        }
        annotation_8 = {
            'id': 8,
            'image_id': 1,
            'category_id': 0,
            'area': 10000,
            'bbox': [250, 260, 100, 100],
            'iscrowd': 1,
            'segmentation': [[250, 260, 350, 260, 350, 360, 250, 360]]
        }

        categories = [{
            'id': 0,
            'name': 'text',
            'supercategory': 'text',
        }]

        fake_json = {
            'images': [image_1, image_2],
            'annotations': [
                annotation_1, annotation_2, annotation_3, annotation_4,
                annotation_5, annotation_6, annotation_7, annotation_8
            ],
            'categories':
            categories
        }
        self.metainfo = dict(classes=('text'))
        mmengine.dump(fake_json, json_name)

    def test_icdar_dataset(self):
        tmp_dir = tempfile.TemporaryDirectory()
        # create dummy data
        fake_json_file = osp.join(tmp_dir.name, 'fake_data.json')
        self._create_dummy_icdar_json(fake_json_file)

        # test initialization
        dataset = IcdarDataset(
            ann_file=fake_json_file,
            data_prefix=dict(img_path='imgs'),
            metainfo=self.metainfo,
            pipeline=[])
        self.assertEqual(dataset.metainfo['classes'], self.metainfo['classes'])
        dataset.full_init()
        self.assertEqual(len(dataset), 2)
        self.assertEqual(len(dataset.load_data_list()), 2)

        # test load_data_list
        anno = dataset.load_data_list()[0]
        self.assertEqual(len(anno['instances']), 4)
        self.assertTrue('ignore' in anno['instances'][0])
        self.assertTrue('bbox' in anno['instances'][0])
        self.assertEqual(anno['instances'][0]['bbox_label'], 0)
        self.assertTrue('polygon' in anno['instances'][0])
        tmp_dir.cleanup()