yolo标注文件txt转xml格式——最简单

只需要在代码中添加txt文件地址和xml保存地址然后一键运行即可

import os
import xml.etree.ElementTree as ET
from xml.dom import minidom

def convert_yolo_to_xml(yolo_folder, xml_folder):
    if not os.path.exists(xml_folder):
        os.makedirs(xml_folder)

    for txt_file in os.listdir(yolo_folder):
        if txt_file.endswith('.txt'):
            xml_file = os.path.splitext(txt_file)[0] + '.xml'
            txt_path = os.path.join(yolo_folder, txt_file)
            xml_path = os.path.join(xml_folder, xml_file)

            with open(txt_path, 'r') as f:
                lines = f.readlines()

            # Create XML structure
            annotation = ET.Element('annotation')

            # Create filename node
            filename = ET.SubElement(annotation, 'filename')
            filename.text = os.path.splitext(txt_file)[0] + '.jpg'  # Assuming jpg images

            # Create size node
            size = ET.SubElement(annotation, 'size')
            width = ET.SubElement(size, 'width')
            height = ET.SubElement(size, 'height')
            depth = ET.SubElement(size, 'depth')

            # Set your image dimensions here
            width.text = '1920'
            height.text = '1080'
            depth.text = '3'  # Assuming RGB images

            for line in lines:
                class_index, x_center, y_center, bbox_width, bbox_height = map(float, line.split())

                # Convert YOLO format to VOC format
                xmin = int((x_center - bbox_width / 2) * 1920)
                ymin = int((y_center - bbox_height / 2) * 1080)
                xmax = int((x_center + bbox_width / 2) * 1920)
                ymax = int((y_center + bbox_height / 2) * 1080)

                # Create object node
                obj = ET.SubElement(annotation, 'object')
                name = ET.SubElement(obj, 'name')
                bbox = ET.SubElement(obj, 'bndbox')
                xmin_elem = ET.SubElement(bbox, 'xmin')
                ymin_elem = ET.SubElement(bbox, 'ymin')
                xmax_elem = ET.SubElement(bbox, 'xmax')
                ymax_elem = ET.SubElement(bbox, 'ymax')

                name.text = str(int(class_index))  # Convert class index to class name if needed
                xmin_elem.text = str(xmin)
                ymin_elem.text = str(ymin)
                xmax_elem.text = str(xmax)
                ymax_elem.text = str(ymax)

            # Create XML tree and write to file
            xmlstr = minidom.parseString(ET.tostring(annotation)).toprettyxml(indent="   ")
            with open(xml_path, "w") as f:
                f.write(xmlstr)

            print(f'Converted {txt_file} to {xml_file}')

yolo_folder = "yolo标注文件txt地址"
xml_folder = "生成的xml文件保存地址"
convert_yolo_to_xml(yolo_folder, xml_folder)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值