python中的txt文件转换为XML
作者:LGDDDDDD 发布时间:2021-12-05 10:45:48
标签:txt文件,XML,python
txt文件转换为XML
很多目标检测的模型都是默认需要VOC的文件输入格式
手上数据label是txt文件。
为了避免不必要的bug,还是选择转换下格式
将数据按VOC形式放置
文件夹 | 内容 |
---|---|
Annotations | 存放生成的XML文件 |
JPEGImages | JPG图片 |
ImageSets | 标明训练集测试集的txt文件 |
Labelss | txt格式的Label文件 |
# -*- coding: utf-8 -*-
from xml.dom.minidom import Document
import os
import os.path
from PIL import Image
import importlib
import sys
importlib.reload(sys)
xml_path = "Annotations\\"
img_path = "JPEGImages\\"
ann_path = "Labelss\\"
if not os.path.exists(xml_path):
os.mkdir(xml_path)
def writeXml(tmp, imgname, w, h, objbud, wxml):
doc = Document()
# owner
annotation = doc.createElement('annotation')
doc.appendChild(annotation)
# owner
folder = doc.createElement('folder')
annotation.appendChild(folder)
folder_txt = doc.createTextNode("VOC2007")
folder.appendChild(folder_txt)
filename = doc.createElement('filename')
annotation.appendChild(filename)
filename_txt = doc.createTextNode(imgname)
filename.appendChild(filename_txt)
# ones#
source = doc.createElement('source')
annotation.appendChild(source)
database = doc.createElement('database')
source.appendChild(database)
database_txt = doc.createTextNode("The VOC2007 Database")
database.appendChild(database_txt)
annotation_new = doc.createElement('annotation')
source.appendChild(annotation_new)
annotation_new_txt = doc.createTextNode("PASCAL VOC2007 ")
annotation_new.appendChild(annotation_new_txt)
image = doc.createElement('image')
source.appendChild(image)
image_txt = doc.createTextNode("flickr")
image.appendChild(image_txt)
# onee#
# twos#
size = doc.createElement('size')
annotation.appendChild(size)
width = doc.createElement('width')
size.appendChild(width)
width_txt = doc.createTextNode(str(w))
width.appendChild(width_txt)
height = doc.createElement('height')
size.appendChild(height)
height_txt = doc.createTextNode(str(h))
height.appendChild(height_txt)
depth = doc.createElement('depth')
size.appendChild(depth)
depth_txt = doc.createTextNode("3")
depth.appendChild(depth_txt)
# twoe#
segmented = doc.createElement('segmented')
annotation.appendChild(segmented)
segmented_txt = doc.createTextNode("0")
segmented.appendChild(segmented_txt)
# threes#
object_new = doc.createElement("object")
annotation.appendChild(object_new)
name = doc.createElement('name')
object_new.appendChild(name)
name_txt = doc.createTextNode('cancer')
name.appendChild(name_txt)
pose = doc.createElement('pose')
object_new.appendChild(pose)
pose_txt = doc.createTextNode("Unspecified")
pose.appendChild(pose_txt)
truncated = doc.createElement('truncated')
object_new.appendChild(truncated)
truncated_txt = doc.createTextNode("0")
truncated.appendChild(truncated_txt)
difficult = doc.createElement('difficult')
object_new.appendChild(difficult)
difficult_txt = doc.createTextNode("0")
difficult.appendChild(difficult_txt)
# threes-1#
bndbox = doc.createElement('bndbox')
object_new.appendChild(bndbox)
xmin = doc.createElement('xmin')
bndbox.appendChild(xmin)
#objbud存放[类别,xmin,ymin,xmax,ymax]
xmin_txt = doc.createTextNode(objbud[1])
xmin.appendChild(xmin_txt)
ymin = doc.createElement('ymin')
bndbox.appendChild(ymin)
ymin_txt = doc.createTextNode(objbud[2])
ymin.appendChild(ymin_txt)
xmax = doc.createElement('xmax')
bndbox.appendChild(xmax)
xmax_txt = doc.createTextNode(objbud[3])
xmax.appendChild(xmax_txt)
ymax = doc.createElement('ymax')
bndbox.appendChild(ymax)
ymax_txt = doc.createTextNode(objbud[4])
ymax.appendChild(ymax_txt)
# threee-1#
# threee#
tempfile = tmp + "test.xml"
with open(tempfile, "wb") as f:
f.write(doc.toprettyxml(indent="\t", newl="\n", encoding="utf-8"))
rewrite = open(tempfile, "r")
lines = rewrite.read().split('\n')
newlines = lines[1:len(lines) - 1]
fw = open(wxml, "w")
for i in range(0, len(newlines)):
fw.write(newlines[i] + '\n')
fw.close()
rewrite.close()
os.remove(tempfile)
return
for files in os.walk('E:\ssd_pytorch_cancer\data\cancer_or_not\Labels'):
print(files)
temp = "/temp/"
if not os.path.exists(temp):
os.mkdir(temp)
for file in files[2]:
print(file + "-->start!")
img_name = os.path.splitext(file)[0] + '.jpg'
fileimgpath = img_path + img_name
im = Image.open(fileimgpath)
width = int(im.size[0])
height = int(im.size[1])
filelabel = open(ann_path + file, "r")
lines = filelabel.read().split(' ')
obj = lines[:len(lines)]
filename = xml_path + os.path.splitext(file)[0] + '.xml'
writeXml(temp, img_name, width, height, obj, filename)
os.rmdir(temp)
不过代码只使用于每个label文件只有一个标注框,可在生成bndbox节点处加入循环
来源:https://blog.csdn.net/weixin_43289424/article/details/106371995


