VOC格式数据集标签转化为YOLO格式标签

VOC格式数据集标签转化为YOLO格式标签

import os
import xml.etree.ElementTree as ET

def convert_voc_to_yolo(voc_xml_path, yolo_txt_path, class_id_map):
    tree = ET.parse(voc_xml_path)
    root = tree.getroot()

    size = root.find('size')
    img_width = float(size.find('width').text)
    img_height = float(size.find('height').text)

    with open(yolo_txt_path, 'w') as yolo_file:
        for obj in root.findall('object'):
            class_name = obj.find('name').text
            if class_name not in class_id_map:
                continue

            class_id = class_id_map[class_name]

            bbox = obj.find('bndbox')
            x_min = float(bbox.find('xmin').text)
            y_min = float(bbox.find('ymin').text)
            x_max = float(bbox.find('xmax').text)
            y_max = float(bbox.find('ymax').text)

            x_center = (x_min + x_max) / (2.0 * img_width)
            y_center = (y_min + y_max) / (2.0 * img_height)
            width = (x_max - x_min) / img_width
            height = (y_max - y_min) / img_height

            yolo_line = f"{class_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}\n"
            yolo_file.write(yolo_line)

def batch_convert_voc_to_yolo(input_folder, output_folder, class_id_map):
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    for filename in os.listdir(input_folder):
        if filename.endswith('.xml'):
            voc_xml_path = os.path.join(input_folder, filename)
            yolo_txt_path = os.path.join(output_folder, os.path.splitext(filename)[0] + '.txt')
            convert_voc_to_yolo(voc_xml_path, yolo_txt_path, class_id_map)

if __name__ == "__main__":
    input_folder = r"F:\HSR_Date\zc\anotations_dj"
    output_folder = r"F:\HSR_Date\zc\labels_dj"
    # class_id_map = {"Brace sleeve-f": 0 , "Brace sleeve-b": 0}  # Update with your class names and IDs
    class_id_map = {"DJLS": 0}  # Update with your class names and IDs

    batch_convert_voc_to_yolo(input_folder, output_folder, class_id_map)

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值