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!