网络编程
位置:首页>> 网络编程>> Python编程>> python中的txt文件转换为XML

python中的txt文件转换为XML

作者:LGDDDDDD  发布时间:2021-12-05 10:45:48 

标签:txt文件,XML,python

txt文件转换为XML

很多目标检测的模型都是默认需要VOC的文件输入格式

手上数据label是txt文件。

为了避免不必要的bug,还是选择转换下格式

将数据按VOC形式放置

python中的txt文件转换为XML

文件夹内容
Annotations存放生成的XML文件
JPEGImagesJPG图片
ImageSets标明训练集测试集的txt文件
Labelsstxt格式的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

0
投稿

猜你喜欢

  • 1. 准备工作下载源码包wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.b
  • 对url进行编码在服务器端我们可以使用asp中的server.urlencode,很方便实现。如:<% ss="asp之家欢
  • 我们通常会通过单击按钮的操作,将定义好的内容直接复制到剪贴板对于用户来说点了按钮直接【Ctrl】+【V】就可以了。其实该功能的核心原理就是用
  • 这些包可以独立使用,也可以与其他包一起使用以满足复杂的业务需求。Integration Services 可以提取和转换来自多种源(如 XM
  • 如何做一个文本书写器?我们有下面的的函数,可做“文本书写器”:<%function WriteToFile(FileName
  • 我们经常会遇到多重查询问题,而长长的SQL语句往往让人丈二和尚摸不着头脑。特别是客户端部分填入查询条件时,如用普通方法将更是难上加难。以下巧
  • 什么是1433端口 1433端口,是SQL Server默认的端口,SQL Server服务使用两个端口:TCP-1433、UDP-1434
  • PHP PDO 事务与自动提交现在通过 PDO 连接上了,在开始进行查询前,必须先理解 PDO 是如何管理事务的。事务支持四大特性(ACID
  • 从小的方面讲,帮助一般是指:手册、说明书、文档、FAQ 等等。从大的方面讲,可以是交互过程中的提示、指引、演示等信息,帮助无处不在!这一切,
  • 想要一个这玩意,可是找了网上许多着色器,要么是兼容性成问题,要么是匹配不精确,比如说:1、注释里包含字符串、关键词,类似于:/* xxxx&
  • 一、正则表达式的作用提示:正则表达式是一段特殊的字符串,它表示的是一段有规律的信息。如果我们想从一段文字中提取想要的内容,就可以通过正则表达
  • <!--#include file="conn/conn.asp"--> <% set Newslis
  • 内容摘要:除了内部性能增强和优化外,IIS6.0版本的 Active Server Pages(ASP)&nb
  • 最近朋友需要一个可以识别图片中的文字的程序,以前做过java验证码识别的程序;刚好最近在做一个python项目,所以顺便用Python练练手
  • 相关文章推荐:各种loading加载图标下载 gif格式loadinfo和ajaxload一样,也是一个在线Ajax载入动画生成工
  • 因为他的简单实用和数量庞大的插件,所以我们喜欢和使用jQuery,在jQuery数量庞大的插件中有很大的一部分是关于图片的,所以今天我们就整
  • 在计算机信息技术中,少有象类似 XML 那样的技术,她让人们产生范围很大的联想,而这些联想及其应用又可能相距甚远。她也更多引起人们对 XML
  • 在Google Reader上看到网友分享的一个链接,真的发现自己已经out了。上面的这张图,是纯CSS实现的,没有背景图、没有Javasc
  • 第一步:安装SQL200,并启动SQL2000。到网上下载SQL2000,并安装完毕。( * 作系统是XP,装的是SQL个人版),按照下面所示
  • 做python项目时,想安装某个依赖包的最新版本,但又不知道它的版本号具体到多少,因此需要搜索查看它的相关简介信息.原来的时候,可以直接通过
手机版 网络编程 asp之家 www.aspxhome.com