Datasets:

Modalities:
Text
Formats:
text
Languages:
English
ArXiv:
Libraries:
Datasets
File size: 7,591 Bytes
a69d681
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
from pathlib import Path
import fire
from PIL import Image
from sahi.utils.coco import Coco, CocoAnnotation, CocoCategory, CocoImage
from sahi.utils.file import save_json
from tqdm import tqdm
import xml.etree.ElementTree as ET
import sys;


NAME_TO_COCO_CATEGORY = {
    "stadium": {"name": "stadium", "supercategory": "stadium"},
    "Expressway-toll-station": {"name": "Expressway-toll-station", "supercategory": "Expressway-toll-station"},
    "bridge": {"name": "bridge", "supercategory": "bridge"},
    "groundtrackfield": {"name": "groundtrackfield", "supercategory": "groundtrackfield"},
    "Expressway-Service-area": {"name": "groundtrackfield", "supercategory": "groundtrackfield"},
    "dam": {"name": "dam", "supercategory": "dam"},
    "ship": {"name": "ship", "supercategory": "ship"},
    "basketballcourt": {"name": "basketballcourt", "supercategory": "basketballcourt"},
    "vehicle": {"name": "vehicle", "supercategory": "vehicle"},
    "golffield": {"name": "golffield", "supercategory": "golffield"},
    "airplane": {"name": "airplane", "supercategory": "airplane"},
    "baseballfield": {"name": "baseballfield", "supercategory": "baseballfield"},
    "airport": {"name": "airport", "supercategory": "airport"},
    "harbor": {"name": "harbor", "supercategory": "harbor"},
    "chimney": {"name": "chimney", "supercategory": "chimney"},
    "tenniscourt": {"name": "tenniscourt", "supercategory": "tenniscourt"},
    "trainstation": {"name": "trainstation", "supercategory": "trainstation"},
    "overpass": {"name": "overpass", "supercategory": "overpass"},
    "storagetank": {"name": "storagetank", "supercategory": "storagetank"},
    "windmill": {"name": "windmill", "supercategory": "windmill"}
}

CATEGORY_ID_REMAPPING = {
    "stadium": "0", 
    "Expressway-toll-station": "1", 
    "bridge": "2", 
    "groundtrackfield": "3", 
    "Expressway-Service-area": "4", 
    "dam": "5",
    "ship": "6", 
    "basketballcourt": "7",
    "vehicle": "8", 
    "golffield": "9", 
    "airplane": "10", 
    "baseballfield": "11", 
    "airport":"12", 
    "harbor":"13", 
    "chimney":"14", 
    "tenniscourt":"15", 
    "trainstation":"16", 
    "overpass":"17", 
    "storagetank":"18", 
    "windmill":"19"
}


def dior_to_coco(
    data_folder_dir,
    split_images_path,
    output_file_path,
    mode,
    category_id_remapping=None,
):
    """
    Converts DIOR annotations into coco annotation.

    Args:
        data_folder_dir: str
            'dior' folder directory
        split_images_path: str
            split_images_path txt file
        output_file_path: str
            Output file path
        mode: str
            mode original|car_other|car
        category_id_remapping: dict
            Used for selecting desired category ids and mapping them.
            If not provided, vedai mapping will be used.
            format: str(id) to str(id)
    """

    # init paths/folders
    input_image_folder = str(Path(data_folder_dir) / "JPEGImages-all")
    input_ann_folder = str(Path(data_folder_dir) / "Annotations/Horizontal Bounding Boxes")

    image_filepath_list = os.listdir(input_image_folder)
    
    if category_id_remapping is None:
        category_id_remapping = CATEGORY_ID_REMAPPING
    
    # init coco object
    coco = Coco()
    
    if mode == 'original':
        # append categories
        categories = ["stadium", "Expressway-toll-station", "bridge", "groundtrackfield", "Expressway-Service-area", "dam",
                      "ship", "basketballcourt", "vehicle", "golffield", "airplane", "baseballfield", "airport",
                      "harbor", "chimney", "tenniscourt", "trainstation", "overpass", "storagetank", "windmill"]
        for i, cat in enumerate(categories):
            coco.add_category(CocoCategory(id=i, name=cat))
    
    elif mode == 'car_other':
        print('"car_other" mode categories')
        coco.add_category(CocoCategory(id=0, name='Other'))
        coco.add_category(CocoCategory(id=1, name='Car'))
        for k,v in category_id_remapping.items():
            if k in 'vehicle':
                category_id_remapping[k] =1
            else:
                category_id_remapping[k]=0
    elif mode == 'car':
        # car related categories: car, pickup, van
        print('"car" mode categories')
        coco.add_category(CocoCategory(id=0, name='Car'))
        category_id_remapping = {'vehicle': '0'}
    else:
        print('pick a defined mode: [original/car_other/car]')
        sys.exit()
    
    
    split_images_lst = []
    with open(split_images_path, 'r') as f:
        for line in f:
            split_images_lst.append(line.strip().split('/')[1])

    # convert dior annotations to coco
    for image_filename in tqdm(image_filepath_list):
        if image_filename in split_images_lst:
            # get image properties
            image_filepath = str(Path(input_image_folder) / image_filename)
            annotation_filename = image_filename.split(".jpg")[0] + ".xml"
            annotation_filepath = str(Path(input_ann_folder) / annotation_filename)
            image = Image.open(image_filepath)
            cocoimage_filename = str(Path(image_filepath)).split(str(Path(data_folder_dir)))[1]
            if cocoimage_filename[0] == os.sep:
                cocoimage_filename = cocoimage_filename[1:]
            # create coco image object
            coco_image = CocoImage(file_name=cocoimage_filename, height=image.size[1], width=image.size[0])
            # read xml annotation file
            tree = ET.parse(annotation_filepath)
            xml_root = tree.getroot()

            # get all object tags in the xml file
            objects = xml_root.findall('object')

            for obj in objects:
                cat_name = obj[0].text
                bbox_tag = obj[2]
                xmin = int(obj[2][0].text)
                ymin = int(obj[2][1].text)
                xmax = int(obj[2][2].text)
                ymax = int(obj[2][3].text)
                #
                # width = maxx-minx, height = maxy-miny
                width = xmax - xmin
                height = ymax - ymin

                bbox = [xmin,
                        ymin,
                        width,
                        height]

                # get category id and name
                category_name = cat_name
                if category_name in category_id_remapping.keys():
                        remapped_category_id = category_id_remapping[category_name]
                else:
                    continue

                # create coco annotation and append it to coco image
                coco_annotation = CocoAnnotation.from_coco_bbox(
                    bbox=bbox,
                    category_id=int(remapped_category_id),
                    category_name=category_name,
                )
                if mode == 'original':
                    if coco_annotation.area > 0:
                        coco_image.add_annotation(coco_annotation)
                elif mode == 'car_other':
                    if 0 < coco_annotation.area < 400:
                        coco_image.add_annotation(coco_annotation)
                elif mode == 'car':
                    if 0 < coco_annotation.area < 400:
                        coco_image.add_annotation(coco_annotation)
            coco.add_image(coco_image)

    save_path = output_file_path
    all_json_path = Path(save_path) / "{}.json".format(split_images_path.split('.')[0])
    save_json(data=coco.json, save_path=all_json_path)


if __name__ == "__main__":
    fire.Fire(dior_to_coco)