-
Notifications
You must be signed in to change notification settings - Fork 562
/
prepare_cityscapes_to_voc.py
160 lines (123 loc) · 5.87 KB
/
prepare_cityscapes_to_voc.py
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
from pascal_voc_writer import Writer
import matplotlib.pyplot as plt
import numpy as np
import os
import json
import glob
import time
from shutil import move, copy
import tqdm
classes = {'bicycle': 'bicycle', 'bus': 'bus', 'car': 'car', 'motorcycle': 'motorcycle',
'person': 'person', 'rider': 'rider', 'train': 'train', 'truck': 'truck'}
classes_keys = list(classes.keys())
def make_dir(path):
if not os.path.isdir(path):
os.makedirs(path)
#----------------------------------------------------------------------------------------------------------------
#convert polygon to bounding box
#code from:
#https://stackoverflow.com/questions/46335488/how-to-efficiently-find-the-bounding-box-of-a-collection-of-points
#----------------------------------------------------------------------------------------------------------------
def polygon_to_bbox(polygon):
x_coordinates, y_coordinates = zip(*polygon)
return [min(x_coordinates), min(y_coordinates), max(x_coordinates), max(y_coordinates)]
# --------------------------------------------
# read a json file and convert to voc format
# --------------------------------------------
def read_json(file):
# if no relevant objects found in the image,
# don't save the xml for the image
relevant_file = False
data = []
with open(file, 'r') as f:
file_data = json.load(f)
for object in file_data['objects']:
label, polygon = object['label'], object['polygon']
# process only if label found in voc
if label in classes_keys:
polygon = np.array([x for x in polygon])
bbox = polygon_to_bbox(polygon)
data.append([classes[label]] + bbox)
# if relevant objects found in image, set the flag to True
if data:
relevant_file = True
return data, relevant_file
#---------------------------
#function to save xml file
#---------------------------
def save_xml(img_path, img_shape, data, save_path):
writer = Writer(img_path,img_shape[1], img_shape[0])
for element in data:
writer.addObject(element[0],element[1],element[2],element[3],element[4])
writer.save(save_path)
def prepare_cityscapes_to_voc(cityscapes_dir, save_path, suffix, image_dir):
cityscapes_dir_gt = os.path.join(cityscapes_dir, 'gtFine')
# ------------------------------------------
# reading json files from each subdirectory
# ------------------------------------------
valid_files = []
trainval_files = []
test_files = []
# make Annotations target directory if already doesn't exist
ann_dir = os.path.join(save_path, 'Annotations')
make_dir(ann_dir)
start = time.time()
for category in os.listdir(cityscapes_dir_gt):
# # no GT for test data
# if category == 'test':
# continue
for city in tqdm.tqdm(os.listdir(os.path.join(cityscapes_dir_gt, category))):
# read files
files = glob.glob(os.path.join(cityscapes_dir, 'gtFine', category, city) + '/*.json')
# process json files
for file in files:
data, relevant_file = read_json(file)
if relevant_file:
base_filename = os.path.basename(file)[:-21]
xml_filepath = os.path.join(ann_dir, base_filename + '{}.xml'.format(suffix))
img_name = base_filename + '{}.png'.format(suffix)
img_path = os.path.join(cityscapes_dir, image_dir, category, city,
base_filename + '{}.png'.format(suffix))
img_shape = plt.imread(img_path).shape
valid_files.append([img_path, img_name])
# make list of trainval and test files for voc format
# lists will be stored in txt files
trainval_files.append(img_name[:-4]) if category == 'train' else test_files.append(img_name[:-4])
# save xml file
save_xml(os.path.join(image_dir, category, city,
base_filename + '{}.png'.format(suffix)), img_shape, data, xml_filepath)
end = time.time() - start
print('Total Time taken: ', end)
# ----------------------------
# copy files into target path
# ----------------------------
images_savepath = os.path.join(save_path, 'JPEGImages')
make_dir(images_savepath)
start = time.time()
for file in valid_files:
copy(file[0], os.path.join(images_savepath, file[1]))
print('Total Time taken: ', end)
# ---------------------------------------------
# create text files of trainval and test files
# ---------------------------------------------
textfiles_savepath = os.path.join(save_path, 'ImageSets', 'Main')
make_dir(textfiles_savepath)
traival_files_wr = [x + '\n' for x in trainval_files]
test_files_wr = [x + '\n' for x in test_files]
with open(os.path.join(textfiles_savepath, 'trainval.txt'), 'w') as f:
f.writelines(traival_files_wr)
with open(os.path.join(textfiles_savepath, 'test.txt'), 'w') as f:
f.writelines(test_files_wr)
if __name__ == '__main__':
cityscapes_dir = 'datasets/cityscapes/'
if not os.path.exists(cityscapes_dir):
print("Please put cityscapes datasets in: {}".format(cityscapes_dir))
exit(0)
save_path = 'datasets/cityscapes_in_voc/'
suffix = "_leftImg8bit"
image_dir = "leftImg8bit"
prepare_cityscapes_to_voc(cityscapes_dir, save_path, suffix, image_dir)
save_path = 'datasets/foggy_cityscapes_in_voc/'
suffix = "_leftImg8bit_foggy_beta_0.02"
image_dir = "leftImg8bit_foggy"
prepare_cityscapes_to_voc(cityscapes_dir, save_path, suffix, image_dir)