Я не получаю все cat_ids изображения с помощью ann
Поэтому у меня есть следующий код, и я надеюсь получить все категории, которые есть на изображении, однако он просто показывает одну категорию для одного изображения, даже если это изображение может иметь несколько аннотаций: https://gist.github.com/monajalal/7cb8f660848421a38447160f7e9fefff
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)
Кроме того, как я могу просто загрузить категории, связанные с одним идентификатором изображения? Последние несколько строк кода дали ошибку:
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
1 ответ
Вот фиксированный код, который показывает все категории изображения:
import csv
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"}
stat_file = open('all_stat.csv', 'wb')
a_complete = csv.writer(stat_file, delimiter=',')
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:
if image_count < 65:
stat_row = []
stat_row.append(str(imgId))
print("{0}: {1}".format("image id is", imgId))
image_count += 1
img = coco.loadImgs(imgId)[0]
annIds = coco.getAnnIds(imgId, catIds=catIds, iscrowd = None)
anns = coco.loadAnns(annIds)
print(coco.getAnnIds(imgId))
list_anns = coco.getAnnIds(imgId)
annotations = coco.loadAnns(list_anns)
for annotation in annotations:
stat_row.append(coco.loadCats(annotation['category_id'])[0]['name'])
#print("{0}: {1}".format("category_id is", annotation['category_id']))
print(coco.loadCats(annotation['category_id'])[0]['name'])
a_complete.writerow(stat_row)
coco.showAnns(anns)
annIds = coco_caps.getAnnIds(imgId)
captions = coco_caps.loadAnns(annIds)
#filename = "/home/mona/mscoco/all_categories_mscoco_caption/"+category+'/'++str(imgId) + ".txt"
filename = "/home/mona/mscoco/merged_captions/"+str(imgId)+".txt"
caption_file = open(filename, 'wb')
for i in range(5):
caption_file.write((captions[i]['caption']) + os.linesep)
caption_file.close()
else:
break
stat_file.close()
Вы можете проверить, используя этот сайт: http://mscoco.org/explore/?id=1927052