当前位置:网站首页>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修复工具增强版「建议收藏」
- You Only Hypothesize Once: 用旋转等变描述子估计变换做点云配准(已开源)
- MP的几种查询方式
- Deep Learning 100 Examples - Convolutional Neural Network (CNN) for mnist handwritten digit recognition
- yolov7 innovation point
- 使用较广泛的安全测试工具有哪些?
- 通过方法引用获取方法名
- 只问耕耘,不问收获,其实收获却在耕耘中
- 如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
- The R language uses the ggtexttable function of the ggpubr package to visualize the table data (draw the table directly or add the table data to the image), set the theme parameter to customize the fi
猜你喜欢

3年测试在职,月薪还不足2w,最近被裁员,用亲身经历给大家提个醒...

Hub and Spoke配置案例

软件测试岗位巨坑?阿里在职7年测试人告诉你千万别上当

Do you agree with this view?Most businesses are digitizing just to ease anxiety

博云入选Gartner中国DevOps代表厂商

MSYS2 QtCreator Clangd 代码分析找不到 mm_malloc.h的问题补救

How to technically ensure the quality of LED display?

Challenge LeetCode1000 questions in 365 days - Day 047 Design Circular Queue Circular Queue

一款优秀的中文识别库——ocr

太帅了!我用炫酷大屏展示爬虫数据!
随机推荐
MySQL模糊查询性能优化
The R language uses the ggtexttable function of the ggpubr package to visualize the table data (draw the table directly or add the table data to the image), set the theme parameter to customize the fi
爆款视频怎么做?这里或许有答案!
How to technically ensure the quality of LED display?
Shell script realizes multi-select DNS simultaneous batch resolution of domain name IP addresses (new update)
情景剧《重走长征路》上演
Spearman's correlation coefficient
关于#oracle#的问题,如何解决?
为什么要使用BGP?
开箱即用-使用异步加载布局来优化页面启动速度的几种方案
软件工程国考总结——选择题
【面向校招】Golang面试题合集
阿里云数据存储生态计划发布,助力伙伴数据创新
LeetCode每日一练 —— 225. 用队列实现栈
LayaBox---TypeScript---装饰器
3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
10份重磅报告 — 展望中国数字经济未来
云原生应用平台的核心模块有哪些
同样做软件测试,和月收入 3W 的学弟聊了一晚上,我彻底崩溃了
MapStruct