Load result file and return a result api object. :param resFile (str) : file name of result file :return: res (obj) : result api object
(self, resFile)
| 280 | print ann['caption'] |
| 281 | |
| 282 | def loadRes(self, resFile): |
| 283 | """ |
| 284 | Load result file and return a result api object. |
| 285 | :param resFile (str) : file name of result file |
| 286 | :return: res (obj) : result api object |
| 287 | """ |
| 288 | res = COCO() |
| 289 | res.dataset['images'] = [img for img in self.dataset['images']] |
| 290 | # res.dataset['info'] = copy.deepcopy(self.dataset['info']) |
| 291 | # res.dataset['licenses'] = copy.deepcopy(self.dataset['licenses']) |
| 292 | |
| 293 | print 'Loading and preparing results... ' |
| 294 | tic = time.time() |
| 295 | anns = json.load(open(resFile)) |
| 296 | assert type(anns) == list, 'results in not an array of objects' |
| 297 | annsImgIds = [ann['image_id'] for ann in anns] |
| 298 | assert set(annsImgIds) == (set(annsImgIds) & set(self.getImgIds())), \ |
| 299 | 'Results do not correspond to current coco set' |
| 300 | if 'caption' in anns[0]: |
| 301 | imgIds = set([img['id'] for img in res.dataset['images']]) & set([ann['image_id'] for ann in anns]) |
| 302 | res.dataset['images'] = [img for img in res.dataset['images'] if img['id'] in imgIds] |
| 303 | for id, ann in enumerate(anns): |
| 304 | ann['id'] = id+1 |
| 305 | elif 'bbox' in anns[0] and not anns[0]['bbox'] == []: |
| 306 | res.dataset['categories'] = copy.deepcopy(self.dataset['categories']) |
| 307 | for id, ann in enumerate(anns): |
| 308 | bb = ann['bbox'] |
| 309 | x1, x2, y1, y2 = [bb[0], bb[0]+bb[2], bb[1], bb[1]+bb[3]] |
| 310 | if not 'segmentation' in ann: |
| 311 | ann['segmentation'] = [[x1, y1, x1, y2, x2, y2, x2, y1]] |
| 312 | ann['area'] = bb[2]*bb[3] |
| 313 | ann['id'] = id+1 |
| 314 | ann['iscrowd'] = 0 |
| 315 | elif 'segmentation' in anns[0]: |
| 316 | res.dataset['categories'] = copy.deepcopy(self.dataset['categories']) |
| 317 | for id, ann in enumerate(anns): |
| 318 | # now only support compressed RLE format as segmentation results |
| 319 | ann['area'] = mask.area([ann['segmentation']])[0] |
| 320 | if not 'bbox' in ann: |
| 321 | ann['bbox'] = mask.toBbox([ann['segmentation']])[0] |
| 322 | ann['id'] = id+1 |
| 323 | ann['iscrowd'] = 0 |
| 324 | print 'DONE (t=%0.2fs)'%(time.time()- tic) |
| 325 | |
| 326 | res.dataset['annotations'] = anns |
| 327 | res.createIndex() |
| 328 | return res |
| 329 | |
| 330 | def download( self, tarDir = None, imgIds = [] ): |
| 331 | ''' |
no test coverage detected