YOLO格式转VOC xml格式代码

YOLO格式转VOC xml格式代码

from xml.dom.minidom import Document
import os
import cv2
from tqdm import tqdm


# def makexml(txtPath, xmlPath, picPath):  # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
def makexml(picPath, txtPath, xmlPath, datasetName):  # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
    """此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件
    在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml
    """
    dic = {'1': "tailing_pond",  # 创建字典用来对类型进行转换 # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致
           }
    files = os.listdir(txtPath)
    for i, name in enumerate(tqdm(files)):
        if name == 'classes.txt':
            continue
        xmlBuilder = Document()
        annotation = xmlBuilder.createElement("annotation")  # 创建annotation标签
        xmlBuilder.appendChild(annotation)
        txtFile = open(os.path.join(txtPath, name))
        txtList = txtFile.readlines()
        img = cv2.imread(os.path.join(picPath, name.replace('txt', 'png')))
        Pheight, Pwidth, Pdepth = img.shape

        folder = xmlBuilder.createElement("folder")  # folder标签
        foldercontent = xmlBuilder.createTextNode(datasetName)
        folder.appendChild(foldercontent)
        annotation.appendChild(folder)  # folder标签结束

        filename = xmlBuilder.createElement("filename")  # filename标签
        #filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".png")
        filenamecontent = xmlBuilder.createTextNode(name[0:-4])
        filename.appendChild(filenamecontent)
        annotation.appendChild(filename)  # filename标签结束

        object_num = xmlBuilder.createElement("object_num")  # filename标签
        object_num_count = xmlBuilder.createTextNode(str(len(txtList)))
        object_num.appendChild(object_num_count)
        annotation.appendChild(object_num)  # object_num标签结束

        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(" ")
            print("oneline: ",oneline)
            object = xmlBuilder.createElement("object")  #object标签
            picname = xmlBuilder.createElement("name")  #name标签
            namecontent = xmlBuilder.createTextNode(oneline[8])
            #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标签结束

            x_list = []
            x_list.append(oneline[0])
            x_list.append(oneline[2])
            x_list.append(oneline[4])
            x_list.append(oneline[6])
            y_list = []
            y_list.append(oneline[1])
            y_list.append(oneline[3])
            y_list.append(oneline[5])
            y_list.append(oneline[7])

            bndbox = xmlBuilder.createElement("bndbox")  # bndbox标签
            xmin = xmlBuilder.createElement("xmin")  # xmin标签
            #mathData = float(oneline[1]) - float(oneline[3])
            mathData = min(x_list)
            xminContent = xmlBuilder.createTextNode(str(mathData))
            xmin.appendChild(xminContent)
            bndbox.appendChild(xmin)  # xmin标签结束

            ymin = xmlBuilder.createElement("ymin")  # ymin标签
            #mathData = float(oneline[2]) - float(oneline[4])
            mathData = min(y_list)
            yminContent = xmlBuilder.createTextNode(str(mathData))
            ymin.appendChild(yminContent)
            bndbox.appendChild(ymin)  # ymin标签结束

            xmax = xmlBuilder.createElement("xmax")  # xmax标签
            #mathData = float(oneline[1]) + float(oneline[3])
            mathData = max(x_list)
            xmaxContent = xmlBuilder.createTextNode(str(mathData))
            xmax.appendChild(xmaxContent)
            bndbox.appendChild(xmax)  # xmax标签结束

            ymax = xmlBuilder.createElement("ymax")  # ymax标签
            #mathData = float(oneline[2]) + float(oneline[4])
            mathData = max(y_list)
            ymaxContent = xmlBuilder.createTextNode(str(mathData))
            ymax.appendChild(ymaxContent)
            bndbox.appendChild(ymax)  # ymax标签结束

            object.appendChild(bndbox)  # bndbox标签结束

            annotation.appendChild(object)  # object标签结束

        f = open(os.path.join(xmlPath, name.replace('txt', 'xml')), 'w')
        #xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
        xmlBuilder.writexml(f, newl='\n', addindent='\t', encoding='utf-8')
        f.close()


if __name__ == "__main__":
    # picPath = r"原图文件夹路径"         # 原图文件夹路径
    # txtPath = r"原txt标签文件夹路径"    # 原txt标签文件夹路径
    # xmlPath = r"保存xml文件夹路径"    # 保存xml文件夹路径
    # datasetName = r'MyDataset'      # 数据集名称
    # os.makedirs(xmlPath) if not os.path.exists(xmlPath) else None
    # makexml(picPath, txtPath, xmlPath, datasetName)

    tasks = ['train', 'test', 'val']          # 当前任务
    for task in tasks:
        picPath = r"C:\Users\Administrator\Desktop\zhouyong\images"        # 原图文件夹路径
        txtPath = r"C:\Users\Administrator\Desktop\zhouyong\labels"        # 原txt标签文件夹路径
        xmlPath = r"C:\Users\Administrator\Desktop\zhouyong\xml"     # 保存xml文件夹路径
        datasetName = r'VOC'
        os.makedirs(xmlPath) if not os.path.exists(xmlPath) else None
        makexml(picPath, txtPath, xmlPath, datasetName)

你可能感兴趣的:(YOLO,xml,python)