File size: 4,725 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

# Copyright (C) 2020 Intel Corporation
#
# SPDX-License-Identifier: MIT

import os
import os.path as osp
import re

from datumaro.components.converter import Converter
from datumaro.components.extractor import (AnnotationType, Bbox, DatasetItem,
    Importer, SourceExtractor)


class WiderFacePath:
    IMAGE_EXT = '.jpg'
    ANNOTATIONS_DIR = 'wider_face_split'
    IMAGES_DIR = 'images'
    SUBSET_DIR = 'WIDER_'
    BBOX_ATTRIBUTES = ['blur', 'expression', 'illumination',
        'occluded', 'pose', 'invalid']

class WiderFaceExtractor(SourceExtractor):
    def __init__(self, path):
        if not osp.isfile(path):
            raise Exception("Can't read annotation file '%s'" % path)
        self._path = path
        self._dataset_dir = osp.dirname(osp.dirname(path))

        subset = osp.splitext(osp.basename(path))[0]
        match = re.fullmatch(r'wider_face_\S+_bbx_gt', subset)
        if match:
            subset = subset.split('_')[2]
        super().__init__(subset=subset)

        self._items = list(self._load_items(path).values())

    def _load_items(self, path):
        items = {}
        with open(path, 'r') as f:
            lines = f.readlines()

        image_ids = [image_id for image_id, line in enumerate(lines)
            if WiderFacePath.IMAGE_EXT in line]

        for image_id in image_ids:
            image = lines[image_id]
            image_path = osp.join(self._dataset_dir, WiderFacePath.SUBSET_DIR
                + self._subset, WiderFacePath.IMAGES_DIR, image[:-1])
            item_id = image[:-(len(WiderFacePath.IMAGE_EXT) + 1)]

            bbox_count = lines[image_id + 1]
            bbox_lines = lines[image_id + 2 : image_id + int(bbox_count) + 2]
            annotations = []
            for bbox in bbox_lines:
                bbox_list = bbox.split()
                if len(bbox_list) >= 4:
                    attributes = {}
                    if len(bbox_list) == 10:
                        i = 4
                        for attr in WiderFacePath.BBOX_ATTRIBUTES:
                            if bbox_list[i] != '-':
                                attributes[attr] = int(bbox_list[i])
                            i += 1
                    annotations.append(Bbox(
                        int(bbox_list[0]), int(bbox_list[1]),
                        int(bbox_list[2]), int(bbox_list[3]),
                        attributes = attributes
                    ))

            items[item_id] = DatasetItem(id=item_id, subset=self._subset,
                image=image_path, annotations=annotations)
        return items

class WiderFaceImporter(Importer):
    @classmethod
    def find_sources(cls, path):
        return cls._find_sources_recursive(osp.join(path,
            WiderFacePath.ANNOTATIONS_DIR), '.txt', 'wider_face')

class WiderFaceConverter(Converter):
    DEFAULT_IMAGE_EXT = '.jpg'

    def apply(self):
        save_dir = self._save_dir

        os.makedirs(save_dir, exist_ok=True)

        for subset_name, subset in self._extractor.subsets().items():
            subset_dir = osp.join(save_dir, WiderFacePath.SUBSET_DIR + subset_name)

            wider_annotation = ''
            for item in subset:
                wider_annotation += '%s\n' % (item.id + WiderFacePath.IMAGE_EXT)
                if item.has_image and self._save_images:
                    self._save_image(item, osp.join(save_dir, subset_dir,
                        WiderFacePath.IMAGES_DIR, item.id + WiderFacePath.IMAGE_EXT))

                bboxes = [a for a in item.annotations
                    if a.type == AnnotationType.bbox]

                wider_annotation += '%s\n' % len(bboxes)
                for bbox in bboxes:
                    wider_bb = ' '.join('%d' % p for p in bbox.get_bbox())
                    wider_annotation += '%s ' % wider_bb
                    if bbox.attributes:
                        wider_attr = ''
                        attr_counter = 0
                        for attr in WiderFacePath.BBOX_ATTRIBUTES:
                            if attr in bbox.attributes:
                                wider_attr += '%s ' % bbox.attributes[attr]
                                attr_counter += 1
                            else:
                                wider_attr += '- '
                        if attr_counter > 0:
                            wider_annotation += wider_attr
                    wider_annotation  += '\n'
            annotation_path = osp.join(save_dir, WiderFacePath.ANNOTATIONS_DIR,
                'wider_face_' + subset_name + '_bbx_gt.txt')
            os.makedirs(osp.dirname(annotation_path), exist_ok=True)
            with open(annotation_path, 'w') as f:
                f.write(wider_annotation)