AU-AIR数据集json文件转YOLO格式

文章讲述了如何将AU-AIR数据集中的COCO标注转换为YOLO所需的txt格式,以便于模型训练。
摘要由CSDN通过智能技术生成

AU-AIR数据集是第一个用于目标检测的多模态无人机数据集,可用于小目标检测。

数据集下载地址:AU-AIR:无人机视觉检测数据集_数据集-飞桨AI Studio星河社区

数据集中给出的标签文件是json文件,在YOLO系列模型上运行需要转化为txt文件,可使用下列方法进行转化,亲测可用。


#将数据集转化为 YOLO 格式的数据集
#--json_path 输入的json文件路径
#--save_path 保存的文件夹名字,默认为当前目录下的labels。

import os
import json
from tqdm import tqdm
import argparse

parser = argparse.ArgumentParser()
#这里根据自己的json文件位置,换成自己的就行
parser.add_argument('--json_path', default='annotations.json',type=str, help="input: coco format(json)")#json文件地址
#这里设置.txt文件保存位置
parser.add_argument('--save_path', default='labels', type=str, help="specify where to save the output dir of labels")#保存label文件地址
arg = parser.parse_args()

def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = box[0] + box[2] / 2.0
    y = box[1] + box[3] / 2.0
    w = box[2]
    h = box[3]
#round函数确定(xmin, ymin, xmax, ymax)的小数位数
    x = round(x * dw, 6)
    w = round(w * dw, 6)
    y = round(y * dh, 6)
    h = round(h * dh, 6)
    return (x, y, w, h)

if __name__ == '__main__':
    json_file =   arg.json_path # COCO Object Instance 类型的标注
    ana_txt_save_path = arg.save_path  # 保存的路径

    data = json.load(open(json_file, 'r'))
    if not os.path.exists(ana_txt_save_path):
        os.makedirs(ana_txt_save_path)
        
    list_file = open(os.path.join('dataSet_path/', 'trainval.txt'), 'w')#保存训练txt文件
    for img in tqdm(data['annotations']):
        filename = img["image_name"]
        img_width = img["image_width:"]#此处要有这个:,json文件中就有
        img_height = img["image_height"]
        #img_id = img["id"]
        head, tail = os.path.splitext(filename)
        ana_txt_name = head + ".txt"  # 对应的txt名字,与jpg一致
        f_txt = open(os.path.join(ana_txt_save_path, ana_txt_name), 'w')
        bbox = img["bbox"]
        for i in range(len(bbox)):
            x = bbox[i]
            box = [bbox[i]["left"], bbox[i]["top"], bbox[i]["width"], bbox[i]["height"]]
            box_c = convert((img_width, img_height), box)
            f_txt.write("%s %s %s %s %s\n" % (bbox[i]["class"], box_c[0], box_c[1], box_c[2], box_c[3]))

        f_txt.close()
        list_file.write('auair/JPEGImages/%s.jpg\n' %(head))#此处注意图片是jpg还是png
    list_file.close()

你可以使用以下步骤将COCO数据集JSON格式换为YOLO格式: 1. 导入所需的库: ```python import json ``` 2. 读取COCO JSON文件: ```python with open('path/to/coco.json', 'r') as f: data = json.load(f) ``` 3. 定义类别标签的映射关系: ```python class_labels = { 1: 'person', 2: 'bicycle', 3: 'car', # 添加其他类别... } ``` 4. 遍历COCO数据集中的每个图像和标注: ```python for image in data['images']: image_id = image['id'] file_name = image['file_name'] # 根据图像ID获取对应的标注信息 annotations = [ann for ann in data['annotations'] if ann['image_id'] == image_id] # 创建YOLO格式的标注文件 with open('path/to/yolo_annotations/{}.txt'.format(file_name.split('.')[0]), 'w') as f: for ann in annotations: category_id = ann['category_id'] bbox = ann['bbox'] # 计算YOLO格式的边界框坐标 x_center = bbox[0] + bbox[2] / 2 y_center = bbox[1] + bbox[3] / 2 width = bbox[2] height = bbox[3] # 将边界框坐标归一化到图像尺寸范围(0~1) x_center /= image['width'] y_center /= image['height'] width /= image['width'] height /= image['height'] # 将标注写入YOLO格式的标注文件 f.write('{} {:.6f} {:.6f} {:.6f} {:.6f}\n'.format(class_labels[category_id], x_center, y_center, width, height)) ``` 这样,你就可以将COCO数据集JSON格式换为YOLO格式的标注文件。请确保将代码中的路径替换为实际的文件路径。同时,你还需要根据自己的需求修改类别标签的映射关系。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值