python:VOC格式数据集转换为YOLO数据集格式

news2024/9/30 1:28:01

作者:CSDN @ _养乐多_

本文将介绍如何将目标检测中常用的VOC格式数据集转换为YOLO数据集,并进行数据集比例划分,从而方便的进行YOLO目标检测。

如果不想分两步,可以直接看第三节代码。


文章目录

      • 一、将VOC格式数据集转换为YOLO格式数据集
      • 二、YOLO格式数据集划分(训练、验证、测试)
      • 三、一步到位


一、将VOC格式数据集转换为YOLO格式数据集

执行以下脚本将VOC格式数据集转换为YOLO格式数据集。
但是需要注意的是:

  1. 转换之后的数据集只有Images和labels两个文件。还需要执行第二节中的脚本进行数据集划分,将总的数据集划分为训练、验证、测试数据集;
  2. 使用的话,需要修改 class_mapping 中类别名和对应标签,还有VOC数据集路径、YOLO数据集路径。
import os
import shutil
import xml.etree.ElementTree as ET

# VOC格式数据集路径
voc_data_path = 'E:\\DataSet\\helmet-VOC'
voc_annotations_path = os.path.join(voc_data_path, 'Annotations')
voc_images_path = os.path.join(voc_data_path, 'JPEGImages')

# YOLO格式数据集保存路径
yolo_data_path = 'E:\\DataSet\\helmet-YOLO'
yolo_images_path = os.path.join(yolo_data_path, 'images')
yolo_labels_path = os.path.join(yolo_data_path, 'labels')

# 创建YOLO格式数据集目录
os.makedirs(yolo_images_path, exist_ok=True)
os.makedirs(yolo_labels_path, exist_ok=True)

# 类别映射 (可以根据自己的数据集进行调整)
class_mapping = {
    'head': 0,
    'helmet': 1,
    'person': 2,
    # 添加更多类别...
}

def convert_voc_to_yolo(voc_annotation_file, yolo_label_file):
    tree = ET.parse(voc_annotation_file)
    root = tree.getroot()

    size = root.find('size')
    width = float(size.find('width').text)
    height = float(size.find('height').text)

    with open(yolo_label_file, 'w') as f:
        for obj in root.findall('object'):
            cls = obj.find('name').text
            if cls not in class_mapping:
                continue
            cls_id = class_mapping[cls]
            xmlbox = obj.find('bndbox')
            xmin = float(xmlbox.find('xmin').text)
            ymin = float(xmlbox.find('ymin').text)
            xmax = float(xmlbox.find('xmax').text)
            ymax = float(xmlbox.find('ymax').text)

            x_center = (xmin + xmax) / 2.0 / width
            y_center = (ymin + ymax) / 2.0 / height
            w = (xmax - xmin) / width
            h = (ymax - ymin) / height

            f.write(f"{cls_id} {x_center} {y_center} {w} {h}\n")

# 遍历VOC数据集的Annotations目录,进行转换
for voc_annotation in os.listdir(voc_annotations_path):
    if voc_annotation.endswith('.xml'):
        voc_annotation_file = os.path.join(voc_annotations_path, voc_annotation)
        image_id = os.path.splitext(voc_annotation)[0]
        voc_image_file = os.path.join(voc_images_path, f"{image_id}.jpg")
        yolo_label_file = os.path.join(yolo_labels_path, f"{image_id}.txt")
        yolo_image_file = os.path.join(yolo_images_path, f"{image_id}.jpg")

        convert_voc_to_yolo(voc_annotation_file, yolo_label_file)
        if os.path.exists(voc_image_file):
            shutil.copy(voc_image_file, yolo_image_file)

print("转换完成!")

二、YOLO格式数据集划分(训练、验证、测试)

随机将数据集按照0.7-0.2-0.1比例划分为训练、验证、测试数据集。
注意,修改代码中图片的后缀,如果是.jpg,就把.png修改为.jpg。

最终结果,

在这里插入图片描述

