coco-caption icon indicating copy to clipboard operation
coco-caption copied to clipboard

KeyError: 'type' when loading annFile

Open sharmalakshay93 opened this issue 7 years ago • 4 comments

Hi,

Trying to use the cocoEvalCapDemo.ipynb, and running coco = COCO(annFile) throws the following error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-3-7f514b15e885> in <module>()
      1 # create coco object and cocoRes object
----> 2 coco = COCO(annFile)
      3 cocoRes = coco.loadRes(resFile)

/Users/lakshay/Documents/NYU/Studies/Fall17/cv/project/coco-caption-master/pycocotools/coco.pyc in __init__(self, annotation_file)
     74             print datetime.datetime.utcnow() - time_t
     75             self.dataset = dataset
---> 76             self.createIndex()
     77 
     78     def createIndex(self):

/Users/lakshay/Documents/NYU/Studies/Fall17/cv/project/coco-caption-master/pycocotools/coco.pyc in createIndex(self)
     91         cats = []
     92         catToImgs = []
---> 93         if self.dataset['type'] == 'instances':
     94             cats = {cat['id']: [] for cat in self.dataset['categories']}
     95             for cat in self.dataset['categories']:

KeyError: 'type'

No clue of why this is so. How could I fix this?

sharmalakshay93 avatar Dec 10 '17 20:12 sharmalakshay93

Same error, waiting for the solution.

ay27 avatar Jan 23 '18 02:01 ay27

You should use the 'annotations' data in the folder of this project rather than the data of coco. @ay27 @sharmalakshay93

junsuper17 avatar Jan 29 '18 15:01 junsuper17

@sharmalakshay93 @ay27 If you're using annotation files from COCO 2017. You can add a new key namely 'type' with any value except 'instances'to the json file to resolve this problem, this is actually a work around, the problem should be solved by changing the source code in pycocotools/coco.py

Below is some code of the work around:

import json
with open('captions_val2017.json', 'r') as f:
    data = json.load(f)
    data['type'] = 'captions'
with open('captions_val2017.json', 'w') as f:
    json.dump(data, f)

vhvkhoa avatar Nov 19 '18 09:11 vhvkhoa

Thanks @vhvkhoa. It solved my problem.

Anirudh257 avatar Jul 12 '19 08:07 Anirudh257