基础学习 3_YOLO、VOC的数据格式转换

主要参考:
YOLO数据格式说明与转换
【YOLOV5-5.x 源码解读】general.py

YOLO数据格式说明

在这里插入图片描述
yolo标注格式保存在.txt文件中,一共5个数据,用空格隔开,举例说明如下图所示:在这里插入图片描述

数据格式转换

假设图像的宽和高(真实宽高)分别为 w、h,bbox的左上角坐标为(x1, y2),右下角坐标为(x2, y2),则可求得bbox中心坐标(x', y')为:

x' = x1 + (x2 - x1)/2 = (x1 + x2)/2
y' = y1 + (y2 - y1)/2 = (y1 + y2)/2

设yolo的5个数据分别为:label, x', y', w', h',则
VOC——>YOLO 有对应关系:

x' = (x1 + x2) / 2 / w = (x1 + x2) / (2w) 
y' = (y1 + y2) / 2 / h = (y1 + y2) / (2h) 
w' = (x2 - x1) / w
h' = (y2 - y1) / h

YOLO——>VOC 有对应关系:

x1 = w * x' - 0.5 * w * w' = w * ( x' - 0.5 * w' )
y1 = h * y' - 0.5 * h * h' = h * ( x' - 0.5 * h' )

x2 = w * x' + 0.5 * w * w'= w * ( x' + 0.5 * w' )
y2 = h * y' + 0.5* h * h' = h * ( x' + 0.5 * h' )

代码实现:

具体栗子可以移步我的另一篇博客:图像数据增强2_标注框同时修改(VOC、YOLO)

xywh2xyxy.py(VOC、YOLO格式转换)

这部分是从Yolov5官方开源的代码中 截取一部分出来的
更详细说明可以参考【YOLOV5-5.x 源码解读】general.py

import cv2
import numpy as np
import pandas as pd
import pkg_resources as pkg
import torch
import torchvision
import yaml


def clip_coords(boxes, shape):
    # Clip bounding xyxy bounding boxes to image shape (height, width)
    '''
    将boxes的坐标(x1y1x2y2左上角右下角)限定在图像的尺寸(img_shapehw)内,防止出界。
    这个函数会用在下面的xyxy2xywhn、save_one_boxd等函数中,很重要,必须掌握
    '''
    if isinstance(boxes, torch.Tensor):  # faster individually
        boxes[:, 0].clamp_(0, shape[1])  # x1
        boxes[:, 1].clamp_(0, shape[0])  # y1
        boxes[:, 2].clamp_(0, shape[1])  # x2
        boxes[:, 3].clamp_(0, shape[0])  # y2
    else:  # np.array (faster grouped)
        boxes[:, [0, 2]] = boxes[:, [0, 2]].clip(0, shape[1])  # x1, x2
        boxes[:, [1, 3]] = boxes[:, [1, 3]].clip(0, shape[0])  # y1, y2


def xyxy2xywh(x):
    # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] where xy1=top-left, xy2=bottom-right
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[:, 0] = (x[:, 0] + x[:, 2]) / 2  # x center
    y[:, 1] = (x[:, 1] + x[:, 3]) / 2  # y center
    y[:, 2] = x[:, 2] - x[:, 0]  # width
    y[:, 3] = x[:, 3] - x[:, 1]  # height
    return y


def xywh2xyxy(x):
    # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[:, 0] = x[:, 0] - x[:, 2] / 2  # top left x
    y[:, 1] = x[:, 1] - x[:, 3] / 2  # top left y
    y[:, 2] = x[:, 0] + x[:, 2] / 2  # bottom right x
    y[:, 3] = x[:, 1] + x[:, 3] / 2  # bottom right y
    return y


def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0):
    # Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[:, 0] = w * (x[:, 0] - x[:, 2] / 2) + padw  # top left x
    y[:, 1] = h * (x[:, 1] - x[:, 3] / 2) + padh  # top left y
    y[:, 2] = w * (x[:, 0] + x[:, 2] / 2) + padw  # bottom right x
    y[:, 3] = h * (x[:, 1] + x[:, 3] / 2) + padh  # bottom right y
    return y


def xyxy2xywhn(x, w=640, h=640, clip=False, eps=0.0):
    # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] normalized where xy1=top-left, xy2=bottom-right
    if clip:
        clip_coords(x, (h - eps, w - eps))  # warning: inplace clip
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[:, 0] = ((x[:, 0] + x[:, 2]) / 2) / w  # x center
    y[:, 1] = ((x[:, 1] + x[:, 3]) / 2) / h  # y center
    y[:, 2] = (x[:, 2] - x[:, 0]) / w  # width
    y[:, 3] = (x[:, 3] - x[:, 1]) / h  # height
    return y


def xyn2xy(x, w=640, h=640, padw=0, padh=0):
    # Convert normalized segments into pixel segments, shape (n,2)
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[:, 0] = w * x[:, 0] + padw  # top left x
    y[:, 1] = h * x[:, 1] + padh  # top left y
    return y
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将YOLO格式数据集转换为VOC格式数据集,需要按照以下步骤进行操作: 1. 根据YOLO格式数据集的标注文件,将每个图像中的目标对象的位置、类别和置信度信息提取出来,存储在一个文本文件中。每行包含一个目标对象的信息,格式如下: ``` <class> <x_center> <y_center> <width> <height> ``` 其中,`<class>`表示目标对象所属的类别,`<x_center>`和`<y_center>`表示目标对象中心点在图像中的坐标,`<width>`和`<height>`表示目标对象的宽度和高度,所有这些值都是相对于图像大小的比例。 2. 将每个图像的文件名和对应的标注文件名存储在一个XML文件中,格式如下: ``` <annotation> <folder>image_folder</folder> <filename>image_name.jpg</filename> <size> <width>image_width</width> <height>image_height</height> <depth>3</depth> </size> <object> <name>object_class</name> <bndbox> <xmin>xmin_value</xmin> <ymin>ymin_value</ymin> <xmax>xmax_value</xmax> <ymax>ymax_value</ymax> </bndbox> </object> ... </annotation> ``` 其中,`<folder>`表示图像文件所在的文件夹,`<filename>`表示图像文件名,`<width>`和`<height>`表示图像的宽度和高度,`<object>`表示一个目标对象,`<name>`表示目标对象所属的类别,`<bndbox>`表示目标对象的边界框,`<xmin>`、`<ymin>`、`<xmax>`和`<ymax>`分别表示边界框左上角和右下角的坐标。 3. 将所有XML文件和对应的图像文件存储在一个文件夹中,这样就得到了一个VOC格式数据集。 需要注意的是,YOLO格式数据集和VOC格式数据集的标注信息格式不同,因此需要进行格式转换。此外,VOC格式数据集还需要包含图像文件本身,因此需要将YOLO格式数据集中的图像文件也复制到VOC格式数据集中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值