|
| 1 | +import numpy as np |
| 2 | +import json, copy, sys, os |
| 3 | + |
| 4 | + |
| 5 | +if __name__ == "__main__": |
| 6 | + assert(len(sys.argv) == 2), \ |
| 7 | + 'Please specify coco annotation directory.' |
| 8 | + coco_annotation_dir = sys.argv[1] |
| 9 | + |
| 10 | + this_dir = os.path.dirname(__file__) |
| 11 | + dir_name = os.path.join(this_dir, 'data') |
| 12 | + vcoco_annotation_dir = dir_name |
| 13 | + |
| 14 | + print("%s, %s"%(coco_annotation_dir, vcoco_annotation_dir)) |
| 15 | + |
| 16 | + # First merge annotations from train and val |
| 17 | + # Load the train and val annotations |
| 18 | + json_train_file = '{:s}/instances_{:s}.json'.format(coco_annotation_dir, 'train2014') |
| 19 | + print("Loading training annotations from %s"%(format(json_train_file))) |
| 20 | + json_train = json.load(open(json_train_file, 'r')) |
| 21 | + |
| 22 | + json_val_file = '{:s}/instances_{:s}.json'.format(coco_annotation_dir, 'val2014') |
| 23 | + print("Loading validating annotations from %s"%(format(json_val_file))) |
| 24 | + json_val = json.load(open(json_val_file, 'r')) |
| 25 | + |
| 26 | + # Copy and sanity check |
| 27 | + assert(json_train['info'] == json_val['info']) |
| 28 | + assert(json_train['licenses'] == json_val['licenses']) |
| 29 | + assert(json_train['categories'] == json_val['categories']) |
| 30 | + |
| 31 | + json_all = json_train |
| 32 | + json_all['images'] = json_train['images'] + json_val['images']; |
| 33 | + json_all['annotations'] = json_train['annotations'] + json_val['annotations']; |
| 34 | + |
| 35 | + # write out collected trainval annotations to a single file |
| 36 | + json_trainval_file = '{:s}/instances_{:s}.json'.format(coco_annotation_dir, 'trainval2014') |
| 37 | + with open(json_trainval_file, 'w') as outfile: |
| 38 | + json.dump(json_all, outfile) |
| 39 | + del json_train |
| 40 | + del json_val |
| 41 | + del json_all |
| 42 | + |
| 43 | + # Second, selct annotations needed for V-COCO |
| 44 | + json_trainval = json.load(open('{:s}/instances_{:s}.json'.format(coco_annotation_dir, 'trainval2014'), 'r')) |
| 45 | + |
| 46 | + vcoco_imlist = np.loadtxt(os.path.join(vcoco_annotation_dir, 'splits', 'vcoco_all.ids'))[:,np.newaxis] |
| 47 | + |
| 48 | + # select images that we need |
| 49 | + coco_imlist = [j_i['id'] for j_i in json_trainval['images']] |
| 50 | + coco_imlist = np.array(coco_imlist)[:,np.newaxis] |
| 51 | + in_vcoco = [] |
| 52 | + for i in range(len(coco_imlist)): |
| 53 | + if np.any(coco_imlist[i] == vcoco_imlist): |
| 54 | + in_vcoco.append(i) |
| 55 | + j_images = [json_trainval['images'][ind] for ind in in_vcoco] |
| 56 | + |
| 57 | + # select annotations that we need |
| 58 | + coco_imlist = [j_i['image_id'] for j_i in json_trainval['annotations']] |
| 59 | + coco_imlist = np.array(coco_imlist)[:,np.newaxis] |
| 60 | + in_vcoco = [] |
| 61 | + for i in range(len(coco_imlist)): |
| 62 | + if np.any(coco_imlist[i] == vcoco_imlist): |
| 63 | + in_vcoco.append(i) |
| 64 | + j_annotations = [json_trainval['annotations'][ind] for ind in in_vcoco] |
| 65 | + |
| 66 | + json_trainval['annotations'] = j_annotations |
| 67 | + json_trainval['images'] = j_images |
| 68 | + |
| 69 | + vcoco = os.path.join(vcoco_annotation_dir, 'instances_vcoco_all_2014.json') |
| 70 | + print("Writing COCO annotations needed for V-COCO to %s."%(format(vcoco))) |
| 71 | + with open(vcoco, 'wt') as f: |
| 72 | + json.dump(json_trainval, f) |
0 commit comments