XML文件划分为训练集和测试集

该脚本使用Python的os和random库将数据集按指定比例划分为训练集(90%)、验证集(10%)以及进一步将验证集分为训练和测试集。它读取Annotations-guosai目录下的XML文件列表,生成对应的trainval.txt、test.txt、train.txt和val.txt文件,用于后续机器学习模型的训练和评估。
摘要由CSDN通过智能技术生成
import os
import random
trainval_percent = 0.1
train_percent = 0.9
xmlfilepath = 'data/Annotations-guosai'
txtsavepath = 'data/ImageSets'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)
ftrainval = open('data/ImageSets/trainval.txt', 'w')
ftest = open('data/ImageSets/test.txt', 'w')
ftrain = open('data/ImageSets/train.txt', 'w')
fval = open('data/ImageSets/val.txt', 'w')
for i in list:
    name = total_xml[i][:-4] + '\n'
    if i in trainval:
        ftrainval.write(name)
        if i in train:
            ftest.write(name)
        else:
            fval.write(name)
    else:
        ftrain.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()

rsod数据集XML换为yolo格式的过程主要是为了适应YOLO(You Only Look Once)对象检测模型的训练格式。YOLO模型通常需要输入数据的标注格式为文本文件,每行包含信息:类索引、中心点坐标、宽和高。而rsod数据集中的标注文件可能是XML格式,这种格式通常包含图像信息和每个目标的详细标注信息,包括边界框的位置、大小以及类别等。 换过程大体可以分为以下步骤: 1. 读取XML文件:遍历数据集中的每个图像文件对应的XML标注文件,解析其中的信息。 2. 提取标注信息:从XML文件中提取出每个目标的类别、边界框的位置和尺寸。 3. 换为YOLO格式:将提取的边界框信息换为YOLO格式,即将边框坐标换为相对于图像尺寸的比例值(中心点坐标以及宽高),并且计算每个目标的类别索引。 4. 写入到TXT文件:将换后的信息写入到以图像名称命名的文本文件中,格式通常为:`<class> <x_center> <y_center> <width> <height>`,每个对象占一行。 5. 划分训练和测试集:根据需求将数据集随机分配为训练集测试集。这可以通过编写脚本实现,随机选择一定比例的图像分配到测试集中,剩余的图像则作为训练集。 以下是一个简单的Python代码示例来展示上述步骤: ```python import os import glob import xml.etree.ElementTree as ET from sklearn.model_selection import train_test_split # 假设data_path是XML文件所在的文件夹路径 data_path = '/path/to/rsod/xml/files' txt_save_path = '/path/to/save/yolo/files' # 获取所有的.xml文件 xml_files = glob.glob(os.path.join(data_path, '*.xml')) # 用于存储图像名称和对应标注信息 images = [] annotations = [] for xml_file in xml_files: tree = ET.parse(xml_file) root = tree.getroot() image_name = os.path.basename(root.find('filename').text) image_width = int(root.find('size/width').text) image_height = int(root.find('size/height').text) for member in root.findall('object'): class_id = member[0].text xmin = member[4][0].text ymin = member[4][1].text xmax = member[4][2].text ymax = member[4][3].text # 换边界框坐标到YOLO格式 x_center = ((float(xmin) + float(xmax)) / 2) / image_width y_center = ((float(ymin) + float(ymax)) / 2) / image_height width = (float(xmax) - float(xmin)) / image_width height = (float(ymax) - float(ymin)) / image_height # 添加到标注信息列表中 annotations.append(f"{class_id} {x_center} {y_center} {width} {height}") # 记录图像名称 images.append(image_name) # 分割训练集测试集 train_images, test_images, train_annotations, test_annotations = train_test_split(images, annotations, test_size=0.2) # 保存YOLO格式的训练集测试集标注文件 for image, annotation in zip(train_images, train_annotations): with open(os.path.join(txt_save_path, f"{image.split('.')[0]}.txt"), 'w') as file: file.write(annotation) for image, annotation in zip(test_images, test_annotations): with open(os.path.join(txt_save_path, f"{image.split('.')[0]}_test.txt"), 'w') as file: file.write(annotation) ``` 请根据实际情况调整上述代码中的路径和其他参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值