def visualize_dataset(imageDir, annFile): coco = COCO(annFile) # Compute area if area is not set in annotations. coco.computeArea() # display coco categories and supercategories cats = coco.loadCats(coco.getCatIds()) nms = [cat['name'] for cat in cats] print('coco categories: \n{}\n'.format(' '.join(nms))) super_nms = set([cat['supercategory'] for cat in cats]) print('coco supercategories: \n{}\n'.format(' '.join(super_nms))) coco.printCatNums() # get all images containing given categories, select one at random catIds = coco.getCatIds(catNms=nms) print(len(catIds)) imgIds = coco.getImgIdsUnion(catIds=catIds) # imgIds = coco.getImgIdsUnion(catIds=coco.getCatIds()); # imgIds = coco.getImgIds(catIds=catIds); # imgIds= coco.getImgIds(); imgIds = sorted(imgIds) print(len(imgIds)) # show image and annotations while True: # randomly pick one image imgId = imgIds[np.random.randint(len(imgIds))] # print(imgId) img = coco.loadImgs(imgId)[0] # I = io.imread(img['file_name']) # print(I.shape) # I = io.imread('%s/images/%s/%s'%(dataDir,dataType,img['file_name'])) # I = io.imread("{}".format(img['file_name'])) # print(img['file_name']) # print(os.path.join(imageDir, img['file_name'])) I = Image.open(os.path.join(imageDir, img['file_name'])) print(img['file_name']) # load and display instance annotations plt.imshow(I) plt.axis('off') annIds = coco.getAnnIds(imgIds=imgId, catIds=catIds) print(annIds) anns = coco.loadAnns(annIds) coco.showAnns(anns, draw_bbox=True) # plt.show() plt.draw() # need to use draw() instead of show() when use waitforbutttonpress() while not plt.waitforbuttonpress(): continue plt.clf()
# ----------------------------------------------------- import sys import json sys.path.insert(0, sys.path[0] + '/..') import os from pycocotools.coco import COCO annFile = sys.argv[1] ratios = [0.6, 0.2, 0.2] subsets_names = ['train', 'val', 'test'] coco = COCO(annFile) # Compute area if area is not set in annotations. coco.computeArea() # catIds = coco.getCatIds() # all cats catNms = ['yin_hua_qing_xie'] catIds = coco.getCatIds(catNms=catNms) print("image num all: {}".format(len(coco.getImgIds(catIds=catIds)))) json_subsets = coco.splitAnno(catIds, ratios) output_dir = os.path.dirname(annFile) for name, json_dict in zip(subsets_names, json_subsets): print("image num {}: {}".format(name, len(json_dict['images']))) json_file = os.path.join(output_dir, name + ".json") # os.makedirs(os.path.dirname(json_file), exist_ok=True) assert not os.path.exists(json_file) with open(json_file, 'w', encoding='utf-8') as f:
import sys sys.path.insert(0, sys.path[0] + '/..') from pycocotools.coco import COCO from pycocotools.cocoeval import COCOeval annType = ['segm', 'bbox', 'keypoints'] annType = annType[1] # specify type here prefix = 'person_keypoints' if annType == 'keypoints' else 'instances' print('Running demo for *%s* results.'.format(annType)) # initialize COCO ground truth api annFile = 'test_set.json' cocoGt = COCO(annFile) # Compute area if area is not set in annotations. cocoGt.computeArea() # initialize COCO detections api resFile = 'test_set_results.json' cocoDt = cocoGt.loadRes(resFile) # running evaluation cocoEval = COCOeval(cocoGt, cocoDt, annType) # cocoEval.params.imgIds = imgIds # area_seq = [400, 800, 1200, 1600, 2000, 1e10] # area_rng = [[0,area_seq[-1]]] # for i in range(len(area_seq)) # cocoEval.params.areaRng = [[0 ** 2, 10e5 ** 2], [0 ** 2, 32 ** 2], # [32 ** 2, 64 ** 2], # [64 ** 2, 96 ** 2], # [96 ** 2, 1e5 ** 2]] # cocoEval.params.areaRngLbl = ['all', 'small', 'sm', 'medium', 'large'] # area_seq = [32**2, 96**2]