使用Python将yolov5的推理保存的txt文本格式转换为XML格式

目标检测是计算机视觉中的基础任务,可以让机器在图像中识别和定位物体。为了高效地标注和存储目标检测数据,我们使用了各种不同的数据格式。其中一种常用的格式是XML结构,通常用于VOC数据集格式。在本文中,我们将探讨一个Python脚本,它可以将目标检测标注从特定的文本文件格式转换为XML格式。

问题描述

假设您有一个图像数据集,同时还有相应的文本文件,其中包含目标检测的标注信息。每个文本文件都提供了有关图像中存在的物体的信息。例如,在每个文本文件中,每行可能包含物体类别、边界框坐标等信息。然而,您希望将这些标注信息转换为XML格式,以便更好地组织和管理数据。

Python脚本实现

以下是一个使用Python编写的脚本,用于将目标检测标注从文本格式转换为XML格式:

python 复制代码
# 导入所需库
import os
import cv2
import shutil
from xml.dom.minidom import Document

# 定义物体类别
cls_names = ['人', '狗']

def makexml(picPath, txtPath, xmlPath):
    # 删除已有的XML保存路径,并创建新的路径
    if os.path.exists(xmlPath):
        shutil.rmtree(xmlPath)
    os.makedirs(xmlPath)

    # 获取图片文件列表
    files = os.listdir(picPath)

    # 遍历每个图片文件
    for i, name in enumerate(files):
        ss = os.path.join(picPath, name)
        print(ss)
        img = cv2.imread(ss)
        Pheight, Pwidth, Pdepth = img.shape

        # 创建XML文档对象
        xmlBuilder = Document()
        annotation = xmlBuilder.createElement("annotation")  # 创建annotation标签
        xmlBuilder.appendChild(annotation)

        # 添加folder标签
        folder = xmlBuilder.createElement("folder")
        folderContent = xmlBuilder.createTextNode("VOC2007")
        folder.appendChild(folderContent)
        annotation.appendChild(folder)

        # 添加filename标签
        filename = xmlBuilder.createElement("filename")
        filenameContent = xmlBuilder.createTextNode(name)
        filename.appendChild(filenameContent)
        annotation.appendChild(filename)

        # 添加size标签
        size = xmlBuilder.createElement("size")
        width = xmlBuilder.createElement("width")
        widthContent = xmlBuilder.createTextNode(str(Pwidth))
        width.appendChild(widthContent)
        size.appendChild(width)
        height = xmlBuilder.createElement("height")
        heightContent = xmlBuilder.createTextNode(str(Pheight))
        height.appendChild(heightContent)
        size.appendChild(height)
        depth = xmlBuilder.createElement("depth")
        depthContent = xmlBuilder.createTextNode(str(Pdepth))
        depth.appendChild(depthContent)
        size.appendChild(depth)
        annotation.appendChild(size)

        # 读取对应的文本文件
        txt = os.path.join(txtPath, name[0:-3] + "txt")
        if not os.path.exists(txt):
            # 若文本文件不存在,则直接保存XML文件并处理下一张图片
            print(txt)
            f = open(os.path.join(xmlPath, name[0:-4] + '.xml'), 'w', encoding="utf-8")
            xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
            f.close()
            continue

        # 若文本文件存在,则读取文本内容并添加相应的标签
        txtFile = open(txt)
        txtList = txtFile.readlines()

        for i in txtList:
            oneline = i.strip().split(" ")
            object = xmlBuilder.createElement("object")
            picname = xmlBuilder.createElement("name")
            nameContent = xmlBuilder.createTextNode(cls_names[int(oneline[0])])
            picname.appendChild(nameContent)
            object.appendChild(picname)

            # 添加pose、truncated、difficult等标签
            # 添加bndbox标签,并计算bounding box的坐标
            # 将以上标签添加至annotation标签下
             pose = xmlBuilder.createElement("pose")
            poseContent = xmlBuilder.createTextNode("Unspecified")
            pose.appendChild(poseContent)
            object.appendChild(pose)
            truncated = xmlBuilder.createElement("truncated")
            truncatedContent = xmlBuilder.createTextNode("0")
            truncated.appendChild(truncatedContent)
            object.appendChild(truncated)
            difficult = xmlBuilder.createElement("difficult")
            difficultContent = xmlBuilder.createTextNode("0")
            difficult.appendChild(difficultContent)
            object.appendChild(difficult)
            bndbox = xmlBuilder.createElement("bndbox")
            xmin = xmlBuilder.createElement("xmin")
            mathData = int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)
            xminContent = xmlBuilder.createTextNode(str(mathData))
            xmin.appendChild(xminContent)
            bndbox.appendChild(xmin)
            ymin = xmlBuilder.createElement("ymin")
            mathData = int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)
            yminContent = xmlBuilder.createTextNode(str(mathData))
            ymin.appendChild(yminContent)
            bndbox.appendChild(ymin)
            xmax = xmlBuilder.createElement("xmax")
            mathData = int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)
            xmaxContent = xmlBuilder.createTextNode(str(mathData))
            xmax.appendChild(xmaxContent)
            bndbox.appendChild(xmax)
            ymax = xmlBuilder.createElement("ymax")
            mathData = int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)
            ymaxContent = xmlBuilder.createTextNode(str(mathData))
            ymax.appendChild(ymaxContent)
            bndbox.appendChild(ymax)
            object.appendChild(bndbox)
            annotation.appendChild(object)

        # 保存生成的XML文件
        f = open(os.path.join(xmlPath, name[0:-4] + '.xml'), 'w', encoding="utf-8")
        xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
        f.close()

# 调用函数进行转换
makexml("../data/images/", "../runs/detect/exp19/labels/", "../data/all/annotations/")

结论

通过使用上述Python脚本,您可以将yolov5的推理保存的txt从文本格式转换为更为常见的XML格式,从而更好地管理和组织您的数据集。转换后的XML格式可以被广泛用于各种目标检测算法和数据处理工具中,为计算机视觉研究和应用提供便利。

相关推荐
冷月半明5 分钟前
时间序列篇:Prophet负责优雅,LightGBM负责杀疯
python·算法
教练我想打篮球_基本功重塑版32 分钟前
L angChain 加载大模型
python·langchain
Coovally AI模型快速验证34 分钟前
YOLOv8-SMOT:基于切片辅助训练与自适应运动关联的无人机视角小目标实时追踪框架
人工智能·深度学习·yolo·计算机视觉·目标跟踪·无人机
是Dream呀1 小时前
YOLOv7:重新定义实时目标检测的技术突破
yolo·目标检测·目标跟踪
跟橙姐学代码3 小时前
手把手教你玩转 multiprocessing,让程序跑得飞起
前端·python·ipython
LCS-3123 小时前
Python爬虫实战: 爬虫常用到的技术及方案详解
开发语言·爬虫·python
穷儒公羊3 小时前
第二章 设计模式故事会之策略模式:魔王城里的勇者传说
python·程序人生·设计模式·面试·跳槽·策略模式·设计规范
心本无晴.3 小时前
面向过程与面向对象
python
花妖大人3 小时前
Python用法记录
python·sqlite
站大爷IP3 小时前
用PyQt快速搭建桌面应用:从零到实战的实用指南
python