本文主要是介绍DIOR数据集xml转txt格式并划分训练集测试集验证集(用于yolo),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.数据集下载
下载地址:
1)http://www.escience.cn/people/gongcheng/DIOR.html
2) 飞桨官网(推荐)
下载如下:
新建一个文件夹JPEGImages,将JPEGImages-test和PEGImages-trainval里的图片都放进JPEGImages里面。最后文件夹包含文件:
2.数据集预处理
# coding:utf-8import os import random import argparseimport xml.etree.ElementTree as ET from os import getcwd from shutil import copyfileparser = argparse.ArgumentParser() #xml文件的地址,根据自己的数据进行修改 xml一般存放在Annotations下 parser.add_argument('--xml_path', default='DIOR/Annotations', type=str, help='input xml label path') #数据集的划分,地址选择自己数据下的ImageSets/Mainopt = parser.parse_args()sets = ['train', 'val', 'test'] classes = ['airplane', 'airport', 'baseballfield', 'basketballcourt', 'bridge', 'chimney', 'dam','Expressway-Service-area', 'Expressway-toll-station', 'golffield', 'groundtrackfield', 'harbor','overpass', 'ship', 'stadium', 'storagetank', 'tenniscourt', 'trainstation', 'vehicle', 'windmill']abs_path = os.getcwd() print(abs_path)# if not os.path.exists('/DIOR'): # os.makedirs('DIOR')if not os.path.exists('DIOR_dataset/labels/'):os.makedirs('DIOR_dataset/labels/') if not os.path.exists('DIOR_dataset/labels/train'):os.makedirs('DIOR_dataset/labels/train') if not os.path.exists('DIOR_dataset_yolo/labels/test'):os.makedirs('DIOR_dataset/labels/test') if not os.path.exists('DIOR_dataset_yolo/labels/val'):os.makedirs('DIOR_dataset/labels/val')if not os.path.exists('DIOR_dataset/images/'):os.makedirs('DIOR_dataset/images/') if not os.path.exists('DIOR_dataset/images/train'):os.makedirs('DIOR_dataset/images/train') if not os.path.exists('DIOR_dataset/images/test'):os.makedirs('DIOR_dataset/images/test') if not os.path.exists('DIOR_dataset/images/val'):os.makedirs('DIOR_dataset/images/val')def convert(size, box):dw = 1. / (size[0])dh = 1. / (size[1])x = (box[0] + box[1]) / 2.0 - 1y = (box[2] + box[3]) / 2.0 - 1w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn x, y, w, hdef convert_annotation(image_id, path): #输入输出文件夹,根据实际情况进行修改in_file = open('DIOR/Annotations/%s.xml' % (image_id), encoding='UTF-8')out_file = open('DIOR_dataset/labels/' + path + '/%s.txt' % (image_id), 'w')tree = ET.parse(in_file)root = tree.getroot()size = root.find('size')w = int(size.find('width').text)h = int(size.find('height').text)for obj in root.iter('object'):#difficult = obj.find('difficult').text#difficult = obj.find('Difficult').textcls = obj.find('name').textif cls not in classes:continuecls_id = classes.index(cls)xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),float(xmlbox.find('ymax').text))b1, b2, b3, b4 = b# 标注越界修正if b2 > w:b2 = wif b4 > h:b4 = hb = (b1, b2, b3, b4)bb = convert((w, h), b)out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')train_percent = 0.6 test_percent = 0.2 val_percent = 0.2xmlfilepath = opt.xml_path # txtsavepath = opt.txt_path total_xml = os.listdir(xmlfilepath) # if not os.path.exists(txtsavepath): # os.makedirs(txtsavepath)num = len(total_xml) list_index = range(num) list_index = list(list_index) random.shuffle(list_index)train_nums = list_index[:int(num * train_percent)] test_nums = list_index[int(num * train_percent): int(num * test_percent)+int(num * train_percent)] val_nums = list_index[int(num * test_percent)+int(num * train_percent):]for i in list_index:name = total_xml[i][:-4]if i in train_nums:convert_annotation(name, 'train') # lablesimage_origin_path = 'DIOR/JPEGImages/' + name + '.jpg'image_target_path = 'DIOR_dataset/images/train/' + name + '.jpg'copyfile(image_origin_path, image_target_path)if i in test_nums:convert_annotation(name, 'test') # lablesimage_origin_path = 'DIOR/JPEGImages/' + name + '.jpg'image_target_path = 'DIOR_dataset/images/test/' + name + '.jpg'copyfile(image_origin_path, image_target_path)if i in val_nums:convert_annotation(name, 'val') # lablesimage_origin_path = 'DIOR/JPEGImages/' + name + '.jpg'image_target_path = 'DIOR_dataset/images/val/' + name + '.jpg'copyfile(image_origin_path, image_target_path)最后生成文件如图所示:
。
这篇关于DIOR数据集xml转txt格式并划分训练集测试集验证集(用于yolo)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!