import os
import shutil
import random

# YOLO格式数据集保存路径
yolo_images_path1 = 'E:\\DataSet\\helmet-VOC'
yolo_labels_path1 = 'E:\\DataSet\\helmet-YOLO'
yolo_data_path = yolo_labels_path1

yolo_images_path = os.path.join(yolo_images_path1, 'JPEGImages')
yolo_labels_path = os.path.join(yolo_labels_path1, 'labels')

# 创建划分后的目录结构
train_images_path = os.path.join(yolo_data_path, 'train', 'images')
train_labels_path = os.path.join(yolo_data_path, 'train', 'labels')
val_images_path = os.path.join(yolo_data_path, 'val', 'images')
val_labels_path = os.path.join(yolo_data_path, 'val', 'labels')
test_images_path = os.path.join(yolo_data_path, 'test', 'images')
test_labels_path = os.path.join(yolo_data_path, 'test', 'labels')

os.makedirs(train_images_path, exist_ok=True)
os.makedirs(train_labels_path, exist_ok=True)
os.makedirs(val_images_path, exist_ok=True)
os.makedirs(val_labels_path, exist_ok=True)
os.makedirs(test_images_path, exist_ok=True)
os.makedirs(test_labels_path, exist_ok=True)

# 获取所有图片文件名(不包含扩展名)
image_files = [f[:-4] for f in os.listdir(yolo_images_path) if f.endswith('.png')]

# 随机打乱文件顺序
random.shuffle(image_files)

# 划分数据集比例
train_ratio = 0.7
val_ratio = 0.2
test_ratio = 0.1

train_count = int(train_ratio * len(image_files))
val_count = int(val_ratio * len(image_files))
test_count = len(image_files) - train_count - val_count

train_files = image_files[:train_count]
val_files = image_files[train_count:train_count + val_count]
test_files = image_files[train_count + val_count:]

# 移动文件到相应的目录
def move_files(files, src_images_path, src_labels_path, dst_images_path, dst_labels_path):
    for file in files:
        src_image_file = os.path.join(src_images_path, f"{file}.png")
        src_label_file = os.path.join(src_labels_path, f"{file}.txt")
        dst_image_file = os.path.join(dst_images_path, f"{file}.png")
        dst_label_file = os.path.join(dst_labels_path, f"{file}.txt")

        if os.path.exists(src_image_file) and os.path.exists(src_label_file):
            shutil.move(src_image_file, dst_image_file)
            shutil.move(src_label_file, dst_label_file)

# 移动训练集文件
move_files(train_files, yolo_images_path, yolo_labels_path, train_images_path, train_labels_path)
# 移动验证集文件
move_files(val_files, yolo_images_path, yolo_labels_path, val_images_path, val_labels_path)
# 移动测试集文件
move_files(test_files, yolo_images_path, yolo_labels_path, test_images_path, test_labels_path)

print("数据集划分完成!")

三、一步到位

如果不想分两步进行格式转换,那么以下脚本结合了以上两步,直接得到最后按比例划分训练、验证、测试的数据集结果。

在这里插入图片描述

注意:需要修改 voc_data_path ,yolo_data_path ,class_mapping 以及 ‘.png’ 后缀。

import os
import shutil
import random
import xml.etree.ElementTree as ET
from tqdm import tqdm

# VOC格式数据集路径
voc_data_path = 'E:\\DataSet-VOC'
voc_annotations_path = os.path.join(voc_data_path, 'Annotations')
voc_images_path = os.path.join(voc_data_path, 'JPEGImages')

# YOLO格式数据集保存路径
yolo_data_path = 'E:\\DataSet-YOLO'
yolo_images_path = os.path.join(yolo_data_path, 'images')
yolo_labels_path = os.path.join(yolo_data_path, 'labels')

# 创建YOLO格式数据集目录
os.makedirs(yolo_images_path, exist_ok=True)
os.makedirs(yolo_labels_path, exist_ok=True)

