Skip to content

Instantly share code, notes, and snippets.

@THEFASHIONGEEK
Created February 18, 2020 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save THEFASHIONGEEK/f87fee5c8b475589572c26e479763a28 to your computer and use it in GitHub Desktop.
Save THEFASHIONGEEK/f87fee5c8b475589572c26e479763a28 to your computer and use it in GitHub Desktop.
coco_data = {}
coco_data["type"] = "instances"
coco_data["images"] = []
coco_data["annotations"] = []
coco_data["categories"] = list_dict
image_id = 0
annotation_id = 0
for i in tqdm(range(len(df))):
img_name = df[columns[0]][i]
labels = df[columns[1]][i]
tmp = labels.split(delimiter);
image_in_path = input_images_folder + "/" + img_name
if(not os.path.isfile(image_in_path)):
continue
img = cv2.imread(image_in_path, 1)
h, w, c = img.shape
images_tmp = {}
images_tmp["file_name"] = img_name
images_tmp["height"] = h
images_tmp["width"] = w
images_tmp["id"] = image_id
coco_data["images"].append(images_tmp)
for j in range(len(tmp)//5):
x1 = int(tmp[j*5+0])
y1 = int(tmp[j*5+1])
x2 = int(tmp[j*5+2])
y2 = int(tmp[j*5+3])
label = tmp[j*5+4]
annotations_tmp = {}
annotations_tmp["id"] = annotation_id
annotation_id += 1
annotations_tmp["image_id"] = image_id
annotations_tmp["segmentation"] = []
annotations_tmp["ignore"] = 0
annotations_tmp["area"] = (x2-x1)*(y2-y1)
annotations_tmp["iscrowd"] = 0
annotations_tmp["bbox"] = [x1, y1, x2-x1, y2-y1]
annotations_tmp["category_id"] = anno.index(label)
coco_data["annotations"].append(annotations_tmp)
image_id += 1
outfile = open(output_annotation_file, 'w')
json_str = json.dumps(coco_data, indent=4)
outfile.write(json_str)
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment