yolov8-obb训练,标签格式注意事项

部署运行你感兴趣的模型镜像

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)

您可能感兴趣的与本文相关的镜像

Yolo-v5

Yolo-v5

Yolo

YOLO(You Only Look Once)是一种流行的物体检测和图像分割模型,由华盛顿大学的Joseph Redmon 和Ali Farhadi 开发。 YOLO 于2015 年推出,因其高速和高精度而广受欢迎

### YOLOv8OBB 自动标注的使用方法 目前,YOLOv8及其扩展版本(如YOLOv8OBB)并未内置官方支持的自动标注功能[^1]。然而,在实际应用中可以通过结合其他工具或技术实现图像的初步自动化标注,然后再将其转换为适合YOLOv8OBB使用的格式。 以下是关于如何利用外部工具完成自动标注并适配到YOLOv8OBB的具体说明: #### 1. 利用第三方工具进行自动标注 可以借助一些现有的开源项目来实现自动标注的功能。例如: - **Roboflow**: 提供了一个强大的在线平台,能够基于预训练模型快速生成标注文件,并导出为多种格式,包括YOLO格式- **LabelImg 或 Supervisely**: 这些工具有一定的半自动化能力,允许用户加载预训练的目标检测模型来进行初始预测,从而减少手动操作的时间成本。 如果选择 Roboflow,则需上传原始图片至其网站上运行推理过程;而 LabelImg 和 Supervisely 可本地部署,更适合隐私敏感场景下的开发需求。 #### 2. 转换标注格式YOLO OBB 格式 一旦获得了基础矩形框形式的结果之后,就需要进一步调整这些数据使其满足 YOLO OBB 的特殊要求——即由八个数值组成的旋转边界盒表示法[class_index, x1, y1, x2, y2, x3, y3, x4, y4][^3] 。此步骤通常涉及编写自定义脚本来解析原生标签结构并将之映射成新的模式。 下面给出一段 Python 示例代码用于演示这一转化逻辑: ```python import numpy as np def convert_to_obb_format(rectangles): obb_annotations = [] for rect in rectangles: cx, cy, w, h, angle = rect # Calculate corner points based on center point and dimensions. corners = calculate_corners(cx, cy, w, h, angle) # Normalize coordinates between 0 to 1 assuming input size is known (e.g., img_width=640,img_height=480). normalized_corners = normalize_coordinates(corners, img_width=640, img_height=480) # Assuming all objects belong to same class '0'. annotation_line = f"{class_id} {' '.join(map(str,normalized_corners))}" obb_annotations.append(annotation_line) return obb_annotations def calculate_corners(center_x, center_y, width, height, rotation_angle_degrees): """Convert bounding box parameters into four corner points.""" radians = math.radians(rotation_angle_degrees) cos_val = math.cos(radians) sin_val = math.sin(radians) half_w = width / 2 half_h = height / 2 offsets = [ (-half_w, -half_h), (+half_w, -half_h), (+half_w, +half_h), (-half_w, +half_h)] rotated_offsets = [(offset[0]*cos_val-offset[1]*sin_val, offset[0]*sin_val+offset[1]*cos_val) for offset in offsets] abs_coords = [[center_x+x, center_y+y] for x,y in rotated_offsets] return abs_coords def normalize_coordinates(coords_list, img_width, img_height): normed = [[x/img_width, y/img_height] for x,y in coords_list] flat_normed = [item for sublist in normed for item in sublist] return flat_normed ``` 上述函数接受标准矩形参数列表作为输入(`rectangles`),其中每个元素都是五元组 `(cx,cy,w,h,angle)` ,分别代表中心位置、宽度高度以及角度信息。最终输出的是符合 YOLO OBB 需求的一系列字符串行。 #### 3. 整合进整个工作流 最后一步就是把经过处理后的标注文件重新导入回您的训练环境中去。这可能意味着更新配置文档中的路径设置或者直接替换掉旧版 txt 文件等内容。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值