# 类别映射 (可以根据自己的数据集进行调整)
class_mapping = {
    'head': 0,
    'helmet': 1,
    'person': 2,
    # 添加更多类别...
}

def convert_voc_to_yolo(voc_annotation_file, yolo_label_file):
    tree = ET.parse(voc_annotation_file)
    root = tree.getroot()

    size = root.find('size')
    width = float(size.find('width').text)
    height = float(size.find('height').text)

    with open(yolo_label_file, 'w') as f:
        for obj in root.findall('object'):
            cls = obj.find('name').text
            if cls not in class_mapping:
                continue
            cls_id = class_mapping[cls]
            xmlbox = obj.find('bndbox')
            xmin = float(xmlbox.find('xmin').text)
            ymin = float(xmlbox.find('ymin').text)
            xmax = float(xmlbox.find('xmax').text)
            ymax = float(xmlbox.find('ymax').text)

            x_center = (xmin + xmax) / 2.0 / width
            y_center = (ymin + ymax) / 2.0 / height
            w = (xmax - xmin) / width
            h = (ymax - ymin) / height

            f.write(f"{cls_id} {x_center} {y_center} {w} {h}\n")

# 遍历VOC数据集的Annotations目录,进行转换
print("开始VOC到YOLO格式转换...")
for voc_annotation in tqdm(os.listdir(voc_annotations_path)):
    if voc_annotation.endswith('.xml'):
        voc_annotation_file = os.path.join(voc_annotations_path, voc_annotation)
        image_id = os.path.splitext(voc_annotation)[0]
        voc_image_file = os.path.join(voc_images_path, f"{image_id}.png")
        yolo_label_file = os.path.join(yolo_labels_path, f"{image_id}.txt")
        yolo_image_file = os.path.join(yolo_images_path, f"{image_id}.png")

        convert_voc_to_yolo(voc_annotation_file, yolo_label_file)
        if os.path.exists(voc_image_file):
            shutil.copy(voc_image_file, yolo_image_file)

print("VOC到YOLO格式转换完成!")

# 划分数据集
train_images_path = os.path.join(yolo_data_path, 'train', 'images')
train_labels_path = os.path.join(yolo_data_path, 'train', 'labels')
val_images_path = os.path.join(yolo_data_path, 'val', 'images')
val_labels_path = os.path.join(yolo_data_path, 'val', 'labels')
test_images_path = os.path.join(yolo_data_path, 'test', 'images')
test_labels_path = os.path.join(yolo_data_path, 'test', 'labels')

os.makedirs(train_images_path, exist_ok=True)
os.makedirs(train_labels_path, exist_ok=True)
os.makedirs(val_images_path, exist_ok=True)
os.makedirs(val_labels_path, exist_ok=True)
os.makedirs(test_images_path, exist_ok=True)
os.makedirs(test_labels_path, exist_ok=True)

# 获取所有图片文件名(不包含扩展名)
image_files = [f[:-4] for f in os.listdir(yolo_images_path) if f.endswith('.png')]

# 随机打乱文件顺序
random.shuffle(image_files)

# 划分数据集比例
train_ratio = 0.7
val_ratio = 0.2
test_ratio = 0.1

train_count = int(train_ratio * len(image_files))
val_count = int(val_ratio * len(image_files))
test_count = len(image_files) - train_count - val_count

train_files = image_files[:train_count]
val_files = image_files[train_count:train_count + val_count]
test_files = image_files[train_count + val_count:]

# 移动文件到相应的目录
def move_files(files, src_images_path, src_labels_path, dst_images_path, dst_labels_path):
    for file in tqdm(files):
        src_image_file = os.path.join(src_images_path, f"{file}.png")
        src_label_file = os.path.join(src_labels_path, f"{file}.txt")
        dst_image_file = os.path.join(dst_images_path, f"{file}.png")
        dst_label_file = os.path.join(dst_labels_path, f"{file}.txt")

        if os.path.exists(src_image_file) and os.path.exists(src_label_file):
            shutil.move(src_image_file, dst_image_file)
            shutil.move(src_label_file, dst_label_file)

