LZ_Jaja

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Reference:

  1. https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoDemo.ipynb
  2. https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py
  3. https://blog.csdn.net/yeyang911/article/details/78675942

Here is my code. But there is still a problem that when I use the function 'showAnns()' to show the instances' annotations of the image, it cannot show the figure though the code run successfully. If you have any solutions, please leave a message in the comment area. Thanks!

  1 import matplotlib
  2 import numpy as np
  3 import skimage.io as io
  4 import matplotlib.pyplot as plt
  5 import pylab
  6 from pycocotools.coco import COCO
  7 pylab.rcParams['figure.figsize'] = (8.0, 10.0)  # image pixel
  8 
  9 # if skimage.io declares before pycocotools.coco, the backend will be chosen as Qt5Agg
 10 # if pycocotools.coco declares before skimage.io, the backend will be chosen as Agg
 11 print(matplotlib.get_backend())
 12 
 13 dataDir = r'G:\MSCOCO 2017\annotations_trainval2017'
 14 dataType = r'val2017'
 15 annFile = r'{}\annotations\instances_{}.json'.format(dataDir, dataType)
 16 
 17 # initialize COCO api for instance annotations
 18 coco = COCO(annotation_file=annFile)    # COCO api class that loads COCO annotation file and prepare data structures.
 19 # loading annotations into memory...
 20 # Done (t=0.80s)
 21 # creating index...
 22 # index created!
 23 
 24 # print(coco.info())  # Print information about the annotation file.
 25 # description: COCO 2017 Dataset
 26 # url: http://cocodataset.org
 27 # version: 1.0
 28 # year: 2017
 29 # contributor: COCO Consortium
 30 # date_created: 2017/09/01
 31 # None
 32 
 33 # display COCO categories and supercategories
 34 # coco.loadCats(self, ids=[]): Load cats with the specified ids.
 35 cats = coco.loadCats(coco.getCatIds())
 36 # print(cats)
 37 # [{'supercategory': 'person', 'id': 1, 'name': 'person'},
 38 #  {'supercategory': 'vehicle', 'id': 2, 'name': 'bicycle'},
 39 #  {'supercategory': 'vehicle', 'id': 3, 'name': 'car'},
 40 #  {...}, {...}, {...}, ... ...,
 41 #  {'supercategory': 'indoor', 'id': 90, 'name': 'toothbrush'}]
 42 nms = [cat['name'] for cat in cats]
 43 # print('COCO categories: \n{}\n'.format(' '.join(nms)))
 44 # person bicycle car motorcycle airplane ... hair drier toothbrush
 45 nms = set([cat['supercategory'] for cat in cats])
 46 # print('COCO supercategories: \n{}'.format(' '.join(nms)))
 47 # vehicle person kitchen electronic appliance indoor accessory animal food furniture outdoor sports
 48 
 49 # get all images containing given categories, select one at random
 50 catIds = coco.getCatIds(catNms=['person', 'dog', 'skateboard'])
 51 # print(catIds)   # [1, 18, 41]
 52 imgIds = coco.getImgIds(catIds=catIds)      # param catIds (int array): get images with all given categories
 53 # imgIds = coco.getImgIds(imgIds=[324158])    # param imgIds (int array): get images for given ids
 54 index = np.random.randint(0, len(imgIds))
 55 # print('randomly selected imgId: %d' % index)
 56 img = coco.loadImgs(imgIds[index])[0]   # Load images with specified ids.
 57 # print(img)
 58 # if index=2, print:
 59 # {'license': 2, 'file_name': '000000279278.jpg', 'coco_url': 'http://images.cocodataset.org/val2017/000000279278.jpg',
 60 # 'height': 429, 'width': 640, 'date_captured': '2013-11-15 01:07:24',
 61 # 'flickr_url': 'http://farm7.staticflickr.com/6101/6275412942_f8dc734c3f_z.jpg', 'id': 279278}
 62 
 63 # load and display image
 64 # i = io.imread('%s/image/%s/%s' % (dataDir, dataType, img['file_name']))
 65 # use url to load image
 66 # print(img['coco_url'])  # http://images.cocodataset.org/val2017/000000279278.jpg
 67 i = io.imread(img['coco_url'])
 68 plt.axis('off')
 69 plt.imshow(i)
 70 # print(plt.imshow(i))    # AxesImage(100, 110; 620x770)
 71 plt.show()
 72 
 73 # load and display instance annotations
 74 plt.imshow(i)
 75 plt.axis('off')
 76 annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
 77 anns = coco.loadAnns(annIds)
 78 coco.showAnns(anns)
 79 
 80 # initialize COCO api for person keypoints annotations
 81 annFile = '{}/annotations/person_keypoints_{}.json'.format(dataDir, dataType)
 82 coco_kps = COCO(annFile)
 83 
 84 # load and display keypoints annotations
 85 plt.imshow(i)
 86 plt.axis('off')
 87 ax = plt.gca()
 88 annIds = coco_kps.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
 89 anns = coco_kps.loadAnns(annIds)
 90 coco_kps.showAnns(anns)
 91 
 92 # initialize COCO api for caption annotations
 93 annFile = '{}/annotations/captions_{}.json'.format(dataDir, dataType)
 94 coco_caps = COCO(annFile)
 95 
 96 # load and display caption annotations
 97 annIds = coco_caps.getAnnIds(imgIds=img['id'])
 98 anns = coco_caps.loadAnns(annIds)
 99 coco_caps.showAnns(anns)
100 plt.imshow(i)
101 plt.axis('off')
102 plt.show()

 

posted on 2018-11-15 22:58  LZ_Jaja  阅读(535)  评论(0编辑  收藏  举报