IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 【数据处理】YOLO格式的标记文件转成VOC格式(txt转xml) -> 正文阅读

[人工智能]【数据处理】YOLO格式的标记文件转成VOC格式(txt转xml)

【数据处理】YOLO格式的标记文件转成VOC格式(txt转xml)

1、YOLO格式

yolo数据的格式一般将目标信息保存在txt文件中。
yolo格式
每一行为一个目标的信息,分别为类别,归一化的(x,y,w,h)

2、VOC格式

在这里插入图片描述以下是格式转化代码

'''
功能:实现YOLO格式的标注txt文件转化为voc格式
作者:月半咬人狼
转载请注明出处
'''
import os
from xml.dom.minidom import Document


def write_xml(Jfilename, imageWidth, imageHeight, Jpath, obj):
    doc = Document()
    annotation = doc.createElement("annotation")
    doc.appendChild(annotation)

    folder = doc.createElement("folder")
    foldertext = doc.createTextNode("JPEGImages")
    folder.appendChild(foldertext)
    annotation.appendChild(folder)

    filename = doc.createElement("filename")
    filenametext = doc.createTextNode(Jfilename)
    filename.appendChild(filenametext)
    annotation.appendChild(filename)

    path = doc.createElement("path")
    pathtext = doc.createTextNode(Jpath)
    path.appendChild(pathtext)
    annotation.appendChild(path)

    sourcename = "source"
    sourceE = doc.createElement(sourcename)

    database = doc.createElement("database")
    databasetext = doc.createTextNode("Unknown")
    database.appendChild(databasetext)
    sourceE.appendChild(database)
    annotation.appendChild(sourceE)

    sizename = "size"
    sizeE = doc.createElement(sizename)

    width = doc.createElement("width")
    widthtext = doc.createTextNode(str(imageWidth))
    width.appendChild(widthtext)
    sizeE.appendChild(width)

    height = doc.createElement("height")
    heighttext = doc.createTextNode(str(imageHeight))
    height.appendChild(heighttext)
    sizeE.appendChild(height)

    depth = doc.createElement("depth")
    depthtext = doc.createTextNode("1")
    depth.appendChild(depthtext)
    sizeE.appendChild(depth)

    annotation.appendChild(sizeE)

    segmented = doc.createElement("segmented")
    segmentedtext = doc.createTextNode("0")
    segmented.appendChild(segmentedtext)
    annotation.appendChild(segmented)
    for i in range(len(obj)):
        obj_list = obj[i].split(" ")

        Jname = obj_list[0]
        x = float(obj_list[1]) * 256
        y = float(obj_list[2]) * 256
        w = float(obj_list[3]) * 256
        h = float(obj_list[4]) * 256
        Jxmin = str(x - w / 2)
        Jxmax = str(x + w / 2)
        Jymin = str(y - h / 2)
        Jymax = str(y + h / 2)

        objectname = "object"
        objectE = doc.createElement(objectname)

        name = doc.createElement("name")
        nametext = doc.createTextNode(Jname)
        name.appendChild(nametext)
        objectE.appendChild(name)

        pose = doc.createElement("pose")
        posetext = doc.createTextNode("Unspecifiel")
        pose.appendChild(posetext)
        objectE.appendChild(pose)

        truncated = doc.createElement("truncated")
        truncatedtext = doc.createTextNode("0")
        truncated.appendChild(truncatedtext)
        objectE.appendChild(truncated)

        difficult = doc.createElement("difficult")
        difficulttext = doc.createTextNode("0")
        difficult.appendChild(difficulttext)
        objectE.appendChild(difficult)

        bndboxname = "bndbox"
        bndboxE = doc.createElement(bndboxname)

        xmin = doc.createElement("xmin")
        xmintext = doc.createTextNode(Jxmin)
        xmin.appendChild(xmintext)
        bndboxE.appendChild(xmin)

        ymin = doc.createElement("ymin")
        ymintext = doc.createTextNode(Jymin)
        ymin.appendChild(ymintext)
        bndboxE.appendChild(ymin)

        xmax = doc.createElement("xmax")
        xmaxtext = doc.createTextNode(Jxmax)
        xmax.appendChild(xmaxtext)
        bndboxE.appendChild(xmax)

        ymax = doc.createElement("ymax")
        ymaxtext = doc.createTextNode(Jymax)
        ymax.appendChild(ymaxtext)
        bndboxE.appendChild(ymax)
        objectE.appendChild(bndboxE)

        annotation.appendChild(objectE)

    f = open(os.path.join(xml_path, xml_name), "w")
    doc.writexml(f, indent='\t', newl='\n', addindent='\t', encoding=None)
    f.close()


if __name__ == "__main__":
    txt_path = r"./test_anno/"    # 存放txt地址
    xml_path = r"./annotation"    # xml保存地址
    img_path = r"./test_img/"     # 图像存放地址

    txt_files = os.listdir(txt_path)
    image_files = os.listdir(img_path)
    obj_num = 0
    for file in txt_files:
        txt_list = file.split(".")

        with open(os.path.join(txt_path, file), 'r') as load_f:
            lines = load_f.read().split("\n")
            print(lines)
            obj = lines[:len(lines) - 1]
            Jpath = os.path.join(img_path, txt_list[0] + '.jpg')
            xml_name = txt_list[0] + '.xml'
            Jfilename = txt_list[0]
            imageWidth = '256'
            imageHeight = '256'
            write_xml(Jfilename, imageWidth, imageHeight, Jpath, obj)

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-08-05 17:21:26  更:2021-08-05 17:22:03 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/8 5:55:31-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码