Skip to content

Instantly share code, notes, and snippets.

@monisoi
Created April 22, 2018 04:12
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 monisoi/c6e620f82c259eb9ac4c0d66bda4e4f4 to your computer and use it in GitHub Desktop.
Save monisoi/c6e620f82c259eb9ac4c0d66bda4e4f4 to your computer and use it in GitHub Desktop.
import urllib.request
import sys
import os
import json
def merge_by_image_id(import_file):
merged_images = []
f = open(import_file, 'r')
json_data = json.load(f)
images = json_data["images"]
annotations = json_data["annotations"]
for i in range(len(annotations)):
image_id = annotations[i]["image_id"]
label_id = annotations[i]["label_id"]
urls = [x["url"][0] for x in images if x["image_id"] == image_id]
url = urls[0] if len(urls) else ""
image = {"image_id": image_id, "label_id": label_id, "url": url}
print("image_id {0}".format(image_id))
merged_images.append(image)
merged_images_dict = {"images": merged_images}
fw = open('merged_images.json', 'w')
json.dump(merged_images_dict, fw, indent=2)
if __name__ == "__main__":
import_file = sys.argv[1]
merge_by_image_id(import_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment