0
所以我有下面的代碼,我希望獲得圖像所有的類別,但它只顯示一個圖像的一個類別,即使該圖像可能有多條註釋: https://gist.github.com/monajalal/7cb8f660848421a38447160f7e9fefff我沒有收到圖像使用的所有cat_id ann
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import pylab
import sys
from pprint import pprint as p
from time import sleep
import os
sys.path.append('/home/mona/mscoco/coco/PythonAPI')
p(sys.path)
from pycocotools.coco import COCO
pylab.rcParams['figure.figsize'] = (10.0, 8.0)
dataDir='.'
dataType='train2014'
annFile='%s/annotations/instances_%s.json'%(dataDir,dataType)
coco = COCO(annFile)
cats = coco.loadCats(coco.getCatIds())
nms = [cat['name'] for cat in cats]
annFile = '%s/annotations/captions_%s.json'%(dataDir,dataType)
coco_caps=COCO(annFile)
categories = {"person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis", "snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon" ,"bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"}
for category in categories:
category_path = "/home/mona/mscoco/all_categories_mscoco_caption/"+category
if not os.path.exists(category_path):
os.makedirs(category_path)
image_count = 0
catIds = coco.getCatIds(category)
imgIds = coco.getImgIds(catIds=catIds);
for imgId in imgIds:
print("{0}: {1}".format("image id is", imgId))
if image_count < 1:
image_count += 1
img = coco.loadImgs(imgId)[0]
annIds = coco.getAnnIds(imgId, catIds=catIds, iscrowd = None)
anns = coco.loadAnns(annIds)
print("{0}: {1}".format("length of annotation is", len(anns)))
for ann in range(len(anns)):
print("annotation is")
print("{0}: {1}".format("category_id is", anns[ann]['category_id']))
print("{0}: {1}".format("id is", anns[ann]['id']))
print(coco.loadCats(anns[ann]['category_id']))
coco.showAnns(anns)
annIds = coco_caps.getAnnIds(imgId);
anns = coco_caps.loadAnns(annIds)
filename = "/home/mona/mscoco/all_categories_mscoco_caption/"+category+'/' +category+'_'+str(imgId) + ".txt"
caption_file = open(filename, 'wb')
for i in range(5):
caption_file.write((anns[i]['caption']) + os.linesep)
caption_file.close()
else:
break
imgId = 152360
print(type(coco.loadImgs))
print(dir(coco.loadImgs))
img = coco.loadImgs(imgId)[0]
print(img)
而且我怎麼能只加載與一個圖像ID的類別?代碼的最後幾行給了一個錯誤:
Traceback (most recent call last):
File "try_coco.py", line 65, in <module>
img = coco.loadImgs(imgId)[0]
File "/home/mona/mscoco/coco/PythonAPI/pycocotools/coco.py", line 219, in loadImgs
return [self.imgs[ids]]
KeyError: 152360
下面的代碼執行的樣子:
image id is: 98304
length of annotation is: 3
annotation is
category_id is: 47
id is: 1511501
[{u'supercategory': u'kitchen', u'id': 47, u'name': u'cup'}]
annotation is
category_id is: 47
id is: 1513367
[{u'supercategory': u'kitchen', u'id': 47, u'name': u'cup'}]
annotation is
category_id is: 47
id is: 2099764
[{u'supercategory': u'kitchen', u'id': 47, u'name': u'cup'}]
我們看到各種類別顯示在這裏然而僅僅使用類別杯被捕獲的API。 http://mscoco.org/explore/?id=1927052