数据集 VOC转YOLO格式

news/2024/10/23 21:38:50/

一、xml转换为txt

import os.path
import xml.etree.ElementTree as ET
import os
import random
# class_names = ['palm', 'stone', 'scissor', 'awesome', 'heartB', 'OK', 'ROCK', 'one', 'swear', 'thanks', 'heartA',
#                'heartC', 'good', 'bad', 'pray', 'call', 'take_picture', 'salute']
class_names = ['menopause', 'hairball', 'broken yarn', 'hole','stains']
xmlpath = 'F:/Project_code/yolov7-main/VOCdevkit/VOC2007/Annotations/'  # 原xml路径
txtpath = 'F:/Project_code/yolov7-main/VOCdevkit/VOC2007/labels_copy/'  # 转换后txt文件存放路径
if not os.path.exists(txtpath):os.makedirs(txtpath)
files = []for root, dirs, files in os.walk(xmlpath):Nonenumber = len(files)
print(number)
i = 0
while i < number:name = files[i][0:-4]xml_name = name + ".xml"txt_name = name + ".txt"xml_file_name = xmlpath + xml_nametxt_file_name = txtpath + txt_namexml_file = open(xml_file_name)tree = ET.parse(xml_file)root = tree.getroot()filename = root.find('filename').textimage_name = root.find('filename').textw = int(root.find('size').find('width').text)h = int(root.find('size').find('height').text)f_txt = open(txt_file_name, 'w+')content = ""first = Truefor obj in root.iter('object'):name = obj.find('name').textclass_num = class_names.index(name)xmlbox = obj.find('bndbox')x1 = int(xmlbox.find('xmin').text)x2 = int(xmlbox.find('xmax').text)y1 = int(xmlbox.find('ymin').text)y2 = int(xmlbox.find('ymax').text)if first:content += str(class_num) + " " + \str((x1 + x2) / 2 / w) + " " + str((y1 + y2) / 2 / h) + " " + \str((x2 - x1) / w) + " " + str((y2 - y1) / h)first = Falseelse:content += "\n" + \str(class_num) + " " + \str((x1 + x2) / 2 / w) + " " + str((y1 + y2) / 2 / h) + " " + \str((x2 - x1) / w) + " " + str((y2 - y1) / h)# print(str(i / (number - 1) * 100) + "%\n")print(content)f_txt.write(content)f_txt.close()xml_file.close()i += 1print("done!")

二、数据集划分

划分为训练集、验证集和测试集

import os, shutil
from sklearn.model_selection import train_test_splitval_size = 0.1
test_size = 0.2
postfix = 'jpg'
imgpath = 'E:/pycharm/yolov7/yolov7/data/hongwai/images/train'
txtpath = 'E:/pycharm/yolov7/yolov7/data/hongwai/labels/'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)listdir = os.listdir(txtpath)
train, test = train_test_split(listdir, test_size=test_size, shuffle=True, random_state=0)
train, val = train_test_split(train, test_size=val_size, shuffle=True, random_state=0)for i in train:shutil.copy('{}/{}.{}'.format(imgpath, i[:-4], postfix), 'Images/train/{}.{}'.format(i[:-4], postfix))shutil.copy('{}/{}'.format(txtpath, i), 'labels/train/{}'.format(i))for i in val:shutil.copy('{}/{}.{}'.format(imgpath, i[:-4], postfix), 'Images/val/{}.{}'.format(i[:-4], postfix))shutil.copy('{}/{}'.format(txtpath, i), 'labels/val/{}'.format(i))for i in test:shutil.copy('{}/{}.{}'.format(imgpath, i[:-4], postfix), 'Images/test/{}.{}'.format(i[:-4], postfix))shutil.copy('{}/{}'.format(txtpath, i), 'labels/test/{}'.format(i))

三、复制到data文件夹下

手动复制
在这里插入图片描述


http://www.ppmy.cn/news/797073.html

相关文章

web 页面布局:(三)position 坐标系布局

web 页面布局&#xff1a;&#xff08;三&#xff09;position 坐标系布局 页面坐标系position 设置fixedrelativeabsolutesticky 应用场景 页面坐标系 因为所有文本内容&#xff0c;是从左向右读&#xff0c;自上而下读&#xff0c;和书写习惯&#xff0c;读书习惯一致&#…

小米路由器局域网设备ping不通

问题 两台电脑在同一个局域网内&#xff0c;都连接上小米路由器&#xff0c;我发现电脑部署的服务局域网设备都访问不到&#xff0c;甚至ping不到&#xff0c;排除了防火墙问题&#xff0c;最终发现是路由器一个设置导致的。 解决 将原来的混合加密&#xff0c;更换为强加密…

opencv报错 ValueError: too many values to unpack (expected 2)

改为&#xff1a; binary,contours,hierarch cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 原代码&#xff1a;contours, _ cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 在pycharm上运行没问题&#xff0c;在树霉派上会…

21-注册中心与配置中心Nacos

已经使用过了Spring cloud提供的Geteway、openFeign。 1、注册中心与配置中心 1.1、注册中心 相当于通讯录,让应用之间相互认识。 用途: 实例的健康检查。 路由转发:为了控制成本,会对机器做动态扩容,此时IP就不固定了。 远程调用。 1.2、配置中心 动态修改线上的配…

l7sa008b故障代码_华硕主板故障维代码指南

华硕主板根据故障代码维修指南 来源 : 电脑软硬件应用网 作者 : 网络收集 更新时间 : 12-29 点击 : 15 次 ( 1 ) POWER,POWEROK,RESET,CLK,REQ#,A20M#,M/IO# 信号错误; ( 2 ) HA,HD,AD,SA 信号错误; ( 3 )插 PCI 卡引起 00 ,去掉 PCI 卡 OK: PCI 总线 GNT 开路 ; ( 4 ) 地址…

阿里云轻量应用服务器和云服务器ECS有什么区别?

阿里云服务器ECS和轻量应用服务器有什么区别&#xff1f;云服务器ECS是明星级云服务器&#xff0c;轻量应用服务器可以理解为简化版的云服务器ECS&#xff0c;轻量适用于单机应用&#xff0c;云服务器ECS适用于集群类高可用高容灾应用&#xff0c;阿里云百科来详细说下阿里云轻…

linux 文件管理命令:rmdir \ chattr \ cksum \ cmp \ split

文章目录 rmdir&#xff1a;删除目录案例练习 chattr&#xff1a;改变文件的属性案例练习 cksum&#xff1a;文件的 CRC 校验案例练习 cmp&#xff1a;比较文件差异案例练习 split&#xff1a;分割文件案例练习 rmdir&#xff1a;删除目录 作用&#xff1a;删除一个或者多个目…

AMD FreeSync显示器上市,这是要把G-Sync虐成渣了

玩家玩个游戏也真不容易&#xff0c;配置低的怕卡顿&#xff0c;配置高了帧数漂亮&#xff0c;但又怕画面撕裂&#xff0c;开垂直同步倒是可以解决部分问题&#xff0c;但帧数限制死了又让人觉得很不爽。对于这个问题&#xff0c;NVIDIA 2013年10月份推出了G-Sync技术&#xff…