CREATING COCO STYLE DATASETS AND USING ITS API TO EVALUATE METRICS
Let's assume that we want to create annotations and results files for an object detection task (So, we are interested in just bounding boxes). Here is a simple and light-weight example which shows how one can create annoatation and result files appropriately formatted for using COCO API metrics.
Annotation file: ann.json
{"images":[{"id": 73}],"annotations":[{"image_id":73,"category_id":1,"bbox":[10,10,50,100],"id":1,"iscrowd": 0,"area": 10}],"categories": [{"id": 1, "name": "person"}, {"id": 2, "name": "bicycle"}, {"id": 3, "name": "car"}]}
Result file: res.json
[{"image_id":73,"category_id":1,"bbox":[10,10,50,100],"score":0.9}]
Now, you can simply use the following script to evaluate the COCO metrics:
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
annFile = './ann.json'
resFile='./res.json'
cocoGt=COCO(annFile)
cocoDt=cocoGt.loadRes(resFile)
annType = 'bbox'
cocoEval = COCOeval(cocoGt,cocoDt,annType)
cocoEval.evaluate()
cocoEval.accumulate()
cocoEval.summarize()