当前位置:网站首页>yolo格式(txt)数据集转VOC(xml)
yolo格式(txt)数据集转VOC(xml)
2022-08-02 10:45:00 【Star_.】
(弄数据集是真的费劲)
1.首先,创建个voc的目录
JPEGImages为图片数据所在的目录
YOLO为yolo格式的标签数据所在目录
Annotations为生成的voc格式数据标签目录(程序运行前这是一个空目录)
2.
from xml.dom.minidom import Document
import os
import cv2
# def makexml(txtPath, xmlPath, picPath): # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
def makexml(picPath, txtPath, xmlPath): # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
"""此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件 在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml """
dic = {
'0': "hat", # 创建字典用来对类型进行转换
'1': "person", # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致
}
files = os.listdir(txtPath)
for i, name in enumerate(files):
xmlBuilder = Document()
annotation = xmlBuilder.createElement("annotation") # 创建annotation标签
xmlBuilder.appendChild(annotation)
txtFile = open(txtPath + name)
txtList = txtFile.readlines()
img = cv2.imread(picPath + name[0:-4] + ".jpg")
Pheight, Pwidth, Pdepth = img.shape
folder = xmlBuilder.createElement("folder") # folder标签
foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")
folder.appendChild(foldercontent)
annotation.appendChild(folder) # folder标签结束
filename = xmlBuilder.createElement("filename") # filename标签
filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".jpg")
filename.appendChild(filenamecontent)
annotation.appendChild(filename) # filename标签结束
size = xmlBuilder.createElement("size") # size标签
width = xmlBuilder.createElement("width") # size子标签width
widthcontent = xmlBuilder.createTextNode(str(Pwidth))
width.appendChild(widthcontent)
size.appendChild(width) # size子标签width结束
height = xmlBuilder.createElement("height") # size子标签height
heightcontent = xmlBuilder.createTextNode(str(Pheight))
height.appendChild(heightcontent)
size.appendChild(height) # size子标签height结束
depth = xmlBuilder.createElement("depth") # size子标签depth
depthcontent = xmlBuilder.createTextNode(str(Pdepth))
depth.appendChild(depthcontent)
size.appendChild(depth) # size子标签depth结束
annotation.appendChild(size) # size标签结束
for j in txtList:
oneline = j.strip().split(" ")
object = xmlBuilder.createElement("object") # object 标签
picname = xmlBuilder.createElement("name") # name标签
namecontent = xmlBuilder.createTextNode(dic[oneline[0]])
picname.appendChild(namecontent)
object.appendChild(picname) # name标签结束
pose = xmlBuilder.createElement("pose") # pose标签
posecontent = xmlBuilder.createTextNode("Unspecified")
pose.appendChild(posecontent)
object.appendChild(pose) # pose标签结束
truncated = xmlBuilder.createElement("truncated") # truncated标签
truncatedContent = xmlBuilder.createTextNode("0")
truncated.appendChild(truncatedContent)
object.appendChild(truncated) # truncated标签结束
difficult = xmlBuilder.createElement("difficult") # difficult标签
difficultcontent = xmlBuilder.createTextNode("0")
difficult.appendChild(difficultcontent)
object.appendChild(difficult) # difficult标签结束
bndbox = xmlBuilder.createElement("bndbox") # bndbox标签
xmin = xmlBuilder.createElement("xmin") # xmin标签
mathData = int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)
xminContent = xmlBuilder.createTextNode(str(mathData))
xmin.appendChild(xminContent)
bndbox.appendChild(xmin) # xmin标签结束
ymin = xmlBuilder.createElement("ymin") # ymin标签
mathData = int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)
yminContent = xmlBuilder.createTextNode(str(mathData))
ymin.appendChild(yminContent)
bndbox.appendChild(ymin) # ymin标签结束
xmax = xmlBuilder.createElement("xmax") # xmax标签
mathData = int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)
xmaxContent = xmlBuilder.createTextNode(str(mathData))
xmax.appendChild(xmaxContent)
bndbox.appendChild(xmax) # xmax标签结束
ymax = xmlBuilder.createElement("ymax") # ymax标签
mathData = int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)
ymaxContent = xmlBuilder.createTextNode(str(mathData))
ymax.appendChild(ymaxContent)
bndbox.appendChild(ymax) # ymax标签结束
object.appendChild(bndbox) # bndbox标签结束
annotation.appendChild(object) # object标签结束
f = open(xmlPath + name[0:-4] + ".xml", 'w')
xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
f.close()
if __name__ == "__main__":
picPath = "VOCdevkit/VOC2007/JPEGImages/" # 图片所在文件夹路径,后面的/一定要带上
txtPath = "VOCdevkit/VOC2007/YOLO/" # txt所在文件夹路径,后面的/一定要带上
xmlPath = "VOCdevkit/VOC2007/Annotations/" # xml文件保存路径,后面的/一定要带上
makexml(picPath, txtPath, xmlPath)
3.把目标路径对应好,只执行代码即可
边栏推荐
- DirectX修复工具增强版「建议收藏」
- 365天挑战LeetCode1000题——Day 047 设计循环队列 循环队列
- 流动性质押挖矿系统开发如何制作?单双币系统开发成熟技术
- 21年毕业转行软件测试,从0收入到月薪过万,我真的很幸运...
- bgp与mpls综合实验
- Deep Learning 100 Examples - Convolutional Neural Network (CNN) for mnist handwritten digit recognition
- How to technically ensure the quality of LED display?
- 行为型模式-模板方法模式
- 学习笔记-支付宝支付
- 3 d laser slam: LeGO - LOAM - ground point extracting method and the analysis of the code
猜你喜欢
How to choose a truly "easy-to-use, high-performance" remote control software
外包学生管理系统架构文档
The realization of the list
保姆级教程:写出自己的移动应用和小程序(篇二)
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
爆款视频怎么做?这里或许有答案!
You Only Hypothesize Once: 用旋转等变描述子估计变换做点云配准(已开源)
如何选择一块真正“好用的、性能高”的远程控制软件
MySql模糊查询大全
Deep Learning 100 Examples - Convolutional Neural Network (CNN) for mnist handwritten digit recognition
随机推荐
超赞!发现一个APP逆向神器!
AlphaFold又放大招,剑指整个生物界!
LayaBox---TypeScript---Three slash instructions
List-based queuing and calling system
字节跳动软件测试岗,收到offer后我却拒绝了~给面试的人一些忠告....
SVN如何删除文件名包含空格的文件
How to technically ensure the quality of LED display?
Challenge LeetCode1000 questions in 365 days - Day 047 Design Circular Queue Circular Queue
从众多接口中脱颖而出的最稳定的接口——淘宝详情api
保姆级教程:写出自己的移动应用和小程序(篇二)
10份重磅报告 — 展望中国数字经济未来
8年软件测试工程师的感悟:与薪资相匹配的永远是实力
MySql模糊查询大全
循环语句综合练习
博云入选Gartner中国DevOps代表厂商
LayaBox---TypeScript---声明合并
LeetCode每日一练 —— 225. 用队列实现栈
同样做软件测试,和月收入 3W 的学弟聊了一晚上,我彻底崩溃了
LayaBox---TypeScript---Module
为什么要使用BGP?