TransWikia.com

How to download COCO dataset images?

Data Science Asked on August 4, 2021

I am trying to download the COCO dataset images using the following COCO API command:

from pycocotools.coco import COCO
import requests
catIds = COCO.getCatIds(catNms=['person','dog', 'car'])

…but am getting the following error message. Any idea why this is happening?

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-42-9cc4e2f62a0e> in <module>
----> 1 catIds = COCO.getCatIds(catNms=['person','dog', 'car'])

TypeError: getCatIds() missing 1 required positional argument: 'self'

Previously, I configured pycocotools using these instructions.

EDIT:

Still some strange error messages. Looking into code it seems as if there are no such categories as a ‘person’,’dog’ or ‘car’. Why is it so?

My code:

a = COCO()  
catIds = a.getCatIds(catNms=['person','dog', 'car'])

Received error message:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-12-57400207fde1> in <module>
      1 a = COCO() # calling init
----> 2 catIds = a.getCatIds(catNms=['person','dog', 'car'])

~Anaconda3libsite-packagespycocotoolscoco.py in getCatIds(self, catNms, supNms, catIds)
    171             cats = self.dataset['categories']
    172         else:
--> 173             cats = self.dataset['categories']
    174             cats = cats if len(catNms) == 0 else [cat for cat in cats if cat['name']          in catNms]
    175             cats = cats if len(supNms) == 0 else [cat for cat in cats if cat['supercategory'] in supNms]

KeyError: 'categories'

Extract from the file ‘cocoapi/PythonAPI/pycocotools/coco.py’:
enter image description here

2 Answers

COCO is a python class and getCatIds is not a Static Method, tho can only be called by an instance/object of the Class COCO and not from the class itself.

You can probably solve it by doing this instead:

a = COCO() # calling init
catIds = a.getCatIds(catNms=['person','dog', 'car']) # calling the method from the class

(answer based on reading the code from https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py)

Answered by Pedro Henrique Monforte on August 4, 2021

Based on the demo code here, you have to download the data separately then use the pycocotools package to access the data:

from pycocotools.coco import COCO

# After downloading images from http://cocodataset.org/#download

# Define location of annotations
dataDir = '..'
dataType = 'val2017'
annFile = f'{dataDir}/annotations/instances_{dataType}.json'

# Create instance
coco = COCO(annFile)

# Filter for specific categories 
catIds = coco.getCatIds(catNms=['person','dog', 'car'])

Answered by Brian Spiering on August 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP