批量将VOC数据集(归一化后)的labels转换为原始标注文件xml

由于在制作VOC数据集时,未生成标注文件(xml),直接生成了labels,格式如下:

先需将其转换为xml文件,格式如下,用于别的模型训练。

转化代码如下(C++):


# encoding: utf-8
from xml.dom.minidom import Document
import os
import os.path
from PIL import Image

ann_path = "/home/swl/darknet_smoke/scripts/VOCdevkit/VOC2019/111111/labels/"         #  labels .txt文件夹所在位置
img_path = "/home/swl/darknet_smoke/scripts/VOCdevkit/VOC2019/111111/JPEGImages/"     #  原图文件夹所在位置
xml_path = "/home/swl/darknet_smoke/scripts/VOCdevkit/VOC2019/111111/Annotations/"    #  所需生成的xml文件所在位置
#print("111")

if not os.path.exists(xml_path):   
    os.mkdir(xml_path)
#print("1121")

def writeXml(tmp, imgname, w, h, objbud, wxml):   
    doc = Document()
    #owner
    ##print("111")
    a = float (str(w))
    #print(a)
    #print(type(a))
    b = float (str(h))
    #print(b)
    #print(type(b))
    annotation = doc.createElement('annotation')           #创建annotation一级节点
    doc.appendChild(annotation)
    ##print(annotation)
    #owner
    folder = doc.createElement('folder')
    annotation.appendChild(folder)
    folder_txt = doc.createTextNode("VOC2005")
    folder.appendChild(folder_txt)

    filename = doc.createElement('filename')
    annotation.appendChild(filename)
    filename_txt = doc.createTextNode(imgname)
    filename.appendChild(filename_txt)
    #ones#
    source = doc.createElement('source')
    annotation.appendChild(source)

    database = doc.createElement('database')
    source.appendChild(database)
    database_txt = doc.createTextNode("The VOC2019 Database")
    database.appendChild(database_txt)




# 以下两个为VOC里没有的

    annotation_new = doc.createElement('annotation')
    source.appendChild(annotation_new)
    annotation_new_txt = doc.createTextNode("PASCAL VOC2019")
    annotation_new.appendChild(annotation_new_txt)

    image = doc.createElement('image')
    source.appendChild(image)
    image_txt = doc.createTextNode("flickr")
    image.appendChild(image_txt)
    #onee#



    #twos#
    size = doc.createElement('size')
    annotation.appendChild(size)
#width
    width = doc.createElement('width')
    size.appendChild(width)
    width_txt = doc.createTextNode(str(w))
    width.appendChild(width_txt)
#height
    height = doc.createElement('height')
    size.appendChild(height)
    height_txt = doc.createTextNode(str(h))
    height.appendChild(height_txt)
#depth
    depth = doc.createElement('depth')
    size.appendChild(depth)
    depth_txt = doc.createTextNode("3")
    depth.appendChild(depth_txt)
    #twoe#
    segmented = doc.createElement('segmented')
    annotation.appendChild(segmented)
    segmented_txt = doc.createTextNode("0")
    segmented.appendChild(segmented_txt)

    ##print("111")
    for i in range(0,len(objbud)/5):
        #threes#
        object_new = doc.createElement("object")
        annotation.appendChild(object_new)
        name = doc.createElement('name')
        object_new.appendChild(name)
        name_txt = doc.createTextNode("smoke")
        name.appendChild(name_txt)

        pose = doc.createElement('pose')
        object_new.appendChild(pose)
        pose_txt = doc.createTextNode("Unspecified")
        pose.appendChild(pose_txt)

        truncated = doc.createElement('truncated')
        object_new.appendChild(truncated)
        truncated_txt = doc.createTextNode("0")
        truncated.appendChild(truncated_txt)

        difficult = doc.createElement('difficult')
        object_new.appendChild(difficult)
        difficult_txt = doc.createTextNode("0")
        difficult.appendChild(difficult_txt)
        #threes-1#
        bndbox = doc.createElement('bndbox')
        object_new.appendChild(bndbox)

        xmin = doc.createElement('xmin')
        bndbox.appendChild(xmin)
        xmin_txt = doc.createTextNode(str(int (float (objbud[i*5+1])*a-float (objbud[i*5+3])*(a/2.0))))

        #xmin_txt = doc.createTextNode(str(float (objbud[i*5+1])))
        xmin.appendChild(xmin_txt)
        #print(float (objbud[i*5+1])*a-a/2)
        #print(float (objbud[i*5+1]))
        #print(type(float (objbud[i*5+1])))


        ymin = doc.createElement('ymin')
        bndbox.appendChild(ymin)
        ymin_txt = doc.createTextNode(str(int (float (objbud[i*5+2])*b-float (objbud[i*5+4])*(b/2.0))))
        ymin.appendChild(ymin_txt)

        xmax = doc.createElement('xmax')
        bndbox.appendChild(xmax)
        xmax_txt = doc.createTextNode(str(int (float (objbud[i*5+1])*a+float (objbud[i*5+3])*(a/2.0))))
        xmax.appendChild(xmax_txt)

        ymax = doc.createElement('ymax')
        bndbox.appendChild(ymax)
        ymax_txt = doc.createTextNode(str(int (float (objbud[i*5+2])*b+float (objbud[i*5+4])*(b/2.0))))
        ymax.appendChild(ymax_txt)
        #threee-1#
        #threee#
        ##print("11")    
    tempfile = tmp + "test.xml"
    with open(tempfile, "w") as f:
        f.write(doc.toprettyxml(indent = '\t', encoding='utf-8'))

    rewrite = open(tempfile, "r")
    lines = rewrite.read().split('\n')
    newlines = lines[1:len(lines)-1]
    
    fw = open(wxml, "w")
    for i in range(0, len(newlines)):
        fw.write(newlines[i] + '\n')
    
    fw.close()
    rewrite.close()
    os.remove(tempfile)
    return

for files in os.walk(ann_path):
    temp = "/home/swl/Documents/temp/"       #要改要改要改要改
    if not os.path.exists(temp):
        os.mkdir(temp)
    for file in files[2]:
        print file + "-->start!"
        img_name = os.path.splitext(file)[0] + '.jpg'
        fileimgpath = img_path + img_name
        im=Image.open(fileimgpath)  
        width= int(im.size[0])
        height= int(im.size[1])
        
        filelabel = open(ann_path + file, "r")
        lines = filelabel.read().split('\n')
        obj = lines[:len(lines)-1]

        filename = xml_path + os.path.splitext(file)[0] + '.xml'
        writeXml(temp, img_name, width, height, obj, filename)
    os.rmdir(temp)




猜你喜欢

转载自blog.csdn.net/m0_37844017/article/details/80604990