Spaces:
Sleeping
Sleeping
File size: 4,835 Bytes
f82a827 | 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 | '''
Rename the Open Images V6 images to adapt pycocotools and produce the corresponding annotations.
The Visual Genome annotations are produced in the same way.
'''
import json
import os
root_path = 'your_path/open-imagev6/' # Download images from Rongjie's repo and unzip it.
with open(root_path +'annotations/'+ 'categories_dict.json') as f:
categories = json.load(f)
obj_categories = categories['obj']
rel_categories = categories['rel']
categories = []
for idx, i in enumerate(obj_categories):
category = {'supercategory': i, 'id': idx, 'name': i}
categories.append(category)
with open(root_path+'annotations/'+'vrd-train-anno.json') as f:
train_image_list = json.load(f)
counter = 0
new_image_name = 1
images = []
annotations = []
train_rel = {}
#
for row in train_image_list:
image_path = root_path+'images/'+row['img_fn']+'.jpg' #TODO image_id
w, h = row['img_size']
os.rename(image_path, root_path+'images/'+str(new_image_name)+'.jpg')
image = {'file_name': str(new_image_name)+'.jpg',
'height': h,
'width': w,
'id': new_image_name}
images.append(image)
for index, j in enumerate(row['bbox']):
bbox = [j[0], j[1], j[2]-j[0], j[3]-j[1]] #cxcywh
area = int(bbox[2] * bbox[3])
anno_id = counter
counter = counter + 1
annotation = {'segmentation': None,
'area': area,
'bbox': bbox,
'iscrowd': 0,
'image_id': new_image_name,
'id': anno_id,
'category_id': row['det_labels'][index]}
annotations.append(annotation)
train_rel[new_image_name] = row['rel']
new_image_name = new_image_name + 1
train_database = {'images': images,
'annotations': annotations,
'categories': categories}
print('train finish')
with open(root_path+'annotations/'+'vrd-test-anno.json') as f:
test_image_list = json.load(f)
images = []
annotations = []
test_rel = {}
for row in test_image_list:
image_path = root_path+'images/'+row['img_fn']+'.jpg' #TODO image_id
w, h = row['img_size']
os.rename(image_path, root_path+'images/'+str(new_image_name)+'.jpg')
image = {'file_name': str(new_image_name)+'.jpg',
'height': h,
'width': w,
'id': new_image_name}
images.append(image)
for index, j in enumerate(row['bbox']):
bbox = [j[0], j[1], j[2]-j[0], j[3]-j[1]] #cxcywh
area = int(bbox[2] * bbox[3])
anno_id = counter
counter = counter + 1
annotation = {'segmentation': None,
'area': area,
'bbox': bbox,
'iscrowd': 0,
'image_id': new_image_name,
'id': anno_id,
'category_id': row['det_labels'][index]}
annotations.append(annotation)
test_rel[new_image_name] = row['rel']
new_image_name = new_image_name + 1
test_database = {'images': images,
'annotations': annotations,
'categories': categories}
print('test finish')
with open(root_path+'annotations/'+'vrd-val-anno.json') as f:
val_image_list = json.load(f)
images = []
annotations = []
val_rel = {}
for row in val_image_list:
image_path = root_path+'images/'+row['img_fn']+'.jpg' #TODO image_id
w, h = row['img_size']
os.rename(image_path, root_path+'images/'+str(new_image_name)+'.jpg')
image = {'file_name': str(new_image_name)+'.jpg',
'height': h,
'width': w,
'id': new_image_name}
images.append(image)
for index, j in enumerate(row['bbox']):
bbox = [j[0], j[1], j[2]-j[0], j[3]-j[1]] #cxcywh
area = int(bbox[2] * bbox[3])
anno_id = counter
counter = counter + 1
annotation = {'segmentation': None,
'area': area,
'bbox': bbox,
'iscrowd': 0,
'image_id': new_image_name,
'id': anno_id,
'category_id': row['det_labels'][index]}
annotations.append(annotation)
val_rel[new_image_name] = row['rel']
new_image_name = new_image_name + 1
val_database = {'images': images,
'annotations': annotations,
'categories': categories}
print('test finish')
rel_database = {'train': train_rel,
'val': val_rel,
'test': test_rel,
'rel_categories': rel_categories}
json.dump(train_database, open('train.json', 'w'))
json.dump(val_database, open('val.json', 'w'))
json.dump(test_database, open('test.json', 'w'))
json.dump(rel_database, open('rel.json', 'w'))
|