python-3.ximage-processingdeep-learningkeypointpycocotools

How to read specific keypoints in COCOEval


I need to calculate the mean average precision (mAP) of specific keypoints (and not for all keypoints, as it done by default). Here's my code :

from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval

# https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
cocoGt = COCO('annotations/person_keypoints_val2017.json')  # initialize COCO ground truth api
cocoDt = cocoGt.loadRes('detections/results.json')  # initialize COCO pred api

cat_ids = cocoGt.getCatIds(catNms=['person'])
imgIds = cocoGt.getImgIds(catIds=cat_ids)

cocoEval = COCOeval(cocoGt, cocoDt, 'keypoints')
cocoEval.params.imgIds = imgIds
cocoEval.evaluate()
cocoEval.accumulate()
cocoEval.summarize()

print(cocoEval.stats[0])

This code prints the mAP for all keypoints ['nose', ...,'right_ankle'] but I need only for few specific keypoints like ['nose', 'left_hip', 'right_hip']


Solution

  • I recently solved this and evaluated only the 13 key points, leaving behind the eyes and the ears as per my application.

    Just open the cocoeval.py under pycocotools, then head over to the computeOKS function, where you will encounter two sets of keypoints—ground truth keypoints—and detection keypoints, such as a NumPy array. Make sure to do proper slicing for that 51 array size Python lists.

    For example, if you wish to only check the mAP for nose, the slicing would be as follows:

    g= np.array(gt['keypoints'][0:3])
    

    Similarly, do it for a dt array.

    Also, set the sigma values of those unwanted key points to 0. You are all set!