from pycocotools.coco import COCO
from pathlib import Path
import matplotlib.pyplot as plt
import cv2
import os
import numpy as np
root = Path("/Users/enzo/Documents/GitHub/dataset/COCO2017")
train_img_file = os.path.join(root, 'train2017')
val_img_file = os.path.join(root, 'val2017')
train_ann_file = os.path.join(root, 'annotations/instances_train2017.json')
val_ann_file = os.path.join(root, 'annotations/instances_val2017.json')
assert os.path.exists(train_ann_file), f'provided COCO path {root} does not exist'
assert os.path.exists(val_ann_file), f'provided COCO path {root} does not exist'
coco = COCO(train_ann_file)
print('\n', '*'*40, '\n')
"""
getAnnIds(imgIds=[], catIds=[], areaRng=[], iscrowd=None)
Get ann ids that satisfy given filter conditions. default skips that filter
:param imgIds (int array) : get anns for given imgs
catIds (int array) : get anns for given cats
areaRng (float array) : get anns for given area range (e.g. [0 inf])
iscrowd (boolean) : get anns for given crowd label (False or True)
:return: ids (int array) : integer array of ann ids
"""
ids = coco.getAnnIds(catIds=[18])
print(len(ids))
"""
getCatIds(catNms=[], supNms=[], catIds=[])
filtering parameters. default skips that filter.
:param catNms (str array) : get cats for given cat names
:param supNms (str array) : get cats for given supercategory names
:param catIds (int array) : get cats for given cat ids
:return: ids (int array) : integer array of cat ids
"""
id = coco.getCatIds(catNms=['dog'])
ids = coco.getCatIds(supNms=['animal'])
print(id)
print(ids)
'''
getImgIds(imgIds=[], catIds=[])
Get img ids that satisfy given filter conditions.
:param imgIds (int array) : get imgs for given ids
:param catIds (int array) : get imgs with all given cats
:return: ids (int array) : integer array of img ids
'''
ids = coco.getImgIds(catIds=[18])
print(len(ids))
"""
loadAnns(ids=[])
Load anns with the specified ids.
:param ids (int array) : integer ids specifying anns
:return: anns (object array) : loaded ann objects
"""
anns = coco.loadAnns(ids=[1727])
print(anns[0].keys())
"""
loadCats(ids=[])
Load cats with the specified ids.
:param ids (int array) : integer ids specifying cats
:return: cats (object array) : loaded cat objects
"""
cats = coco.loadCats(ids=[18])
print(cats)
"""
loadImgs(ids=[])
Load anns with the specified ids.
:param ids (int array) : integer ids specifying img
:return: imgs (object array) : loaded img objects
"""
imgs = coco.loadImgs(ids=[98304])
print(imgs)
"""
showAnns(anns)
Display the specified annotations.
:param anns (array of object): annotations to display
:return: None
"""
img_file = os.path.join(train_img_file, '000000098304.jpg')
img = cv2.imread(img_file)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
anns = coco.loadAnns(coco.getAnnIds(imgIds=[98304]))
coco.showAnns(anns)
plt.show()
"""
loadRes(resFile)
Load result file and return a result api object.
:param resFile (str) : file name of result file
:return: res (obj) : result api object
"""
cocoRes = coco.loadRes("results.json")