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)
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