生成DOTA格式的数据集

1 安装工具箱

生成dota数据集需要该工具箱

2 更改参数

下载安装好后,配置BboxToolkit/tools路径下的split_configs/dota1_0/ss_train.json文件。
根据需求更改参数。

3 更改类别

更改BboxToolkit/BboxToolkit/datasets/misc.py文件中的dota1_0中的类别在这里插入图片描述

4 准备数据

现在有xywhθ标注的数据集,要转换成dota标注的格式
在这里插入图片描述
使用如下代码,路径需要自己修改:

import math
import shutil
import os
import numpy as np
import xml.etree.ElementTree as et

dataset_dir = r'D:\dataset\sar\RSDD\RSDD-SAR\JPEGImages'
ana_dir = r'D:\dataset\sar\RSDD\RSDD-SAR\Annotations'
save_dir = r'D:\dataset\sar\RSDD\dota'
data_type = 'test'
train_img_dir = r'D:\dataset\sar\RSDD\RSDD-SAR\ImageSets\test.txt'
f1 = open(train_img_dir, 'r')
train_img = f1.readlines()

def rota(center_x1, center_y1, x, y, w, h, a):  # 旋转中心点,旋转中心点,框的w,h,旋转角

    # a = (math.pi * a) / 180  # 角度转弧度
    x1, y1 = x - w / 2, y - h / 2  # 旋转前左上
    x2, y2 = x + w / 2, y - h / 2  # 旋转前右上
    x3, y3 = x + w / 2, y + h / 2  # 旋转前右下
    x4, y4 = x - w / 2, y + h / 2  # 旋转前左下

    px1 = (x1 - center_x1) * math.cos(a) - (y1 - center_y1) * math.sin(a) + center_x1  # 旋转后左上
    py1 = (x1 - center_x1) * math.sin(a) + (y1 - center_y1) * math.cos(a) + center_y1
    px2 = (x2 - center_x1) * math.cos(a) - (y2 - center_y1) * math.sin(a) + center_x1  # 旋转后右上
    py2 = (x2 - center_x1) * math.sin(a) + (y2 - center_y1) * math.cos(a) + center_y1
    px3 = (x3 - center_x1) * math.cos(a) - (y3 - center_y1) * math.sin(a) + center_x1  # 旋转后右下
    py3 = (x3 - center_x1) * math.sin(a) + (y3 - center_y1) * math.cos(a) + center_y1
    px4 = (x4 - center_x1) * math.cos(a) - (y4 - center_y1) * math.sin(a) + center_x1  # 旋转后左下
    py4 = (x4 - center_x1) * math.sin(a) + (y4 - center_y1) * math.cos(a) + center_y1

    return px1, py1, px2, py2, px3, py3, px4, py4  # 旋转后的四个点,左上,右上,右下,左下


for img in train_img:
    shutil.copy(os.path.join(dataset_dir, img[:-1] + '.jpg'),
                os.path.join(save_dir, data_type, 'images', img[:-1] + '.jpg'))

    xml_file = open(os.path.join(ana_dir, img[:-1] + '.xml'), encoding='utf-8')
    tree = et.parse(xml_file)
    root = tree.getroot()
    with open(os.path.join(save_dir, data_type, 'labelTxt', img[:-1] + '.txt'), 'w') as f:
        f.write('imagesource:GoogleEarth\ngsd:NaN\n')
        for obj in root.iter('object'):
            cls = obj.find('name').text
            box = obj.find('robndbox')
            x_c = box.find('cx').text
            y_c = box.find('cy').text
            h = box.find('h').text
            w = box.find('w').text
            theta = box.find('angle').text
            box = list(map(np.float16, [x_c, y_c, h, w, theta]))
            box = rota(box[0], box[1], *box)
            box = list(map(int, box))
            box = list(map(str, box))
            f.write(' '.join(box))
            f.write(' ' + cls + ' 0\n')

train_img_dir 是JPEGImages中的图像,文件内容如下:
在这里插入图片描述
最后生成如下的文件
在这里插入图片描述
在这里插入图片描述

5 得到结果

运行img_split.py文件,得到最后生成结果:
在这里插入图片描述
在这里插入图片描述
over!

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值