Skip to content

Instantly share code, notes, and snippets.

@trongan93
Created April 9, 2021 05:39
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 trongan93/203b7d88441663f4a62c84ab244ef223 to your computer and use it in GitHub Desktop.
Save trongan93/203b7d88441663f4a62c84ab244ef223 to your computer and use it in GitHub Desktop.
from pycocotools.coco import COCO
json_file = "/mnt/d/RarePlanes/datasets/synthetic/metadata_annotations/instances_train_aircraft.json"
coco=COCO(json_file)
# display COCO categories
cats = coco.loadCats(coco.getCatIds())
nms=[cat['name'] for cat in cats]
print('COCO categories: \n{}\n'.format(' '.join(nms)))
# get all images containing given categories, select one at random
catIds = coco.getCatIds(catNms=['aircraft']);
imgIds = coco.getImgIds(catIds=catIds);
imgIds = coco.getImgIds(imgIds = [22])
img = coco.loadImgs(imgIds[np.random.randint(0,len(imgIds))])[0]
print(img)
# load and display image
I = cv2.imread("/mnt/d/RarePlanes/datasets/synthetic/train/images/" + img['file_name'])
plt.axis('off')
plt.imshow(I)
plt.show()
# load and display instance annotations
plt.imshow(I); plt.axis('off')
annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment