Created
April 9, 2021 05:39
-
-
Save trongan93/203b7d88441663f4a62c84ab244ef223 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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