from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
import json
from tempfile import NamedTemporaryFile
# coco格式的json文件,原始标注数据
anno_file = '/home/j_m/Desktop/val/val.json'
coco_gt = COCO(anno_file)
# 用GT框作为预测框进行计算,目的是得到detection_res
with open(anno_file, 'r') as f:
json_file = json.load(f)
annotations = json_file['annotations']
detection_res = []
for anno in annotations:
detection_res.append({
'score': 1.,
'category_id': anno['category_id'],
'bbox': anno['bbox'],
'image_id': anno['image_id']
})
with NamedTemporaryFile(suffix='.json') as tf:
# 由于后续需要,先将detection_res转换成二进制后写入json文件
content = json.dumps(detection_res).encode(encoding='utf-8')
tf.write(content)
res_path = tf.name
# loadRes会在coco_gt的基础上生成一个新的COCO类型的instance并返回
coco_dt &
调用pycocotools计算mAP
最新推荐文章于 2024-07-10 15:19:58 发布
本文介绍了如何调用pycocotools库来计算目标检测任务中的每个类别的平均精度(mAP),并提供了相关代码来源。
摘要由CSDN通过智能技术生成