yolo格式标签转化为xml格式标签

当时用labelimg标注的时候直接保存了yolo格式的txt标签,现在跑pytorch yolo需要用到voc格式的xml标签,故用以下代码转化。

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': "0degree",  # 创建字典用来对类型进行转换
           '1': "6degree",  # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致
           '2': "12degree",
           }
    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()
        for root,dirs,filename in os.walk(picPath):
            img = cv2.imread(root+ '\\'+filename[i])
            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 = r"E:\bottle_data\test1\VOCdevkit\JPEGImages"  # 图片所在文件夹路径,后面的/一定要带上
    txtPath = r"E:\bottle_data\test1\VOCdevkit\YOLO"  # yolo txt所在文件夹路径,后面的/一定要带上
    xmlPath = r"E:\bottle_data\test1\VOCdevkit\XML"  # xml文件保存路径,后面的/一定要带上
    makexml(picPath, txtPath, xmlPath)

修改两处即可:
(1)改成自己class类别

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

(2)改成自己的路径

if __name__ == "__main__":
    picPath = r"E:\bottle_data\test1\VOCdevkit\JPEGImages"  # 图片所在文件夹路径,后面的/一定要带上
    txtPath = r"E:\bottle_data\test1\VOCdevkit\YOLO"  # yolo txt所在文件夹路径,后面的/一定要带上
    xmlPath = r"E:\bottle_data\test1\VOCdevkit\XML"  # xml文件保存路径,后面的/一定要带上
    makexml(picPath, txtPath, xmlPath)
  • 12
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
当将YOLO格式标签文件转换为XML格式标签文件时,您可以按照以下步骤进行操作: 1. 解析YOLO格式标签文件,获取每个边界框的类别、边界框左上角坐标和宽高。 2. 创建一个XML文件,并设置根节点。 3. 针对每个边界框,创建一个XML子节点,并添加类别、边界框坐标和宽高的子节点。 4. 将XML内容保存到文件中。 以下是一个使用Python实现的示例代码: ```python import xml.etree.ElementTree as ET def convert_yolo_to_xml(yolo_file, output_file): with open(yolo_file, 'r') as f: lines = f.readlines() root = ET.Element("annotations") for line in lines: line = line.strip().split() class_id = int(line[0]) x_center = float(line[1]) y_center = float(line[2]) width = float(line[3]) height = float(line[4]) xmin = int((x_center - width / 2) * image_width) ymin = int((y_center - height / 2) * image_height) xmax = int((x_center + width / 2) * image_width) ymax = int((y_center + height / 2) * image_height) box = ET.SubElement(root, "object") name = ET.SubElement(box, "name") name.text = str(class_id) bndbox = ET.SubElement(box, "bndbox") xmin_node = ET.SubElement(bndbox, "xmin") xmin_node.text = str(xmin) ymin_node = ET.SubElement(bndbox, "ymin") ymin_node.text = str(ymin) xmax_node = ET.SubElement(bndbox, "xmax") xmax_node.text = str(xmax) ymax_node = ET.SubElement(bndbox, "ymax") ymax_node.text = str(ymax) tree = ET.ElementTree(root) tree.write(output_file) # 使用示例 yolo_file = 'path/to/yolo.txt' output_file = 'path/to/xml.xml' image_width = 640 image_height = 480 convert_yolo_to_xml(yolo_file, output_file) ``` 请将示例代码中的`yolo_file`替换为YOLO格式标签文件路径,`output_file`替换为您希望保存XML标签文件的路径,`image_width`和`image_height`替换为图像的宽度和高度。运行代码后,将生成一个包含边界框信息的XML标签文件。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值