猜你喜欢
- 最近在备考软考的软件设计师考试,在学习过程遇到很多于计算机基础计算相关的知识点,正好最近在学Go语言,所以就把计算的方式用Go语言实现一下。
- 前言:任何一个编程者都少不了要去调试代码,不管你是高手还是菜鸟,调试程序都是一项必不可少的工作。一般来说调试程序是在编写代码之后或测试期修改
- 我们网站的静态资源(css、js和背景图片)和web应用程序是分开部署的,几乎所有的静态资源都部署在同一个应用下。最开始的网站
- throttle我们这里说的throttle就是函数节流的意思。再说的通俗一点就是函数调用的频度控制器,是连续执行时间间隔控制。主要应用的场
- 1 概述C/C++和Java(以及大多数的主流编程语言)都有自己成熟的单元测试框架,前者如Check,后者如JUnit,但这些编程框架本质上
- 引言 上一篇介绍完了观察者模式的原理,本篇想就此再介绍一个小应用,虽然我也就玩了一下午,是当时看observer正好找到的,以及还有Djan
- 安装流程:前期准备工作--->安装ORACLE软件--->安装升级补丁--->安装odbc创建数据库--->安装监听
- 近期将公司的MySQL架构升级了,由原先的一主多从换成了DRBD+Heartbeat双主多从,正好手上有一个电子商务网站新项目也要上线了,用
- 如下所示:#!/usr/bin/python#-*-coding:utf-8-*-import _winreg as wr #导入内置的wi
- 最近,接手的项目里,提供的数据文件格式简直让人看不下去,使用pandas打不开
- split() 通过指定分隔符对字符串进行切片,如果第二个参数 num 有指定值,则分割为 num+1 个子字符串。split()方法定义于
- 描述print函数是Python的内置函数,它会将对象的__repr__特殊函数返回的字符串打印输出。默认情况下,print函数调用底层的s
- MySQL root密码正确,却怎么也无法从本地登录MySQL,提示ERROR 1045 (28000): Access denied fo
- MySQL的异常处理分析如下:标准格式DECLARE handler_type HANDLER FOR condition_value[,.
- Oracle sql语句执行日志查询在Oracle数据中,我们经常编写sql语句,有时我们会编写一些特别长的sql语句,而有一些意外导致sq
- 目录简介Spare data的例子SparseArraySparseDtypeSparse的属性Sparse的计算SparseSeries
- 默认情况下Python的logging模块将日志打印到了标准输出中,且只显示了大于等于WARNING级别的日志,这说明默认的日志级别设置为W
- 本文实例讲述了python实现查找两个字符串中相同字符并输出的方法。分享给大家供大家参考。具体实现方法如下:seq1 = "spa
- 本文实例讲述了Python使用try except处理程序异常的三种常用方法。分享给大家供大家参考,具体如下:如果你在写python程序时遇
- HTML文件其实就是由一组尖括号构成的标签组织起来的,每一对尖括号形式一个标签,标签之间存在上下关系,形成标签树;XPath 使用路径表达式