coco-caption
coco-caption copied to clipboard
KeyError: 'type' when loading annFile
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?
Same error, waiting for the solution.
You should use the 'annotations' data in the folder of this project rather than the data of coco. @ay27 @sharmalakshay93
@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)
Thanks @vhvkhoa. It solved my problem.