我正在尝试使用以下COCO命令下载COCO映像:
from pycocotools.coco import COCO
import requests
catIds = COCO.getCatIds(catNms=['person','dog', 'car'])...but正在收到以下错误消息。知道为什么会这样吗?
---------------------------------------------------------------------------
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'以前,我使用这些指示配置了化茧工具。
仍然有一些奇怪的错误信息。查看代码,似乎没有“人”、“狗”或“车”这样的类别。为什么会这样呢?
a = COCO()
catIds = a.getCatIds(catNms=['person','dog', 'car'])---------------------------------------------------------------------------
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'])
~\Anaconda3\lib\site-packages\pycocotools\coco.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'从文件“cocoapi/PythonAPI/pycocotools/coco.py”中提取:

发布于 2019-11-24 02:40:29
COCO是一个python类,getCatIds不是一个静态方法,只能由类COCO的实例/对象调用,而不能从类本身调用。
您可能可以通过这样做来解决这个问题:
a = COCO() # calling init
catIds = a.getCatIds(catNms=['person','dog', 'car']) # calling the method from the class(答案基于从https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py读取代码)
发布于 2019-12-25 19:16:14
根据演示代码这里,您必须分别下载数据,然后使用pycocotools包访问数据:
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'])https://datascience.stackexchange.com/questions/63639
复制相似问题