在用darknet训练voc数据集时,需要将xml格式的标签转换为txt格式的标签。
同时,用自定义数据集在darknet中进行训练时,如遇到xml格式转txt格式的问题,也可用本文方法。
废话不多,开始介绍。

  1. 新建文件夹VOCdevkit,文件结构为:
├── gen_files.py
└── VOCdevkit└── VOC2007├── Annotations├── ImageSets├── JPEGImages└── labels

按照以上结构来建文件夹,并保持名字与上一致。
Annotations用来存放xml格式的标注文件;
JPEGImages存放图片数据集;
labels存放转换后的txt标注文件,目前是空文件夹。

  1. gen_files.py放至与VOCdevkit文件夹同级目录,代码内容如下:
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import randomclasses=["class1","class2"]def clear_hidden_files(path):dir_list = os.listdir(path)for i in dir_list:abspath = os.path.join(os.path.abspath(path), i)if os.path.isfile(abspath):if i.startswith("._"):os.remove(abspath)else:clear_hidden_files(abspath)def convert(size, box):dw = 1./size[0]dh = 1./size[1]x = (box[0] + box[1])/2.0y = (box[2] + box[3])/2.0w = box[1] - box[0]h = box[3] - box[2]x = x*dww = w*dwy = y*dhh = h*dhreturn (x,y,w,h)def convert_annotation(image_id):in_file = open('VOCdevkit/VOC2007/Annotations/%s.xml' %image_id)out_file = open('VOCdevkit/VOC2007/labels/%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').textcls = obj.find('name').textif cls not in classes or int(difficult) == 1: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))bb = convert((w,h), b)out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')in_file.close()out_file.close()wd = os.getcwd()
wd = os.getcwd()
work_sapce_dir = os.path.join(wd, "VOCdevkit/")
if not os.path.isdir(work_sapce_dir):os.mkdir(work_sapce_dir)
work_sapce_dir = os.path.join(work_sapce_dir, "VOC2007/")
if not os.path.isdir(work_sapce_dir):os.mkdir(work_sapce_dir)
annotation_dir = os.path.join(work_sapce_dir, "Annotations/")
if not os.path.isdir(annotation_dir):os.mkdir(annotation_dir)
clear_hidden_files(annotation_dir)
image_dir = os.path.join(work_sapce_dir, "JPEGImages/")
if not os.path.isdir(image_dir):os.mkdir(image_dir)
clear_hidden_files(image_dir)
VOC_file_dir = os.path.join(work_sapce_dir, "ImageSets/")
if not os.path.isdir(VOC_file_dir):os.mkdir(VOC_file_dir)
VOC_file_dir = os.path.join(VOC_file_dir, "Main/")
if not os.path.isdir(VOC_file_dir):os.mkdir(VOC_file_dir)train_file = open(os.path.join(wd, "2007_train.txt"), 'w')
test_file = open(os.path.join(wd, "2007_test.txt"), 'w')
train_file.close()
test_file.close()
VOC_train_file = open(os.path.join(work_sapce_dir, "ImageSets/Main/train.txt"), 'w')
VOC_test_file = open(os.path.join(work_sapce_dir, "ImageSets/Main/test.txt"), 'w')
VOC_train_file.close()
VOC_test_file.close()
if not os.path.exists('VOCdevkit/VOC2007/labels'):os.makedirs('VOCdevkit/VOC2007/labels')
train_file = open(os.path.join(wd, "2007_train.txt"), 'a')
test_file = open(os.path.join(wd, "2007_test.txt"), 'a')
VOC_train_file = open(os.path.join(work_sapce_dir, "ImageSets/Main/train.txt"), 'a')
VOC_test_file = open(os.path.join(work_sapce_dir, "ImageSets/Main/test.txt"), 'a')
list = os.listdir(image_dir) # list image files
probo = random.randint(1, 100)
print("Probobility: %d" % probo)
for i in range(0,len(list)):path = os.path.join(image_dir,list[i])if os.path.isfile(path):image_path = image_dir + list[i]voc_path = list[i](nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))(voc_nameWithoutExtention, voc_extention) = os.path.splitext(os.path.basename(voc_path))annotation_name = nameWithoutExtention + '.xml'annotation_path = os.path.join(annotation_dir, annotation_name)probo = random.randint(1, 100)print("Probobility: %d" % probo)if(probo < 75):if os.path.exists(annotation_path):train_file.write(image_path + '\n')VOC_train_file.write(voc_nameWithoutExtention + '\n')convert_annotation(nameWithoutExtention)else:if os.path.exists(annotation_path):test_file.write(image_path + '\n')VOC_test_file.write(voc_nameWithoutExtention + '\n')convert_annotation(nameWithoutExtention)
train_file.close()
test_file.close()
VOC_train_file.close()
VOC_test_file.close()
  1. classes=["class1","class2"]改成你要转换的数据集对应的类别即可(注意类别顺序),其他的默认即可。
  2. 运行gen_files.py,之后会在labels文件夹生成转换成功的txt文件,同时会在根目录下生成train.txt test.txt。

