File size: 6,544 Bytes
d21cb06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
import os.path as osp
from unittest import TestCase

import numpy as np
from datumaro.components.extractor import Bbox, DatasetItem
from datumaro.components.project import Dataset, Project
from datumaro.plugins.widerface_format import WiderFaceConverter, WiderFaceImporter
from datumaro.util.test_utils import TestDir, compare_datasets


class WiderFaceFormatTest(TestCase):
    def test_can_save_and_load(self):
        source_dataset = Dataset.from_iterable([
            DatasetItem(id='1', subset='train', image=np.ones((8, 8, 3)),
                annotations=[
                    Bbox(0, 2, 4, 2),
                    Bbox(0, 1, 2, 3, attributes = {
                        'blur': 2, 'expression': 0, 'illumination': 0,
                        'occluded': 0, 'pose': 2, 'invalid': 0}),
                ]
            ),
            DatasetItem(id='2', subset='train', image=np.ones((10, 10, 3)),
                annotations=[
                    Bbox(0, 2, 4, 2, attributes = {
                        'blur': 2, 'expression': 0, 'illumination': 1,
                        'occluded': 0, 'pose': 1, 'invalid': 0}),
                    Bbox(3, 3, 2, 3, attributes = {
                        'blur': 0, 'expression': 1, 'illumination': 0,
                        'occluded': 0, 'pose': 2, 'invalid': 0}),
                    Bbox(2, 1, 2, 3, attributes = {
                        'blur': 2, 'expression': 0, 'illumination': 0,
                        'occluded': 0, 'pose': 0, 'invalid': 1}),
                ]
            ),

            DatasetItem(id='3', subset='val', image=np.ones((8, 8, 3)),
                annotations=[
                    Bbox(0, 1, 5, 2, attributes = {
                        'blur': 2, 'expression': 1, 'illumination': 0,
                        'occluded': 0, 'pose': 1, 'invalid': 0}),
                    Bbox(0, 2, 3, 2),
                    Bbox(0, 2, 4, 2),
                    Bbox(0, 7, 3, 2, attributes = {
                        'blur': 2, 'expression': 1, 'illumination': 0,
                        'occluded': 0, 'pose': 1, 'invalid': 0}),
                ]
            ),

            DatasetItem(id='4', subset='val', image=np.ones((8, 8, 3))),
        ])

        with TestDir() as test_dir:
            WiderFaceConverter.convert(source_dataset, test_dir, save_images=True)
            parsed_dataset = WiderFaceImporter()(test_dir).make_dataset()

            compare_datasets(self, source_dataset, parsed_dataset)

    def test_can_save_dataset_with_no_subsets(self):
        source_dataset = Dataset.from_iterable([
            DatasetItem(id='a/b/1', image=np.ones((8, 8, 3)),
                annotations=[
                    Bbox(0, 2, 4, 2),
                    Bbox(0, 1, 2, 3, attributes = {
                        'blur': 2, 'expression': 0, 'illumination': 0,
                        'occluded': 0, 'pose': 2, 'invalid': 0}),
                ]
            ),
        ])

        with TestDir() as test_dir:
            WiderFaceConverter.convert(source_dataset, test_dir, save_images=True)
            parsed_dataset = WiderFaceImporter()(test_dir).make_dataset()

            compare_datasets(self, source_dataset, parsed_dataset)

    def test_can_save_dataset_with_non_widerface_attributes(self):
        source_dataset = Dataset.from_iterable([
            DatasetItem(id='a/b/1', image=np.ones((8, 8, 3)),
                annotations=[
                    Bbox(0, 2, 4, 2),
                    Bbox(0, 1, 2, 3, attributes = {
                        'non-widerface attribute': 0,
                        'blur': 1, 'invalid': 1}),
                    Bbox(1, 1, 2, 2, attributes = {
                        'non-widerface attribute': 0}),
                ]
            ),
        ])

        target_dataset = Dataset.from_iterable([
            DatasetItem(id='a/b/1', image=np.ones((8, 8, 3)),
                annotations=[
                    Bbox(0, 2, 4, 2),
                    Bbox(0, 1, 2, 3, attributes = {
                        'blur': 1, 'invalid': 1}),
                    Bbox(1, 1, 2, 2),
                ]
            ),
        ])

        with TestDir() as test_dir:
            WiderFaceConverter.convert(source_dataset, test_dir, save_images=True)
            parsed_dataset = WiderFaceImporter()(test_dir).make_dataset()

            compare_datasets(self, target_dataset, parsed_dataset)

DUMMY_DATASET_DIR = osp.join(osp.dirname(__file__), 'assets', 'widerface_dataset')

class WiderFaceImporterTest(TestCase):
    def test_can_detect(self):
        self.assertTrue(WiderFaceImporter.detect(DUMMY_DATASET_DIR))

    def test_can_import(self):
        expected_dataset = Dataset.from_iterable([
            DatasetItem(id='0--Parade/0_Parade_image_01', subset='train',
                image=np.ones((10, 15, 3)),
                annotations=[
                    Bbox(1, 2, 2, 2, attributes = {
                        'blur': 0, 'expression': 0, 'illumination': 0,
                        'occluded': 0, 'pose': 0, 'invalid': 0}),
                ]
            ),
            DatasetItem(id='1--Handshaking/1_Handshaking_image_02', subset='train',
                image=np.ones((10, 15, 3)),
                annotations=[
                    Bbox(1, 1, 2, 2, attributes = {
                        'blur': 0, 'expression': 0, 'illumination': 1,
                        'occluded': 0, 'pose': 0, 'invalid': 0}),
                    Bbox(5, 1, 2, 2, attributes = {
                        'blur': 0, 'expression': 0, 'illumination': 1,
                        'occluded': 0, 'pose': 0, 'invalid': 0}),
                ]
            ),
            DatasetItem(id='0--Parade/0_Parade_image_03', subset='val',
                image=np.ones((10, 15, 3)),
                annotations=[
                    Bbox(0, 0, 1, 1, attributes = {
                        'blur': 2, 'expression': 0, 'illumination': 0,
                        'occluded': 0, 'pose': 2, 'invalid': 0}),
                    Bbox(3, 2, 1, 2, attributes = {
                        'blur': 0, 'expression': 0, 'illumination': 0,
                        'occluded': 1, 'pose': 0, 'invalid': 0}),
                    Bbox(5, 6, 1, 1, attributes = {
                        'blur': 2, 'expression': 0, 'illumination': 0,
                        'occluded': 0, 'pose': 2, 'invalid': 0}),
                ]
            ),
        ])

        dataset = Project.import_from(DUMMY_DATASET_DIR, 'wider_face') \
            .make_dataset()

        compare_datasets(self, expected_dataset, dataset)