txt转xml

 代码转自:目标检测: 数据集转换txt转为xml格式_程序员小杜的博客-CSDN博客

# .txt-->.xml
# ! /usr/bin/python
# -*- coding:UTF-8 -*-
import os
import cv2


def txt_to_xml(txt_path, img_path, xml_path):
    # 1.字典对标签中的类别进行转换
    dict = {'0': "all",
            '1': "hole",
            }
    # 2.找到txt标签文件夹
    files = os.listdir(txt_path)
    # 用于存储 "老图"
    pre_img_name = ''
    # 3.遍历文件夹
    for i, name in enumerate(files):
        # 许多人文件夹里有该文件,默认的也删不掉,那就直接pass
        if name == "desktop.ini":
            continue
        print(name)
        # 4.打开txt
        txtFile = open(txt_path + name)
        # 读取所有内容
        txtList = txtFile.readlines()
        # 读取图片名称
        img_name = name.split(".")[0]
        pic = cv2.imread(img_path + img_name + ".jpg")
        # 获取图像大小信息
        Pheight, Pwidth, Pdepth = pic.shape
        # 5.遍历txt文件中每行内容
        for row in txtList:
            # 按' '分割txt的一行的内容
            oneline = row.strip().split(" ")
            # 遇到的是一张新图片
            if img_name != pre_img_name:
                # 6.新建xml文件
                xml_file = open((xml_path + img_name + '.xml'), 'w')
                xml_file.write('\n')
                xml_file.write('    VOC2007\n')
                xml_file.write('    ' + img_name + '.jpg' + '\n')
                xml_file.write('\n')
                xml_file.write('orgaquant\n')
                xml_file.write('organoids\n')
                xml_file.write('\n')
                xml_file.write('    \n')
                xml_file.write('        ' + str(Pwidth) + '\n')
                xml_file.write('        ' + str(Pheight) + '\n')
                xml_file.write('        ' + str(Pdepth) + '\n')
                xml_file.write('    \n')
                xml_file.write('    \n')
                xml_file.write('' + dict[oneline[0]] + '\n')
                xml_file.write('        \n')
                xml_file.write('            ' + str(
                    int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)) + '\n')
                xml_file.write('            ' + str(
                    int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)) + '\n')
                xml_file.write('            ' + str(
                    int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)) + '\n')
                xml_file.write('            ' + str(
                    int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)) + '\n')
                xml_file.write('        \n')
                xml_file.write('    \n')
                xml_file.close()
                pre_img_name = img_name  # 将其设为"老"图
            else:  # 不是新图而是"老图"
                # 7.同一张图片,只需要追加写入object
                xml_file = open((xml_path + img_name + '.xml'), 'a')
                xml_file.write('    \n')
                xml_file.write('' + dict[oneline[0]] + '\n')
                '''  按需添加这里和上面
                xml_file.write('        Unspecified\n')
                xml_file.write('        0\n')
                xml_file.write('        0\n')
                '''
                xml_file.write('        \n')
                xml_file.write('            ' + str(
                    int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)) + '\n')
                xml_file.write('            ' + str(
                    int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)) + '\n')
                xml_file.write('            ' + str(
                    int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)) + '\n')
                xml_file.write('            ' + str(
                    int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)) + '\n')
                xml_file.write('        \n')
                xml_file.write('    \n')
                xml_file.close()

        # 8.读完txt文件最后写入
        xml_file1 = open((xml_path + pre_img_name + '.xml'), 'a')
        xml_file1.write('')
        xml_file1.close()
    print("Done !")


# 修改成自己的文件夹 注意文件夹最后要加上/
txt_to_xml("E:/train/side-detection/darknet-test/obj/txt/", "E:/train/side-detection/darknet-test/obj/pic/", "E:/train/side-detection/darknet-test/obj/xml/")

修改:1.开头的对应字典,改成自己的类

2.最后放三个地址,txt,jpg,xml(自己新建)

注意:这个xml中没有difficult标签,所以可能会报错,报错看另一篇。

或者自己加一下,我对着我用labelImg标记生成的xml文件修改了一下(还有一点点不同,但是不影响)

