-
Notifications
You must be signed in to change notification settings - Fork 0
/
dd2coco_det_label_convert.py
134 lines (119 loc) · 4.41 KB
/
dd2coco_det_label_convert.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
import os
import copy
import xml.etree.ElementTree as ET
import json
import argparse
from tqdm import tqdm
import imageio
import glob
from PIL import Image
import multiprocessing
def parse_arguments():
parser = argparse.ArgumentParser(description='Convert d2city video to image')
parser.add_argument(
"--dir",
default="../",
)
return parser.parse_args()
def convert(i):
print(i)
vid = imageio.read(i)
for num, img in enumerate(vid.iter_data()):
save_dir = i.split(".mp4")[0]
save_dir = save_dir+ "_{}.jpg".format(num)
img = Image.fromarray(img)
img.save(save_dir)
def dd2coco(images, xml_file):
id2images = copy.deepcopy(images)
id2images = [''].extend(id2images)
image_num = len(images)
image2id = {images[i]: i+1 for i in range(image_num)}
images = list()
empty_images = []
annotations = list()
annotation_id = 1
for xml_name in tqdm(xml_file):
frame_name = xml_name.split(args.dir)[-1].split(".xml")[0]
xml = ET.parse(xml_name).getroot()
meta = xml[1]
invalid_label = set()
frame_num = int(meta[0].text)
width = int(meta[1].text)
height = int(meta[2].text)
for track_list in xml[2:]:
track_label = track_list.attrib['label']
if track_label not in label2id.keys():
invalid_label.add(track_label)
continue
track_label_id = label2id[track_label]
for track in track_list:
annotation = dict()
attrib = track.attrib
frame = attrib['frame']
x1 = int(float(attrib['xtl']))
y1 = int(float(attrib['ytl']))
x2 = int(float(attrib['xbr']))
y2 = int(float(attrib['ybr']))
annotation['image_id'] = image2id[frame_name+"_{}.jpg".format(frame)]
annotation['area'] = float((x2-x1) * (y2-y1))
annotation['iscrowd'] = 0
annotation['bbox'] = [x1, y1, x2-x1, y2-y1]
annotation['id'] = annotation_id
annotation_id+=1
annotation['ignore'] = 0
annotation['segmentation'] = [[x1, y1, x1, y2, x2, y2, x2, y1]]
annotation['category_id'] = track_label_id
annotations.append(annotation)
for frame_id in range(frame_num):
image = dict()
image['file_name'] = frame_name+'_{}.jpg'.format(frame_id)
image['height'] = height
image['width'] = width
image['id'] = image2id[image['file_name']]
images.append(image)
attr_dict = {}
attr_dict["categories"] = [
{"supercategory": "none", "id": 1, "name": "car"},
{"supercategory": "none", "id": 2, "name": "bus"},
{"supercategory": "none", "id": 3, "name": "truck"},
{"supercategory": "none", "id": 4, "name": "person rider"},
{"supercategory": "none", "id": 5, "name": "bike"},
{"supercategory": "none", "id": 6, "name": "motor"},
]
attr_dict['images'] = images
attr_dict['annotations'] = annotations
attr_dict['type'] = "instances"
return attr_dict
if __name__ == '__main__':
args = parse_arguments()
train_image_id = []
train_xmls = []
label2id = {}
label2id['car'] = 1
label2id['van'] = 1
label2id['bus'] = 2
label2id['truck'] = 3
label2id['person'] = 4
label2id['bicycle'] = 5
label2id['motorcycle'] = 6
label2id['open-tricycle'] = 6
label2id['closed-tricycle'] = 6
for i in range(8):
train_image_id.extend(glob.glob(args.dir + "000{}/*.jpg".format(i)))
train_xmls.extend(glob.glob(args.dir + "000{}/*.xml".format(i)))
print(train_image_id[:10])
train_image_id = [i.split(args.dir)[1] for i in train_image_id]
print(train_image_id[:20])
attr_dict = dd2coco(train_image_id, train_xmls)
json_string = json.dumps(attr_dict)
with open("train_label_convert.json", 'w') as f:
f.write(json_string)
val_image_id = glob.glob(args.dir + "0008/*.jpg")
val_image_id = [i.split(args.dir)[1] for i in val_image_id]
val_image_id.sort()
val_xmls = glob.glob(args.dir + "0008/*.xml")
print(len(val_xmls), len(val_image_id))
attr_dict = dd2coco(val_image_id, val_xmls)
json_string = json.dumps(attr_dict)
with open("val_label_convert.json", 'w') as f:
f.write(json_string)