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
| from pycocotools.coco import COCO from pycocotools import mask as cocomask import numpy as np import skimage.io as io import matplotlib.pyplot as plt import pylab import random import os from pycococreatortools import pycococreatortools as pt import json import random root_dir = '/root'
INFO = { "description": "Construction Site Image Dataset", "url": "", "version": "0.2.0", "year": 2020, "contributor": "", }
LICENSES = [ { "id": 1, "name": "Attribution-NonCommercial-ShareAlike License", "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/" } ]
CATEGORIES = [ { "id": 1, "name": "truck", "supercategory": "class" }, { "id": 2, "name": "excavator", "supercategory": "class" }, { "id": 3, "name": "crane", "supercategory": "class" }, { "id": 4, "name": "other_machine", "supercategory": "class" }, { "id": 5, "name": "precast_concrete", "supercategory": "class" }, { "id": 6, "name": "steel", "supercategory": "class" }, { "id": 7, "name": "aggregates", "supercategory": "class" }, { "id": 8, "name": "timber", "supercategory": "class" }, { "id": 9, "name": "other_materials", "supercategory": "class" }, { "id": 10, "name": "personnel", "supercategory": "class" }, ] coco_output_train = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } coco_output_val = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } coco_output_test = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } annotation_root_path = '/root' annotation_name = os.path.join( annotation_root_path, "via_export_coco_300_refine.json")
coco = COCO(annotation_name) catogry_info = coco.loadCats(coco.getCatIds( catNms=["truck", "excavator", "crane","other_machine","precast_concrete","steel","aggregates","timber","other_materials","personnel"])) coco_output_train['categories'] = catogry_info coco_output_val['categories'] = catogry_info coco_output_test['categories'] = catogry_info image_ids = coco.getImgIds()[0:300] random.shuffle(image_ids)
for image_id in image_ids[:200]: image_info = coco.loadImgs(image_id)[0] coco_output_train["images"].append(image_info) annotation_ids = [] for anno in coco.imgToAnns[str(image_id)]: annotation_ids.append(anno['id']) for annotation_id in annotation_ids: annotation_info = coco.loadAnns(annotation_id)[0] annotation_info['image_id'] = int(annotation_info['image_id']) coco_output_train["annotations"].append(annotation_info)
for image_id in image_ids[200:250]: image_info = coco.loadImgs(image_id)[0] coco_output_val["images"].append(image_info) annotation_ids = [] for anno in coco.imgToAnns[str(image_id)]: annotation_ids.append(anno['id']) for annotation_id in annotation_ids: annotation_info = coco.loadAnns(annotation_id)[0] annotation_info['image_id'] = int(annotation_info['image_id']) coco_output_val["annotations"].append(annotation_info)
for image_id in image_ids[250:]: image_info = coco.loadImgs(image_id)[0] coco_output_test["images"].append(image_info) annotation_ids = [] for anno in coco.imgToAnns[str(image_id)]: annotation_ids.append(anno['id']) for annotation_id in annotation_ids: annotation_info = coco.loadAnns(annotation_id)[0] annotation_info['image_id'] = int(annotation_info['image_id']) coco_output_test["annotations"].append(annotation_info)
with open('{}/anno_train_refine.json'.format(annotation_root_path), 'w') as output_json_file: json.dump(coco_output_train, output_json_file) with open('{}/anno_val_refine.json'.format(annotation_root_path), 'w') as output_json_file: json.dump(coco_output_val, output_json_file) with open('{}/anno_test_refine.json'.format(annotation_root_path), 'w') as output_json_file: json.dump(coco_output_test, output_json_file)
|