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