# 移动训练集文件
print("移动训练集文件...")
move_files(train_files, yolo_images_path, yolo_labels_path, train_images_path, train_labels_path)
# 移动验证集文件
print("移动验证集文件...")
move_files(val_files, yolo_images_path, yolo_labels_path, val_images_path, val_labels_path)
# 移动测试集文件
print("移动测试集文件...")
move_files(test_files, yolo_images_path, yolo_labels_path, test_images_path, test_labels_path)

print("数据集划分完成!")

# 删除原始的 images 和 labels 文件夹
shutil.rmtree(yolo_images_path)
shutil.rmtree(yolo_labels_path)

print("原始 images 和 labels 文件夹删除完成!")

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1991679.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

司美格鲁肽,又名索玛鲁肽;Semaglutide;CAS号:910463-68-2

司美格鲁肽,又名索玛鲁肽;Semaglutide; CAS号:910463-68-2 分子量:4113.58 结构图: 司美格鲁肽,又名Semaglutide (上海楚肽生物科技有限提供) 分子式:C187H291N45O59 …

nginx和php工具的使用

一、本地主机通过域名访问自己写的网页 1、开启phpstudy 2、找到phpstudy目录下的www文件夹,创建less01文件夹、index.html、web.php文件,进行配置,如下图: 3、重启一下phpstudy,然后访问网页 4、上面只能通过文件目录…

