Python将VOC格式的TXT文件转换为XML文件

 可以将voc格式中储存的图片的标注信息转化为xml文件

方便阅读以及可视化

可以二次标注

txt格式: cls、x、 y、w、h 

import cv2
from lxml.etree import Element, SubElement, tostring

# 修改voc格式中数字对应的类别
classes = {0:'w',1:'b'}

# 读取图片信息 返回WHC
def Read_img(path):
    image = cv2.imread(path)
    list_image = (image.shape[0],image.shape[1],image.shape[2])
    return list_image

# 读取与图片对应的txt文件的信息
def Read_txt(path1,path2):
    xyxy_list = []
    f = open(path1,'r')
    for line in f.readlines():
        line = line.strip('\n').split()
        cls = int(line[0])
        line[0] = classes[cls]
        W = Read_img(path2)[0]
        H = Read_img(path2)[1]
        x = float(line[1])
        y = float(line[2])
        w = float(line[3])
        h = float(line[4])
        xmax = int(W * (2 * x + w)) / 2
        xmin = int(W * (2 * x - w)) / 2
        ymax = int(H * (2 * y + h)) / 2
        ymin = int(H * (2 * y - h)) / 2
        line[1] = str(xmin)
        line[2] = str(ymin)
        line[3] = str(xmax)
        line[4] = str(ymax)
        xyxy_list.append(line)
    f.close()
    return xyxy_list

def create_xml(list_xml,list_images,xml_path):
    """
    创建xml文件,依次写入xml文件必备关键字
    :param list_xml:   txt文件中的box
    :param list_images:   图片信息,xml中需要写入WHC
    :return:
    """
    node_root = Element('annotation')
    node_folder = SubElement(node_root, 'folder')
    node_folder.text = 'Images'
    node_filename = SubElement(node_root, 'filename')
    node_filename.text = str(list_images[3])
    node_size = SubElement(node_root, 'size')
    node_width = SubElement(node_size, 'width')
    node_width.text = str(list_images[0])
    node_height = SubElement(node_size, 'height')
    node_height.text = str(list_images[1])
    node_depth = SubElement(node_size, 'depth')
    node_depth.text = str(list_images[2])

    if len(list_xml)>=1:  
        for list_ in list_xml:
            node_object = SubElement(node_root, 'object')
            node_name = SubElement(node_object, 'name')
            node_name.text = str(list_[0])
            node_difficult = SubElement(node_object, 'difficult')
            node_difficult.text = '0'
            node_bndbox = SubElement(node_object, 'bndbox')
            node_xmin = SubElement(node_bndbox, 'xmin')
            node_xmin.text = str(list_[1])
            node_ymin = SubElement(node_bndbox, 'ymin')
            node_ymin.text = str(list_[2])
            node_xmax = SubElement(node_bndbox, 'xmax')
            node_xmax.text = str(list_[3])
            node_ymax = SubElement(node_bndbox, 'ymax')
            node_ymax.text = str(list_[4])

    xml = tostring(node_root, pretty_print=True)   

    # 不是jpg格式的将 .jpg 修改为对应的格式
    file_name = list_images[3].replace(".jpg",'.xml')
    filename = xml_path+"/{}".format(file_name)

    f = open(filename, "wb")
    f.write(xml)
    f.close()


if __name__ == '__main__':

    import os
    img_path = r"/Data/1"          # 图片路径
    txt_path = r"/1_txt"      # xml标注保存路径
    xml_path = r"/1_xml"      # xml标注保存路径
    filename=os.listdir(txt_path)
    # if '.DS_Store' in filename:
    #     filename.remove('.DS_Store')

    for txt in filename:
        txtpath = os.path.join(txt_path,txt)
        _jpg = txt.replace('.txt','.jpg')
        imgpath = os.path.join(img_path,_jpg)
        xyxy_list = Read_txt(txtpath,imgpath)
        list_image = list(Read_img(imgpath))
        list_image.append(str(_jpg))
        print(list_image)
        create_xml(xyxy_list, list_image, xml_path)

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