(日常搬砖)voc等xml格式的数据集转换为yolo可用的txt格式的数据集(亲测可用)相关推荐

  1. Pytorch机器学习(三)——VOC数据集转换为YOLO数据集

    Pytorch机器学习(三)--VOC数据集转换为YOLO数据集 目录 Pytorch机器学习(三)--VOC数据集转换为YOLO数据集 前言 一.yolo格式 二.代码 总结 前言 本文为利用pyt ...

  2. ofd格式转pdf,所需代码和jar包--亲测可用

    ofd格式转pdf,所需代码和jar包–亲测可用 工具类package com.comingnet.commons.util.file;import java.nio.file.Path; impor ...

  3. 把 Jul 8, 2020 12:00:00 AM 格式的时间转换为 2020-07-08 12:00:00 格式的时间

    /*** 把 Jul 8, 2020 12:00:00 AM 格式的时间转换为 2020-07-08 12:00:00 格式的时间** @param date Jul 8, 2020 12:00:00 ...

  4. Android 把 Jun 25, 2022 12:00:00 AM 格式的时间转换为 2022-07-08 12:00:00 格式的时间

    /*** 把 Jun 25, 2022 12:00:00 AM 格式的时间转换为 2022-07-08 12:00:00 格式的时间* @param date* @param type* @retur ...

  5. 将VOC格式标注文件转换为Yolo格式

    这篇文章主要参考博客中的代码,对原博客VOC格式数据集转yolo格式代码进行一定修改.添加注释,此外还在后面添加了我自己写的一段关于对转换后的图片和标注文件进行整理的脚本代码. 关于数据集在Yolo格 ...

  6. 如何将AVI格式的视频转换为MP4?用嗨格式试试看

    如何将AVI格式的视频转换为MP4?MP4是目前最为常见且兼容性好的视频格式之一,但有时候遇到的视频格式是AVI的,怎么才能将其转换器MP4格式呢?下面就给你分享一个好用的方法. 首先在电脑上下载并运 ...

  7. 日常搬砖 - 腾讯X5 tbs 文件预览

    源自: https://www.jianshu.com/p/b54f65d8b5a7 其实吧,网络上很多 X5的教程.我只是想写一点 我遇到的问题吧. https://x5.tencent.com/t ...

  8. Java码农日常搬砖整理 一(实现EXCEL新增)

    需求: 给用户下载EXCEL模板 ,然后用户按模板填写进行批量新增. 一,实现下载模板(文件下载) 分析:其实就是前台发送请求后,然后下载服务器内固定地址的文件: 请求方式: function exc ...

  9. Citypersons数据集转VOC标准格式(YOLO 目标检测txt格式)

    CItyscapes城市数据集包含一组不同的立体视频序列中记录来自50个不同城市的街景,高质量的进行像素级的注释.数据集下载地址(需要申请注册,通过申请才能下载)[https://www.citysc ...

  10. spring boot 日志文件配置(logback-spring.xml)亲测可用!

    问题描述:如何配置springboot项目,通过日志配置,使之输出自定义日志. 详细文章:https://blog.csdn.net/gebitan505/article/details/701421 ...

最新文章

  1. 修改上传附件大小限制方法
  2. WPF使用Linq 一行代码搞定数据绑定
  3. python爬虫必会的23个项目
  4. Python通过WMI读取主板BIOS信息
  5. 机器学习十大经典算法-KMeans
  6. 分享一款在线考试学习系统.net源码
  7. 眼镜的基础知识与挑选
  8. Oracle学习3:dual详解
  9. 为什么神经网络有偏置? 神经网络中的偏置(bias)究竟有这么用
  10. Hanoi塔(汉诺塔/梵天塔)问题
  11. BIBTeX制作参考文献 [转]
  12. Effie: 一款属于程序工作者的写作软件
  13. 百度提供的LBS服务
  14. 【深度学习】池化 (pooling)
  15. css中float问题,列表中的css float问题
  16. 绝对路径! 报错:[gazebo-2] process has died [pid 2382, exit code 134
  17. 解决双显卡无法安装Ubuntu问题(转载)
  18. html 奇数div,使用奇数偶数类的Javascript DOM操作
  19. 服务器1075错误服务修复,错误1075:依存服务不存在,或已标记为删除
  20. 【C语】例如2+22+222+2222+22222此式的和

热门文章

  1. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
  2. docker命令入门 - 夜的第一张
  3. vue 中获取select 的option的value 直接click?
  4. 曲演杂坛--SQLCMD下执行命令失败但没有任何错误提示的坑
  5. vs配置opencv
  6. VB6之从1970年1月1日起的秒数 的与C语言类似的时间函数
  7. 进程间通信(三)共享内存和信号量
  8. ReportServer中,要配置远程可登录帐号
  9. this.$router.push相关的vue-router的导航方法
  10. ubuntu虚拟环境