voc数据集转yolov5数据格式

from xml.etree import ElementTree as ET
import os

xml_path = r"F:\traffic_signs_yolov5\Annotations"
label_path = r"F:\traffic_signs_yolov5\labels\train"
image_path = r"F:\traffic_signs_yolov5\images\train"

class_name = []

for xml_file in os.listdir(xml_path):
    xml2txt_name = os.path.splitext(xml_file)[0] + '.txt'

    tree = ET.parse(os.path.join(xml_path,xml_file))
    root = tree.getroot()
    sign_object = root.findall('object')
    img_size = root.find("size")
    img_width = int(img_size.find("width").text)
    img_height = int(img_size.find("height").text)

    if sign_object:
        for sign in sign_object:
            name = sign.find('name')
            sign_name = name.text
            if sign_name not in class_name:
                class_name.append(sign_name)

            class_id = class_name.index(sign_name)

            bndbox = sign.find('bndbox')
            xmin = float(bndbox[0].text)
            ymin = float(bndbox[1].text)
            xmax = float(bndbox[2].text)
            ymax = float(bndbox[3].text)

            width = (xmax - xmin) / img_width
            height = (ymax - ymin) / img_height
            x_center = (xmin + (xmax - xmin)/2) / img_width
            y_center = (ymin + (ymax - ymin)/2) / img_height

            with open(os.path.join(label_path,xml2txt_name), 'a') as f:
                f.write("{} {} {} {} {}\n".format(class_id, x_center, y_center, width, height))

    else:
        #with open(os.path.join(label_path,xml2txt_name),'a') as f:
        #    f.write("")
        remove_img_name = os.path.splitext(xml_file)[0] + '.jpg'
        if os.path.exists(os.path.join(image_path,remove_img_name)):
            os.remove(os.path.join(image_path,remove_img_name))
            print("remove {}".format(remove_img_name))


print("class_name:",class_name)

要将VOC数据集转换Yolov5格式,需要进行以下步骤: 1. 下载并安装Yolov5:首先,你需要在你的机器上下载和安装Yolov5。你可以从Yolov5的GitHub存储库获取代码和详细的安装说明。 2. 准备VOC数据集:确保你已经下载和准备好了VOC数据集。这个数据集包含了图像文件和相应的标注文件。 3. 创建Yolov5标注文件格式:Yolov5使用自定义的标注文件格式,而不是VOC数据集中的XML格式。你需要将VOC数据集中的每个图像的标注信息转换Yolov5格式。 Yolov5的标注文件格式如下: ``` class_index x_center y_center width height ``` - class_index:目标类别的索引,从0开始 - x_center, y_center:目标边界框中心的归一化坐标(相对于图像宽度和高度) - width, height:目标边界框的归一化宽度和高度(相对于图像宽度和高度) 例如,如果你有一个类别为"dog"的目标,边界框中心点坐标为(100, 200),宽度为50,高度为100,图像的宽度和高度为500,那么对应的Yolov5标注行为: ``` 0 0.2 0.4 0.1 0.2 ``` 4. 将VOC数据集转换Yolov5格式:使用脚本将VOC数据集转换Yolov5格式。Yolov5的作者提供了一个用于数据集转换的脚本,在Yolov5的GitHub存储库中可以找到。你可以使用该脚本将所有图像的标注信息转换Yolov5格式。 运行脚本的命令如下: ``` python path/to/yolov5/scripts/voc2yolo.py --data path/to/data.yaml ``` 其中,`path/to/yolov5`是你安装Yolov5的路径,`path/to/data.yaml`是包含数据集信息的Yaml文件的路径。 5. 获取Yolov5格式的数据集转换完成后,你将得到一个包含Yolov5格式标注的数据集。这个数据集可以直接用于训练Yolov5模型。 请注意,以上步骤是基于Yolov5官方提供的脚本和标注文件格式。如果你的需求有所不同,你可能需要进行自定义修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

布响哒公

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值