当前位置:   article > 正文

将VOC2012格式的数据集转为YOLOV8格式_ilsvrc2012转换yolo格式

ilsvrc2012转换yolo格式

简介

  1. 将voc2012中xml格式的标签转为yolov8中txt格式
  2. 将转换后的图像和标签按照yolov8训练的要求整理为对应的目录结构

1.数据集格式

1.1数据集目录格式对比

(1)VOC2012的数据集文件目录如下:
在这里插入图片描述
(2)YOLOv8需要的文件目录
在这里插入图片描述
同时需要生成关于训练集、验证集和测试集图像目录的txt文件,最好是绝对路径
在这里插入图片描述
在这里插入图片描述

1.2标签格式对比

(1)voc数据集标签
在这里插入图片描述
(2)YOLO数据集标签
每一行代表一个目标框的信息:{class_index} {x_center} {y_center} {width} {height}
在这里插入图片描述

2.格式转换脚本

修改脚本中文件目录,然后运行:

python3 trans_voc_yolo.py
  • 1
# -*- 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 os

voc_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').text
            class_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_width
            height = (y_max - y_min) / image_height

            f.write(f'{class_index} {x_center} {y_center} {width} {height}\n')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

3.文件处理脚本

将数据集按照7:2:1的比例划分为训练集、验证集和测试集,并生成相应的目录

python3 split_train_val_test.py
  • 1
# -*- 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_ok=True)
os.makedirs("images/val", exist_ok=True)
os.makedirs("images/test", exist_ok=True)
os.makedirs("labels/train", exist_ok=True)
os.makedirs("labels/val", exist_ok=True)
os.makedirs("labels/test", exist_ok=True)

# 获取所有图像文件名
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(".")] + ".txt"
    shutil.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_count+val_count]
for image_file in val_images:
    label_file = image_file[:image_file.rfind(".")] + ".txt"
    shutil.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_count+val_count:]
for image_file in test_images:
    label_file = image_file[:image_file.rfind(".")] + ".txt"
    shutil.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("数据划分完成!")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/780645
推荐阅读
相关标签
  

闽ICP备14008679号