栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

生成DOTA格式的数据集

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

生成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:datasetsarRSDDRSDD-SARJPEGImages'
ana_dir = r'D:datasetsarRSDDRSDD-SARAnnotations'
save_dir = r'D:datasetsarRSDDdota'
data_type = 'test'
train_img_dir = r'D:datasetsarRSDDRSDD-SARImageSetstest.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:GoogleEarthngsd:NaNn')
        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 + ' 0n')

train_img_dir 是JPEGImages中的图像,文件内容如下:

最后生成如下的文件

5 得到结果

运行img_split.py文件,得到最后生成结果:


over!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/951476.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号