0

したがって、次のコードがあり、画像に含まれるすべてのカテゴリを取得したいと考えていますが、画像に複数の注釈がある場合でも、1 つの画像に対して 1 つのカテゴリしか表示されません: 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)

また、1 つの画像 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

4

1 に答える 1

0

画像のすべてのカテゴリを表示する固定コードは次のとおりです。

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()

この Web サイトを使用して確認できます: http://mscoco.org/explore/?id=1927052

于 2016-11-01T02:06:28.693 に答える