# .txt-->.xml
# ! /usr/bin/python
# -*- coding:UTF-8 -*-
import os
import cv2


def txt_to_xml(txt_path, img_path, xml_path):
    # 1.字典对标签中的类别进行转换
    dict = {'0': "all",
            '1': "hole",

            }
    # 2.找到txt标签文件夹
    files = os.listdir(txt_path)
    # 用于存储 "老图"
    pre_img_name = ''
    # 3.遍历文件夹
    for i, name in enumerate(files):
        # 许多人文件夹里有该文件,默认的也删不掉,那就直接pass
        if name == "desktop.ini":
            continue
        print(name)
        # 4.打开txt
        txtFile = open(txt_path + name)
        # 读取所有内容
        txtList = txtFile.readlines()
        # 读取图片名称
        img_name = name.split(".")[0]
        pic = cv2.imread(img_path + img_name + ".jpg")
        # 获取图像大小信息
        Pheight, Pwidth, Pdepth = pic.shape
        # 5.遍历txt文件中每行内容
        for row in txtList:
            # 按' '分割txt的一行的内容
            oneline = row.strip().split(" ")
            # 遇到的是一张新图片
            if img_name != pre_img_name:
                # 6.新建xml文件
                xml_file = open((xml_path + img_name + '.xml'), 'w')
                xml_file.write('\n')
                xml_file.write('            VOC2007\n')
                xml_file.write('            ' + img_name + '.jpg' + '\n')
                xml_file.write('            \n')
                xml_file.write('                    orgaquant\n')
                xml_file.write('                    organoids\n')
                xml_file.write('            \n')
                xml_file.write('            \n')
                xml_file.write('                    ' + str(Pwidth) + '\n')
                xml_file.write('                    ' + str(Pheight) + '\n')
                xml_file.write('                    ' + str(Pdepth) + '\n')
                xml_file.write('            \n')
                xml_file.write('            0\n')
                xml_file.write('            \n')
                xml_file.write('                    ' + dict[oneline[0]] + '\n')
                xml_file.write('                    Unspecified\n')
                xml_file.write('                    0\n')
                xml_file.write('                    0\n')
                xml_file.write('                    \n')
                xml_file.write('                            ' + str(
                    int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)) + '\n')
                xml_file.write('                            ' + str(
                    int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)) + '\n')
                xml_file.write('                            ' + str(
                    int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)) + '\n')
                xml_file.write('                            ' + str(
                    int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)) + '\n')
                xml_file.write('                    \n')
                xml_file.write('            \n')
                xml_file.close()
                pre_img_name = img_name  # 将其设为"老"图
            else:  # 不是新图而是"老图"
                # 7.同一张图片,只需要追加写入object
                xml_file = open((xml_path + img_name + '.xml'), 'a')
                xml_file.write('            \n')
                xml_file.write('                    ' + dict[oneline[0]] + '\n')
                xml_file.write('                    Unspecified\n')
                xml_file.write('                    0\n')
                xml_file.write('                    0\n')
                '''  按需添加这里和上面
                xml_file.write('        Unspecified\n')
                xml_file.write('        0\n')
                xml_file.write('        0\n')
                '''
                xml_file.write('                    \n')
                xml_file.write('                            ' + str(
                    int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)) + '\n')
                xml_file.write('                            ' + str(
                    int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)) + '\n')
                xml_file.write('                            ' + str(
                    int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)) + '\n')
                xml_file.write('                            ' + str(
                    int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)) + '\n')
                xml_file.write('                    \n')
                xml_file.write('            \n')
                xml_file.close()

        # 8.读完txt文件最后写入
        xml_file1 = open((xml_path + pre_img_name + '.xml'), 'a')
        xml_file1.write('')
        xml_file1.close()
    print("Done !")


# 修改成自己的文件夹 注意文件夹最后要加上/
txt_to_xml("E:/train/side-detection/darknet-test/obj/txt/", "E:/train/side-detection/darknet-test/obj/pic/", "E:/train/side-detection/darknet-test/obj/xml/")

你可能感兴趣的:(python,深度学习)