当前位置:   article > 正文

【AI目标检测】VOC格式数据集转换为DOTA类型数据集_voc to dota

voc to dota

由于目前的imglabel画出来框都是voc类型的xml文件:

<annotation>
	<folder>rotate_jueyuanzi_zip</folder>
	<filename>0004.jpg</filename>
	<path>D:\Desktop\rotate\dataset\rotate_jueyuanzi_zip\0004.jpg</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>1037</width>
		<height>778</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>comp</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<robndbox>
			<cx>453.9988</cx>
			<cy>159.4623</cy>
			<w>44.3132</w>
			<h>106.04</h>
			<angle>4.963185</angle>
		</robndbox>
		<extra/>
	</object>
	<object>
		<name>comp</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<robndbox>
			<cx>548.4988</cx>
			<cy>159.4623</cy>
			<w>35.867</w>
			<h>98.7147</h>
			<angle>4.963185</angle>
		</robndbox>
		<extra/>
	</object>
	<object>
		<name>comp</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<robndbox>
			<cx>486.0786</cx>
			<cy>304.3616</cy>
			<w>45.8548</w>
			<h>99.6275</h>
			<angle>0.81</angle>
		</robndbox>
		<extra/>
	</object>
	<object>
		<name>comp</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<robndbox>
			<cx>585.526</cx>
			<cy>326.5931</cy>
			<w>46.7375</w>
			<h>102.784</h>
			<angle>0.81</angle>
		</robndbox>
		<extra/>
	</object>
</annotation>

  • 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

想要转换成为DOTA类型的数据:

513.9 363.3 622.1 466.1 578.5 511.9 470.3 409.1 comp 0
280.0 198.9 411.9 159.6 427.1 210.7 295.3 250.1 comp 0
404.4 198.2 538.1 165.5 550.7 217.4 417.1 250.1 comp 0
373.9 332.3 482.1 435.1 438.5 480.9 330.3 378.1 comp 0

  • 1
  • 2
  • 3
  • 4
  • 5

转换代码:

import os
import xml.etree.ElementTree as ET
import math


def voc_to_dota(xml_path, xml_name):
    txt_name = xml_name[:-4] + '.txt'
    txt_path = xml_path + '/txt_label'
    if not os.path.exists(txt_path):
        os.makedirs(txt_path)
    txt_file = os.path.join(txt_path, txt_name)
    file_path = os.path.join(xml_path, file_list[i])
    tree = ET.parse(os.path.join(file_path))
    root = tree.getroot()
    # print(root[6][0].text)
    with open(txt_file, "w+", encoding='UTF-8') as out_file:
        # out_file.write('imagesource:null' + '\n' + 'gsd:null' + '\n')
        for obj in root.findall('object'):
            name = obj.find('name').text
            difficult = obj.find('difficult').text
            # print(name, difficult)
            robndbox = obj.find('robndbox')
            cx = float(robndbox.find('cx').text)
            cy = float(robndbox.find('cy').text)
            w = float(robndbox.find('w').text)
            h = float(robndbox.find('h').text)
            angle = float(robndbox.find('angle').text)
            # print(cx, cy, w, h, angle)
            p0x, p0y = rotatePoint(cx, cy, cx - w / 2, cy - h / 2, -angle)
            p1x, p1y = rotatePoint(cx, cy, cx + w / 2, cy - h / 2, -angle)
            p2x, p2y = rotatePoint(cx, cy, cx + w / 2, cy + h / 2, -angle)
            p3x, p3y = rotatePoint(cx, cy, cx - w / 2, cy + h / 2, -angle)
            data = str(p0x) + " " + str(p0y) + " " + str(p1x) + " " + str(p1y) + " " + \
                   str(p2x) + " " + str(p2y) + " " + str(p3x) + " " + str(p3y) + " "
            data = data + name + " " + difficult + "\n"
            out_file.write(data)


# 转换成四点坐标
def rotatePoint(xc, yc, xp, yp, theta):
    xoff = xp - xc
    yoff = yp - yc
    cosTheta = math.cos(theta)
    sinTheta = math.sin(theta)
    pResx = cosTheta * xoff + sinTheta * yoff
    pResy = - sinTheta * xoff + cosTheta * yoff
    # pRes = (xc + pResx, yc + pResy)
    # 保留一位小数点
    return float(format(xc + pResx, '.1f')), float(format(yc + pResy, '.1f'))
    # return xc + pResx, yc + pResy


if __name__ == '__main__':
    root_path = '../annotation'
    file_list = os.listdir(root_path)
    for i in range(0, len(file_list)):
        if ('.xml' in file_list[i]) or ('.XML' in file_list[i]):
            voc_to_dota(root_path, file_list[i])
            print('----------------------------------------{}{}----------------------------------------'
                  .format(file_list[i], ' has Done!'))
        else:
            print(file_list[i] + ' is not xml file')

  • 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
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/722305
推荐阅读
相关标签
  

闽ICP备14008679号