Skip to content

Commit

Permalink
Relax parsing requirements, fix a typo
Browse files Browse the repository at this point in the history
- Fix a typo in coco.py (tylin#44)
- Allow unavailable type, licenses and info keys (tylin#45
tylin#26)
- If GT file contains more annotations, filter them to the image IDs
provided in the results file for convenience
  • Loading branch information
ozancaglayan committed Aug 1, 2019
1 parent 3a9afb2 commit 31065b7
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions pycocotools/coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def createIndex(self):

cats = []
catToImgs = []
if self.dataset['type'] == 'instances':
if self.dataset.get('type', '') == 'instances':
cats = {cat['id']: [] for cat in self.dataset['categories']}
for cat in self.dataset['categories']:
cats[cat['id']] = cat
Expand All @@ -112,7 +112,7 @@ def info(self):
Print information about the annotation file.
:return:
"""
for key, value in self.datset['info'].items():
for key, value in self.dataset['info'].items():
print '%s: %s'%(key, value)

def getAnnIds(self, imgIds=[], catIds=[], areaRng=[], iscrowd=None):
Expand Down Expand Up @@ -266,15 +266,19 @@ def loadRes(self, resFile):
"""
res = COCO()
res.dataset['images'] = [img for img in self.dataset['images']]
res.dataset['info'] = copy.deepcopy(self.dataset['info'])
res.dataset['type'] = copy.deepcopy(self.dataset['type'])
res.dataset['licenses'] = copy.deepcopy(self.dataset['licenses'])
res.dataset['info'] = copy.deepcopy(self.dataset.get('info', {}))
res.dataset['type'] = copy.deepcopy(self.dataset.get('type', ''))
res.dataset['licenses'] = copy.deepcopy(self.dataset.get('licenses', []))

print 'Loading and preparing results... '
time_t = datetime.datetime.utcnow()
anns = json.load(open(resFile))
anns = json.load(open(resFile))
assert type(anns) == list, 'results in not an array of objects'
annsImgIds = [ann['image_id'] for ann in anns]

# If the results contain more annotations, restrict them to the
# matching ones with the ground truth annotations
_imgIds = self.getImgIds()
annsImgIds = [ann['image_id'] for ann in anns if ann['image_id'] in _imgIds]
assert set(annsImgIds) == (set(annsImgIds) & set(self.getImgIds())), \
'Results do not correspond to current coco set'
if 'caption' in anns[0]:
Expand Down

0 comments on commit 31065b7

Please sign in to comment.