网站建设管理ppt,高新建设网站电话,wordpress店铺模板,wordpress建立cms文章目录 简介1.数据集格式1.1数据集目录格式对比1.2标签格式对比 2.格式转换脚本3.文件处理脚本 简介
将voc2012中xml格式的标签转为yolov8中txt格式将转换后的图像和标签按照yolov8训练的要求整理为对应的目录结构
1.数据集格式
1.1数据集目录格式对比
#xff08;11VOC2012的数据集文件目录如下 2YOLOv8需要的文件目录 同时需要生成关于训练集、验证集和测试集图像目录的txt文件最好是绝对路径
1.2标签格式对比
1voc数据集标签 2YOLO数据集标签 每一行代表一个目标框的信息{class_index} {x_center} {y_center} {width} {height}
2.格式转换脚本
修改脚本中文件目录然后运行
python3 trans_voc_yolo.py# -*- coding: utf-8 -*-
# 在脚本中你需要将voc_labels_folder和output_folder两个变量设置为正确的路径
# 分别是VOC2012数据集的XML标签文件夹路径和转换后的YOLO格式标签文件夹路径。同时你还需要根据VOC2012数据集的类别列表自定义class_names变量的内容。
# 执行脚本后它会遍历VOC2012数据集的XML标签文件夹中的每个XML文件解析其中的目标实例信息并将它们转换为YOLO格式的txt标签文件。
# 转换后的txt文件将保存在指定的输出文件夹中每个txt文件对应相应的XML文件。
# 请确保脚本中的文件路径正确并提前创建好输出文件夹。运行脚本后你会在输出文件夹中得到与VOC2012数据集中的每个XML标签文件对应的YOLO格式txt标签文件。import xml.etree.ElementTree as ET
import osvoc_labels_folder Annotations/ # VOC2012的XML标签文件夹路径
output_folder yolo_labels/ # 转换后的YOLO格式标签文件夹路径
class_names [aeroplane, bicycle, bird, boat, bottle, bus, car, cat, chair, cow, diningtable,dog, horse, motorbike, person, pottedplant, sheep, sofa, train, tvmonitor] # 类别名称列表if not os.path.exists(output_folder):os.makedirs(output_folder)for xml_file in os.listdir(voc_labels_folder):tree ET.parse(os.path.join(voc_labels_folder, xml_file))root tree.getroot()image_width int(root.find(size/width).text)image_height int(root.find(size/height).text)txt_file xml_file.replace(.xml, .txt)txt_path os.path.join(output_folder, txt_file)with open(txt_path, w) as f:for obj in root.findall(object):class_name obj.find(name).textclass_index class_names.index(class_name)bbox obj.find(bndbox)x_min int(float(bbox.find(xmin).text))y_min int(float(bbox.find(ymin).text))x_max int(float(bbox.find(xmax).text))y_max int(float(bbox.find(ymax).text))x_center (x_min x_max) / (2 * image_width)y_center (y_min y_max) / (2 * image_height)width (x_max - x_min) / image_widthheight (y_max - y_min) / image_heightf.write(f{class_index} {x_center} {y_center} {width} {height}\n)3.文件处理脚本
将数据集按照7:2:1的比例划分为训练集、验证集和测试集并生成相应的目录
python3 split_train_val_test.py# -*- coding: utf-8 -*-import os
import random
import shutil# 设置文件路径和划分比例
root_path /home/lusx/data/voc_yolo/
image_dir JPEGImages/
label_dir labels_sum/
train_ratio 0.7
val_ratio 0.2
test_ratio 0.1# 创建训练集、验证集和测试集目录
os.makedirs(images/train, exist_okTrue)
os.makedirs(images/val, exist_okTrue)
os.makedirs(images/test, exist_okTrue)
os.makedirs(labels/train, exist_okTrue)
os.makedirs(labels/val, exist_okTrue)
os.makedirs(labels/test, exist_okTrue)# 获取所有图像文件名
image_files os.listdir(image_dir)
total_images len(image_files)
random.shuffle(image_files)# 计算划分数量
train_count int(total_images * train_ratio)
val_count int(total_images * val_ratio)
test_count total_images - train_count - val_count# 划分训练集
train_images image_files[:train_count]
for image_file in train_images:label_file image_file[:image_file.rfind(.)] .txtshutil.copy(os.path.join(image_dir, image_file), images/train/)shutil.copy(os.path.join(label_dir, label_file), labels/train/)# 划分验证集
val_images image_files[train_count:train_countval_count]
for image_file in val_images:label_file image_file[:image_file.rfind(.)] .txtshutil.copy(os.path.join(image_dir, image_file), images/val/)shutil.copy(os.path.join(label_dir, label_file), labels/val/)# 划分测试集
test_images image_files[train_countval_count:]
for image_file in test_images:label_file image_file[:image_file.rfind(.)] .txtshutil.copy(os.path.join(image_dir, image_file), images/test/)shutil.copy(os.path.join(label_dir, label_file), labels/test/)# 生成训练集图片路径txt文件
with open(train.txt, w) as file:file.write(\n.join([root_path images/train/ image_file for image_file in train_images]))# 生成验证集图片路径txt文件
with open(val.txt, w) as file:file.write(\n.join([root_path images/val/ image_file for image_file in val_images]))# 生成测试集图片路径txt文件
with open(test.txt, w) as file:file.write(\n.join([root_path images/test/ image_file for image_file in test_images]))print(数据划分完成)