1.obb打标工具:
(roLabelImg) GitHub - cgvict/roLabelImg: Label Rotated Rect On Images for training
我是window环境,工具作者建议用PyQt4,但是PyQt4比较老,比较难适配,我用的是Python3.8+PyQt5,实测可以正常工作。
2.标签格式转换:
roLabelImg打出来的标签格式是:(未归一化的坐标)x y x y x y x y class difficuty(样本检测难易程度,yolov8-obb不需要这一项)
而yolov8-obb要求的格式是:class x y x y x y x y(归一化之后,并且小数位数控制在6位以内)
所以需要进行调整:下面提供我的转换代码
import os
from PIL import Image
####我的数据集结构
# datasets
# -train
# -images
# -labels
# -val
# -images
# -labels
#源标签文件夹
labelsPath=r'D:\xlx\ultralytics-main\ultralytics-main\datasets\nocare\train\labels-obb'
#转换后新标签文件夹
newLabelsPath=r'D:\xlx\ultralytics-main\ultralytics-main\datasets\nocare\train\labels'
#图像文件夹
imagesPath=r'D:\xlx\ultralytics-main\ultralytics-main\datasets\nocare\train\images'
if(not os.path.exists(labelsPath)):
os.makedirs(labelsPath)
if (not os.path.exists(newLabelsPath)):
os.makedirs(newLabelsPath)
if(not os.path.exists(imagesPath)):
os.makedirs(imagesPath)
labelList=os.listdir(labelsPath)
for file in labelList:
labelPath=os.path.join(labelsPath,file)
imagePath=os.path.join(imagesPath,file.split('.')[0]+'.jpg')
img=Image.open(imagePath)
w,h=img.size
newStrList = []
with open(labelPath,'r') as f:
strlist=[]
strlist=[x.strip().split() for x in f.readlines() if len(x)]
for i in strlist:
i[0]=str(round(float(i[0])/w,6))
i[1]=str(round(float(i[1])/h,6))
i[2]=str(round(float(i[2])/w,6))
i[3]=str(round(float(i[3])/h,6))
i[4]=str(round(float(i[4])/w,6))
i[5]=str(round(float(i[5])/h,6))
i[6]=str(round(float(i[6])/w,6))
i[7]=str(round(float(i[7])/h,6))
newi=list(i[0:9])
#调换坐标和类别顺序
newi[0]=i[8]
newi[1:9]=i[0:8]
#将转换后的每行存在新list中
newStrList.append(newi)
#将调整好的内容写入新标签文件,存在新标签文件夹中
with open(os.path.join(newLabelsPath,file),'w') as f:
for newStr in newStrList:
string=' '.join(newStr)+'\n'
f.write(string)