使用cocotools对yolov5的检测结果进行评估

项目场景:

用自制数据集训练yolov5,数据集是yolo的txt格式。训练完成后需要用coco指标衡量模型的性能。yolov5中的val.py没有带coco的测评,所以需要自己用pycocotools实现。


步骤分解

0. 转换图像命名

由于coco数据集名称都是以0000001.jpg类似格式命名,为了后续好操作,我们沿用这样的命名格式,将图片重命名,代码如下:

import os
# 获取文件列表,并按照你希望的顺序排序
file_dir = "xxxx" #读取文件的地址
file_names = sorted(os.listdir(file_dir))
save_path="xxxxx" #保存文件的地址

# 定义起始编号
start_index = 1

# 遍历文件列表,对每个文件名进行重新命名
for file_name in file_names:
    # 获取文件扩展名
    _, file_ext = os.path.splitext(file_name)
    
    # 构建新的文件名,使用 str.zfill() 方法填充编号为六位数字
    new_file_name = f"{
     start_index:07d}{
     file_ext}"
    
    # 拼接原始路径和新的文件名
    old_file_path = os.path.join(file_dir, file_name)
    new_file_path = os.path.join(save_path, new_file_name)
    
    # 重命名文件
    os.rename(old_file_path, new_file_path)
    
    # 增加编号
    start_index += 1

上述代码得到的结果是:
在这里插入图片描述

1. 需要得到groundtruth的json文件

a.yolo格式的txt标签数据格式如下:
3 0.482528 0.424537 0.00994318 0.0231481 
5 0.773295 0.319907 0.00909091 0.0564815 
4 0.366903 0.400463 0.0286932 0.0490741 
3 0.287642 0.444907 0.0178977 0.106481 
5 0.546591 0.199074 0.00852273 0.0703704 
5 0.578125 0.198148 0.00965909 0.0740741 
4 0.325284 0.421296 0.0568182 0.133333 
4 0.152415 0.405556 0.258807 0.32037

(第一位是class_id,然后是xywh坐标,坐标均经过归一化)

b.coco数据集是一个json格式的文件,由info、images、annotations、categories、licenses五部分组成。

  {
   "info": {
   
    "description": "",
    "url": "",
    "version": "",
    "year": 2023,
    "contributor": "JeJe",
    "date_created": "2023-05-18"
  },
  "licenses": [
    {
   
      "id": 1,
      "name": null,
      "url": null
    }
  ],
  "categories": [
    {
   
      "id": 1,
      "name": "pedestrian",
      "supercategory": "None"
    }
  ],
  "images": [
    {
   
      "file_name": "0000001.jpg",
      "height": 1080,
      "width": 3520,
      "date_captured": "2022-07-8",
      "id": 1,
      "license": 1,
      "color_url": "",
      "flickr_url": ""
    },
    
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值