首先介绍一下如何制作带有角度的YOLOv5数据集标注。
先去这个网站下标注软件代码GitHub - cgvict/roLabelImg: Label Rotated Rect On Images for training
随后为其创建独立的虚拟环境
conda create -n rolabel36 python=3.6
激活环境
conda activate rolabel36
安装对应的依赖包
pip install pyqt5-tools
pip install lxml
pyrcc5 -o resources.py resources.qrc
在项目根目录下运行打开标注软件的程序
python roLabelImg.py
选中需要标注的文件夹即可对数据集进行标注,标注示例如下图所示。
然而软件标注的格式是xml格式,网络训练需要的标注格式是txt文件,因此需要把xml文件转换为txt格式。以下代码可以实现这个转换,只需要把XMLDIR和OUTDIR改为自己的路径即可。
import os
import os.path as osp
import math
BASEDIR = osp.dirname(osp.abspath(__file__)) #获取的当前执行脚本的完整路径
XMLDIR = osp.join(BASEDIR, r'C:\Users\BJUT\Desktop\label')
OUTDIR = osp.join(BASEDIR, r'C:\Users\BJUT\Desktop\labels')
if not osp.exists(OUTDIR):
os.makedirs(OUTDIR)
# pi=3.1415926
xmlnames = [i for i in os.listdir(XMLDIR) if i.endswith('.xml')]
# print(names)
pi = 3.1415926
# 转换成四点坐标
def convert(cx, cy, w, h, a):
if a >= pi:
a -= pi
# 计算斜径半长
l = math.sqrt(w ** 2 + h ** 2) / 2
# 计算初始矩形角度
a0 = math.atan(h / w)
# 旋转,计算旋转角
# 右上角点 ↗
a1 = a0 + a
x1 = cx + l * math.cos(a1)
y1 = cy + l * math.sin(a1)
# 右下角点 ↘
a2 = -a0 + a
x2 = cx + l * math.cos(a2)
y2 = cy + l * math.sin(a2)
# 左下角点 ↙
a3 = a1 + pi