VOC数据集格式转YOLO格式

发布于:2025-02-10 ⋅ 阅读:(33) ⋅ 点赞:(0)

在这里插入图片描述

将VOC格式的数据集转换为YOLO格式通常涉及以下几个步骤。YOLO格式的标注文件是每个图像对应一个.txt文件,文件中每一行表示一个目标,格式为:

<class_id> <x_center> <y_center> <width> <height>

其中:

  • <class_id> 是类别的索引(从0开始)。
  • <x_center><y_center> 是边界框中心的归一化坐标(相对于图像宽度和高度)。
  • <width><height> 是边界框的归一化宽度和高度(相对于图像宽度和高度)。

转换步骤

  1. 解析VOC格式的标注文件
    VOC格式的标注文件通常是XML格式,包含图像尺寸、目标类别、边界框坐标等信息。

  2. 计算YOLO格式的归一化坐标
    根据VOC格式中的边界框坐标和图像尺寸,计算YOLO格式的归一化坐标。

  3. 生成YOLO格式的标注文件
    为每个图像生成一个对应的.txt文件,写入转换后的标注信息。

示例代码

以下是一个Python脚本示例,用于将VOC格式的标注文件转换为YOLO格式:

import os
import xml.etree.ElementTree as ET

def convert_voc_to_yolo(voc_annotation_path, output_dir, class_list):
    # 解析VOC格式的XML文件
    tree = ET.parse(voc_annotation_path)
    root = tree.getroot()

    # 获取图像尺寸
    size = root.find('size')
    img_width = int(size.find('width').text)
    img_height = int(size.find('height').text)

    # 创建YOLO格式的标注文件
    image_name = os.path.splitext(os.path.basename(voc_annotation_path))[0]
    yolo_annotation_path = os.path.join(output_dir, f"{image_name}.txt")

    with open(yolo_annotation_path, 'w') as yolo_file:
        for obj in root.findall('object'):
            # 获取类别名称
            class_name = obj.find('name').text
            if class_name not in class_list:
                continue  # 如果类别不在class_list中,跳过

            # 获取类别ID
            class_id = class_list.index(class_name)

            # 获取边界框坐标
            bbox = obj.find('bndbox')
            xmin = float(bbox.find('xmin').text)
            ymin = float(bbox.find('ymin').text)
            xmax = float(bbox.find('xmax').text)
            ymax = float(bbox.find('ymax').text)

            # 计算YOLO格式的归一化坐标
            x_center = (xmin + xmax) / 2 / img_width
            y_center = (ymin + ymax) / 2 / img_height
            width = (xmax - xmin) / img_width
            height = (ymax - ymin) / img_height

            # 写入YOLO格式的标注文件
            yolo_file.write(f"{class_id} {x_center} {y_center} {width} {height}\n")

# 示例使用
voc_annotations_dir = "path/to/voc/annotations"
yolo_output_dir = "path/to/yolo/annotations"
class_list = ["class1", "class2", "class3"]  # 类别列表

# 确保输出目录存在
os.makedirs(yolo_output_dir, exist_ok=True)

# 遍历VOC标注文件并转换
for voc_annotation_file in os.listdir(voc_annotations_dir):
    if voc_annotation_file.endswith(".xml"):
        voc_annotation_path = os.path.join(voc_annotations_dir, voc_annotation_file)
        convert_voc_to_yolo(voc_annotation_path, yolo_output_dir, class_list)

说明

  • voc_annotations_dir:VOC格式的标注文件目录。
  • yolo_output_dir:YOLO格式的标注文件输出目录。
  • class_list:类别列表,用于将类别名称映射到类别ID。

注意

  • 确保VOC标注文件中的类别名称与class_list中的名称一致。
  • 图像尺寸信息必须正确,否则归一化坐标会出错。
  • 生成的YOLO标注文件与图像文件同名,只是扩展名不同(.txt)。

通过以上步骤和代码,你可以将VOC格式的数据集转换为YOLO格式,以便在YOLO模型中使用。


网站公告

今日签到

点亮在社区的每一天
去签到