MIMO系统中差分空间调制解调matlab误码率仿真

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 4.1 空间调制原理 4.2 发送端模型 4.3 接收端模型 5.算法完整程序工程 1.算法运行效果图预览 (完整程序运行后无水印) 2.算法运行软件版本 matlab2022a 3.部分核心程序 (完…

打开法学著作的AI新方式:元典问达「知识+」正式上线!

号外!号外! 元典问达正式上新 「知识」 法律人现在可以在「知识」板块,直接与《刑法注释书》、《刑事诉讼法注释书》“对话”,通过提问,获得权威法学出版物总结而成的刑事法律解读和案例分析! 信息爆炸时…

esp32使用数码管显示数字

前言:本文参考视频链接放在最后,同时本文为学习笔记,为了自己好复习。 #数码管概念 现在显示流行有 LCD OLED 但是数码管也能用来作为显示,相对于前两者,数码管稳定价格低廉,到现在也在被应用&#xff0…

UE4材质基础---方形渐变、中心渐变

1、方形渐变 效果图: 代码: 解析:TexCoord的纹理坐标的R通道(0,0.5,1)减0.5> (-0.5,0,0.5)取abs> (0.5,0,0.5)乘…

b站ip地址怎么改到别的城市去

对于B站的忠实用户而言,每当在个人主页、发布动态或评论等,都会注意到IP属地的显示,但在某些情况下,出于对个人隐私的保护、跨越地域限制或其他个性化需求,我们可能会想要改变B站显示的IP地址到另一个城市。那么&#…

有没有值得推荐的加密软件

1. 金刚钻信息数据防泄密系统 特点:专为企业设计,提供全面的信息安全管理功能,支持透明加密技术,确保敏感文件在创建、编辑和保存过程中自动加密,不影响正常办公流程。 功能:提供文档权限管理、数据备份与…

ctfhub Bypass disable_function(完结)

LD_PRELOAD url 蚁剑连接 选择插件 点击开始 查看到此文件名编辑连接拼接到url后面重新连接 点击开启终端 在终端执行命令 ls / /readfile ShellShock url CTFHub 环境实例 | 提示信息 蚁剑连接 写入shell.php <?phpeval($_REQUEST[ant]);putenv("PHP_test() { :…

无心剑七绝《陈梦夺冠》

七绝陈梦夺冠 陈言务去志高扬 梦舞巴黎万里香 夺取天机心气壮 冠轻国重醉千觞 2024年8月8日 平水韵七阳平韵 这首七绝是一首藏头诗&#xff0c;每句的首字连起来是“陈梦夺冠”&#xff0c;表达了对陈梦在巴黎奥运乒乓女单斩获冠军的赞美。 陈言务去志高扬&#xff1a;这句诗意…

【生成式人工智能-五-大模型的修炼-阶段三RLHF】

大模型的修炼-RLHF 增强式学习 大模型修炼阶段Instruct Fine-tune 和 RLHF 的区别和联系 回馈模型 Reward Model增强式学习的难题怎么定义什么是好的&#xff1f;人类也无法判定好坏的 大模型是如何具备人工智能的呢&#xff1f; 上面一篇文章介绍到了前两个阶段&#xff0c;接…

网页 生成桌面快捷应用 manifest.json

效果如图 代码 <link rel"manifest" href"./manifest.json" />// manifest.json {"name": "讨口子","short_name": "TKZ","start_url": "/","display": "standalo…

奔驰GLS450升级迈巴赫四座版内饰 后排电动遮阳帘 后排娱乐案例

多座互联娱乐系统 独乐乐,不如众乐乐。 同级独有的多座互联提乐系统,可令车内所有乘客通过7.0美对中央扶手触控屏与双11.6英寸后排提乐系统缺控屏,随心所欲地分享号航、视频、音频等内容,即便身处后排,您同样也可以享受完M的MBUX智能人都交互体验,直接控制车M的全种功能。 奔驰…

The Otherworld《我独自升级》活动来了!

最近&#xff0c;我们迎来了韩国初创公司 Otherworld&#xff0c;加入 The Sandbox 大家庭。这次合作建立了一个元宇宙网络动漫中心&#xff0c;以 KakaoPage 的热门 IP 为基础&#xff0c;为我们的玩家和创作者在 The Sandbox 中提供多样化的体验。我们将推出一个全新的活动&a…

pikachu: unsafe filedownload(文件下载)

是一个图片下载页面&#xff0c;随便下载一张图片&#xff0c;查看下载链接发现是 http://127.0.0.1:8001/vul/unsafedownload/execdownload.php?filenamekb.png 修改拼接 URL&#xff0c; 构造想要传的的路径来对其进行文件上传 http://127.0.0.1/pikachu-master/vul/unsa…

芯片底部填充工艺流程有哪些?

芯片底部填充工艺流程有哪些&#xff1f;底部填充工艺&#xff08;Underfill&#xff09;是一种在电子封装过程中广泛使用的技术&#xff0c;主要用于增强倒装芯片&#xff08;Flip Chip&#xff09;、球栅阵列&#xff08;BGA&#xff09;、芯片级封装&#xff08;CSP&#xf…

多久没有清理你的电脑磁盘了?轻松解锁免费轻量磁盘清理工具

随着我们日常使用电脑的时间越来越长&#xff0c;磁盘上积累的无用文件和垃圾数据也越来越多。这些文件不仅占用宝贵的存储空间&#xff0c;还可能拖慢电脑的运行速度。 那么&#xff0c;你多久没有清理过你的电脑磁盘了呢&#xff1f; 今天&#xff0c;我将为大家推荐几款免…

低代码平台:效率利器还是质量妥协?

目录 低代码平台&#xff1a;效率利器还是质量妥协&#xff1f; 一、引言 二、低代码平台的定义和背景 1、什么是低代码平台&#xff1f; 2、低代码平台的兴起 三、低代码开发的机遇 1、提高开发效率 2、降低开发成本 3、赋能业务人员 四、低代码开发的挑战 1、质量…

Midjourney V6.1更新 | 细节狂魔,绝美人像(附提示词)

前言 Midjourney V6.1版本&#xff0c;堪称细节狂魔&#xff0c;在人像上简直登峰造极&#xff01; 自V6.1版本更新以来我一次次被Midjourney生成的人像震惊到&#xff01;用Midjourney官网分享的提示词微调&#xff0c;生成图像&#xff0c;每一张都绝美&#xff0c;晚上玩到…