yolo格式(txt)数据集转VOC(xml)

yolov5训练的数据标签格式为txt格式,有时候会有一些需求需要把txt格式的数据集替换成xml格式的数据集。

1.首先讲一下数据的格式,首先按下图要求创建目录结构。

JPEGImages为图片数据所在的目录
    YOLO为yolo格式的标签数据所在目录
             Annotations为生成的voc格式数据标签目录(程序运行前这是一个空目录)

2.把对应文件放到对应文件夹下

3.执行yolo_to_voc.py:

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)
 

Caution:这里对应好自己的数据文件,运行完成Annotation文件夹下就会出现转好的xml文件。

dic = {'0': "hat",  # 创建字典用来对类型进行转换
       '1': "person",  # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致
       }

 

  • 23
    点赞
  • 100
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
要将YOLO格式数据集换为VOC格式数据集,需要按照以下步骤进行操作: 1. 根据YOLO格式数据集的标注文件,将每个图像中的目标对象的位置、类别和置信度信息提取出来,存储在一个文本文件中。每行包含一个目标对象的信息,格式如下: ``` <class> <x_center> <y_center> <width> <height> ``` 其中,`<class>`表示目标对象所属的类别,`<x_center>`和`<y_center>`表示目标对象中心点在图像中的坐标,`<width>`和`<height>`表示目标对象的宽度和高度,所有这些值都是相对于图像大小的比例。 2. 将每个图像的文件名和对应的标注文件名存储在一个XML文件中,格式如下: ``` <annotation> <folder>image_folder</folder> <filename>image_name.jpg</filename> <size> <width>image_width</width> <height>image_height</height> <depth>3</depth> </size> <object> <name>object_class</name> <bndbox> <xmin>xmin_value</xmin> <ymin>ymin_value</ymin> <xmax>xmax_value</xmax> <ymax>ymax_value</ymax> </bndbox> </object> ... </annotation> ``` 其中,`<folder>`表示图像文件所在的文件夹,`<filename>`表示图像文件名,`<width>`和`<height>`表示图像的宽度和高度,`<object>`表示一个目标对象,`<name>`表示目标对象所属的类别,`<bndbox>`表示目标对象的边界框,`<xmin>`、`<ymin>`、`<xmax>`和`<ymax>`分别表示边界框左上角和右下角的坐标。 3. 将所有XML文件和对应的图像文件存储在一个文件夹中,这样就得到了一个VOC格式数据集。 需要注意的是,YOLO格式数据集VOC格式数据集的标注信息格式不同,因此需要进行格式换。此外,VOC格式数据集还需要包含图像文件本身,因此需要将YOLO格式数据集中的图像文件也复制到VOC格式数据集中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值