【AI目标检测】VOC格式数据集转换为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>

想要转换成为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

转换代码:

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')

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
VOC数据转换为YOLO的txt数据集需要进行以下步骤: 1. 下载VOC数据集并解压,得到Annotations和JPEGImages两个文件夹。 2. 在Annotations文件夹中,每个xml文件对应一张图片,包含了该图片中所有物体的位置信息。需要将这些位置信息转换为YOLO格式。每个xml文件的内容如下: ``` <annotation> <folder>VOC2007</folder> <filename>000001.jpg</filename> <source> <database>The VOC2007 Database</database> <annotation>PASCAL VOC2007</annotation> <image>flickr</image> </source> <size> <width>353</width> <height>500</height> <depth>3</depth> </size> <object> <name>person</name> <pose>Left</pose> <truncated>1</truncated> <difficult>0</difficult> <bndbox> <xmin>174</xmin> <ymin>101</ymin> <xmax>349</xmax> <ymax>351</ymax> </bndbox> </object> ... </annotation> ``` 需要将每个物体的位置信息转换为YOLO格式,即将左上角和右下角的像素坐标转换为物体中心点相对于图片宽度和高度的比例,以及物体的宽度和高度相对于图片宽度和高度的比例。例如,上面的person物体转换后为: ``` 0 0.4641148 0.904 0.3116592 0.5 ``` 其中,第一个0表示该物体的类别编号,后面四个数分别表示物体中心点相对于图片宽度和高度的比例、物体宽度相对于图片宽度的比例、物体高度相对于图片高度的比例。 3. 编写Python脚本将所有xml文件转换为txt文件,并保存在同一目录下。脚本如下: ``` import os import xml.etree.ElementTree as ET classes = ["person", "car", "bike", ...] # 物体类别 def convert(size, box): dw = 1.0 / size[0] dh = 1.0 / size[1] x = (box[0] + box[1]) / 2.0 y = (box[2] + box[3]) / 2.0 w = box[1] - box[0] h = box[3] - box[2] x = x * dw w = w * dw y = y * dh h = h * dh return (x, y, w, h) def convert_annotation(image_id): in_file = open('Annotations/%s.xml' % (image_id)) out_file = open('labels/%s.txt' % (image_id), 'w') tree = ET.parse(in_file) root = tree.getroot() size = root.find('size') w = int(size.find('width').text) h = int(size.find('height').text) for obj in root.iter('object'): difficult = obj.find('difficult').text cls = obj.find('name').text if cls not in classes or int(difficult) == 1: continue cls_id = classes.index(cls) xmlbox = obj.find('bndbox') b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)) bb = convert((w,h), b) out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') if not os.path.exists('labels'): os.makedirs('labels') image_ids = open('ImageSets/Main/train.txt').read().strip().split() for image_id in image_ids: convert_annotation(image_id) ``` 该脚本将所有xml文件转换为txt文件,并保存在labels文件夹下。 4. 编写train.txt文件,其中每行为一张图片的文件名(不包括扩展名),保存在同一目录下。例如: ``` 000001 000002 000003 ... ``` 5. 最后,可以使用YOLO训练模型,使用转换后的txt数据集进行训练。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值