• YOLO系列 -- txt2xml脚本


    YOLO系列 – txt2xml脚本

    一般,训练YOLO算法的时候,我们通常需要把xml转换成txt格式标签文件,可以参考我之前写的这篇:
    YOLO系列 — xml2txt脚本


    但是,有时候也需要将txt反向转换为xml格式,话不多说,直接上代码:

    from xml.dom import minidom
    import cv2
    import os
    import glob
    
    txt_dir = r'E:\work\qx_data\clean_all_data\obj_train_data/' #txt存放路径
    img_dir = r"E:\work\qx_data\clean_all_data\obj_train_data/" #图片存放路径(可以与txt存放路径相同)
    save_xml_dir = r"E:\work\qx_data\clean_all_data\xml/" #生成xml所保存的路径
    cls = ["person"......] #类别列表
    
    for i in glob.glob(txt_dir+"*.txt"):
        base_name = os.path.basename(i)[:-4]
        txt_dirtory = i
        jpg_dirtory = img_dir + base_name + ".jpg"
        if not os.path.exists(jpg_dirtory):
            print(jpg_dirtory + "not exist!!!!!!!")
        else:
            img_name = base_name + ".jpg"
            floder = "VOC2007"
            im = cv2.imread(jpg_dirtory)
            w = im.shape[1]
            h = im.shape[0]
            d = im.shape[2]
            # print w,h,d
            print('i=', i)
            doc = minidom.Document()  # 创建DOM树对象
    
            annotation = doc.createElement('annotation')  # 创建子节点
            doc.appendChild(annotation)  # annotation作为doc树的子节点
    
            folder = doc.createElement('folder')
            folder.appendChild(doc.createTextNode(floder))  # 文本节点作为floder的子节点
            annotation.appendChild(folder)  # folder作为annotation的子节点
    
            filename = doc.createElement('filename')
            filename.appendChild(doc.createTextNode(img_name))
            annotation.appendChild(filename)
    
            source = doc.createElement('source')
            database = doc.createElement('database')
            database.appendChild(doc.createTextNode("Unknown"))
            source.appendChild(database)
            annotation.appendChild(source)
    
    
            size = doc.createElement('size')
            width = doc.createElement('width')
            width.appendChild(doc.createTextNode(str(w)))
            size.appendChild(width)
            height = doc.createElement('height')
            height.appendChild(doc.createTextNode(str(h)))
            size.appendChild(height)
            depth = doc.createElement('depth')
            depth.appendChild(doc.createTextNode(str(d)))
            size.appendChild(depth)
            annotation.appendChild(size)
    
    
            txtLabel = open(txt_dirtory, 'r')
            boxes = txtLabel.readlines()
            for box in boxes:
                cls_idx, g_x, g_y, g_w, g_h = box.split(' ')
                t_x1 = (float(g_x) - 0.5 * float(g_w)) * w
                t_y1 = (float(g_y) - 0.5 * float(g_h)) * h
                t_x2 = (float(g_x) + 0.5 * float(g_w)) * w
                t_y2 = (float(g_y) + 0.5 * float(g_h)) * h
                class_name = cls[int(cls_idx)]
                
                object = doc.createElement('object')
                nm = doc.createElement('name')
                nm.appendChild(doc.createTextNode(str(class_name)))
                object.appendChild(nm)
                pose = doc.createElement('pose')
                pose.appendChild(doc.createTextNode("Unspecified"))
                object.appendChild(pose)
                truncated = doc.createElement('truncated')
                # truncated.appendChild(doc.createTextNode("1"))
                truncated.appendChild(doc.createTextNode("Unknown"))
                object.appendChild(truncated)
                difficult = doc.createElement('difficult')
                difficult.appendChild(doc.createTextNode('0'))
                object.appendChild(difficult)
    
                bndbox = doc.createElement('bndbox')
                # xmin ymin
                xmin = doc.createElement('xmin')
                xmin.appendChild(doc.createTextNode(str(int(t_x1))))
                bndbox.appendChild(xmin)
    
                ymin = doc.createElement('ymin')
                ymin.appendChild(doc.createTextNode(str(int(t_y1))))
                bndbox.appendChild(ymin)
    
                # xmax ymin
                xmax = doc.createElement('xmax')
                xmax.appendChild(doc.createTextNode(str(int(t_x2))))
                bndbox.appendChild(xmax)
    
                ymin = doc.createElement('ymax')
                ymin.appendChild(doc.createTextNode(str(int(t_y2))))
                bndbox.appendChild(ymin)
    
                #+++++++++++++++++++++++++++++++++++++++++++++++++
                object.appendChild(bndbox)
                annotation.appendChild(object)
                savefile = open(os.path.join(save_xml_dir, base_name + '.xml'), 'w')
                savefile.write(doc.toprettyxml())
                savefile.close()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
  • 相关阅读:
    docker 打包镜像
    盲人餐厅点餐:科技之光照亮餐桌上的美食之旅
    常见的内网穿透工具有 ngrok/ localtunnel/ frp
    opentelemetry+python+jaeger链路追踪相关使用备注
    基于.Net Core实现的飞书所有文档一键导出服务(支持多系统)
    zaabix实现对nginx监控
    一文详解Docker数据卷(volume)
    Javascript中integrity属性安全验证
    【LeetCode】Day131-子集 I&II & 组合
    shell 判断文件是否存在(csh bash)
  • 原文地址:https://blog.csdn.net/weixin_42206075/article/details/126971408