当前位置:   article > 正文

DOTA数据集转VOC数据集,模仿DIOR数据集类型_dior-r数据集下载

dior-r数据集下载

DOTA文本数据集转XML格式

说明:由于本人比较喜欢DIOR数据集格式,所以仿照DIOR数据集转的相同格式

DOTA数据集下载链接:https://pan.baidu.com/s/1lksAYbogYT3OjTBzNuQuNA
提取码:7vwv

DOIR数据集下载链接:
https://pan.baidu.com/s/1QPALicrLHqhblnGu_EBjlw
提取码:jcpg

代码如下(改了网上的代码,此处贴上链接

import os
from xml.dom.minidom import Document
from xml.dom.minidom import parse
import xml.dom.minidom
import numpy as np
import csv
import cv2
import string

def WriterXMLFiles(filename,img_name, path, box_list, label_list, w, h, d):

    # dict_box[filename]=json_dict[filename]
    doc = xml.dom.minidom.Document()
    root = doc.createElement('annotation')
    doc.appendChild(root)

    # foldername = doc.createElement("folder")
    # foldername.appendChild(doc.createTextNode("JPEGImages"))
    # root.appendChild(foldername)

    nodeFilename = doc.createElement('filename')
    nodeFilename.appendChild(doc.createTextNode(img_name))
    root.appendChild(nodeFilename)

    # pathname = doc.createElement("path")
    # pathname.appendChild(doc.createTextNode("xxxx"))
    # root.appendChild(pathname)

    sourcename=doc.createElement("source")

    databasename = doc.createElement("database")
    databasename.appendChild(doc.createTextNode("DOTA"))
    sourcename.appendChild(databasename)

    # annotationname = doc.createElement("annotation")
    # annotationname.appendChild(doc.createTextNode("xxx"))
    # sourcename.appendChild(annotationname)

    # imagename = doc.createElement("image")
    # imagename.appendChild(doc.createTextNode("xxx"))
    # sourcename.appendChild(imagename)

    # flickridname = doc.createElement("flickrid")
    # flickridname.appendChild(doc.createTextNode("0"))
    # sourcename.appendChild(flickridname)

    root.appendChild(sourcename)

    nodesize = doc.createElement('size')
    nodewidth = doc.createElement('width')
    nodewidth.appendChild(doc.createTextNode(str(w)))
    nodesize.appendChild(nodewidth)
    nodeheight = doc.createElement('height')
    nodeheight.appendChild(doc.createTextNode(str(h)))
    nodesize.appendChild(nodeheight)
    nodedepth = doc.createElement('depth')
    nodedepth.appendChild(doc.createTextNode(str(d)))
    nodesize.appendChild(nodedepth)
    root.appendChild(nodesize)

    segname = doc.createElement("segmented")
    segname.appendChild(doc.createTextNode("0"))
    root.appendChild(segname)

    for (box, label) in zip(box_list, label_list):

        nodeobject = doc.createElement('object')
        nodename = doc.createElement('name')
        nodename.appendChild(doc.createTextNode(str(label)))
        nodeobject.appendChild(nodename)
        nodebndbox = doc.createElement('bndbox')
        nodex1 = doc.createElement('x1')
        nodex1.appendChild(doc.createTextNode(str(box[0])))
        nodebndbox.appendChild(nodex1)
        nodey1 = doc.createElement('y1')
        nodey1.appendChild(doc.createTextNode(str(box[1])))
        nodebndbox.appendChild(nodey1)
        nodex2 = doc.createElement('x2')
        nodex2.appendChild(doc.createTextNode(str(box[2])))
        nodebndbox.appendChild(nodex2)
        nodey2 = doc.createElement('y2')
        nodey2.appendChild(doc.createTextNode(str(box[3])))
        nodebndbox.appendChild(nodey2)
        nodex3 = doc.createElement('x3')
        nodex3.appendChild(doc.createTextNode(str(box[4])))
        nodebndbox.appendChild(nodex3)
        nodey3 = doc.createElement('y3')
        nodey3.appendChild(doc.createTextNode(str(box[5])))
        nodebndbox.appendChild(nodey3)
        nodex4 = doc.createElement('x4')
        nodex4.appendChild(doc.createTextNode(str(box[6])))
        nodebndbox.appendChild(nodex4)
        nodey4 = doc.createElement('y4')
        nodey4.appendChild(doc.createTextNode(str(box[7])))
        nodebndbox.appendChild(nodey4)

        # ang = doc.createElement('angle')
        # ang.appendChild(doc.createTextNode(str(angle)))
        # nodebndbox.appendChild(ang)
        nodeobject.appendChild(nodebndbox)
        root.appendChild(nodeobject)
    fp = open(path + filename, 'w')
    doc.writexml(fp, indent='\n')
    fp.close()


def load_annoataion(p):
    '''
    load annotation from the text file
    :param p:
    :return:
    '''
    text_polys = []
    text_tags = []
    if not os.path.exists(p):
        return np.array(text_polys, dtype=np.float32)
    with open(p, 'r') as f:
        for line in f.readlines()[2:]:
            label = 'text'
            # strip BOM. \ufeff for python3,  \xef\xbb\bf for python2
            #line = [i.strip('\ufeff').strip('\xef\xbb\xbf') for i in line]
            #print(line)

            x1, y1, x2, y2, x3, y3, x4, y4 ,label= line.split(' ')[0:9]
            #print(label)
            text_polys.append([x1, y1, x2, y2, x3, y3, x4, y4])
            text_tags.append(label)

        return np.array(text_polys, dtype=np.float), np.array(text_tags, dtype=np.str)

txt_path = r'E:/baidudisk/DOTA/train/labelTxt-v1.5/DOTA-v1.5_train/'
xml_path = r'E:/baidudisk/DOTA/train/labelTxt-v1.5-voc/DOTA-v1.5_train/'
img_path = r'E:/baidudisk/DOTA/train/images/images/'
print(os.path.exists(txt_path))
txts = os.listdir(txt_path)
for count, t in enumerate(txts):
    path = os.path.join(txt_path,t)
    print(path)
    # boxes存储的是八个坐标
    # labels存储的是标签
    boxes, labels = load_annoataion(os.path.join(txt_path, t))
    # P0000.xml
    xml_name = t.replace('.txt', '.xml')
    img_name = t.replace('.txt', '.png')
    # P0000.png
    img = cv2.imread(os.path.join(img_path, img_name))
    h, w, d = img.shape
    #print(xml_name, xml_path, boxes, labels, w, h, d)
    WriterXMLFiles(xml_name,img_name, xml_path, boxes, labels, w, h, d)
    if count % 1000 == 0:
        print(count)
  • 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
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151

说明
1:请务必将自己的文件路径进行修改
2:导包出现如下图横线,感觉也没有影响程序运行
在这里插入图片描述
3:文件转换最终如下图:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/679241
推荐阅读
相关标签
  

闽ICP备14008679号