python标签TXT文件转XML

1.效果展示
txt文件
在这里插入图片描述
转换的xml文件
在这里插入图片描述

参考以下这篇文章在此基础上改动
https://blog.csdn.net/samylee/article/details/62040727

2.源代码

# -*- coding: utf-8 -*-
"""
Created on Mon Oct 14 16:15:04 2019

@author: 24281
"""

# -*- coding: utf-8 -*-
"""
Created on Fri Oct 11 20:06:45 2019

@author: 24281
"""

from xml.dom.minidom import Document
import os
import os.path
from PIL import Image
 
ann_path = "E:\\txt\\test\\"  #txt文件路径
img_path = "E:\\img\\test\\"  #图片路径
xml_path = "E:\\xml\\gxy\\"   #xml保存路径
 
if not os.path.exists(xml_path):
    os.mkdir(xml_path)
 
def writeXml(tmp, imgname, w, h, objbud, wxml):
    doc = Document()
  
    annotation = doc.createElement('annotation')
    doc.appendChild(annotation)
   
    folder = doc.createElement('folder')
    annotation.appendChild(folder)
    folder_txt = doc.createTextNode("ImageResource")
    folder.appendChild(folder_txt)
 
    filename = doc.createElement('filename')
    annotation.appendChild(filename)
    filename_txt = doc.createTextNode(imgname)
    filename.appendChild(filename_txt)
    
    path=doc.createElement('path')
    annotation.appendChild(path)
    path_txt=doc.createTextNode(img_path+imgname)
    path.appendChild(path_txt)
    
    source = doc.createElement('source')
    annotation.appendChild(source)
 
    database = doc.createElement('database')
    source.appendChild(database)
    database_txt = doc.createTextNode("Unknown")
    database.appendChild(database_txt)
 
    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)
  
    segmented = doc.createElement('segmented')
    annotation.appendChild(segmented)
    segmented_txt = doc.createTextNode("0")
    segmented.appendChild(segmented_txt)
 
    for i in range(0,len(objbud)//5):
        
        object_new = doc.createElement("object")
        annotation.appendChild(object_new)
 
        name = doc.createElement('name')
        object_new.appendChild(name)
        name_txt = doc.createTextNode(objbud[i*5])  
        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)
        xmin_txt = doc.createTextNode(objbud[i*5+1])#
        xmin.appendChild(xmin_txt)
 
        ymin = doc.createElement('ymin')
        bndbox.appendChild(ymin)
        ymin_txt = doc.createTextNode(objbud[i*5+2])#
        ymin.appendChild(ymin_txt)
 
        xmax = doc.createElement('xmax')
        bndbox.appendChild(xmax)
        xmax_txt = doc.createTextNode(objbud[i*5+3])#
        xmax.appendChild(xmax_txt)
 
        ymax = doc.createElement('ymax')
        bndbox.appendChild(ymax)
        ymax_txt = doc.createTextNode(objbud[i*5+4])#
        ymax.appendChild(ymax_txt)
       
    tempfile = tmp + "test.xml"   #
    with open(tempfile, "wb") as f:
        f.write(doc.toprettyxml(indent = '\t', 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(ann_path):
    temp = "E:\\temp\\"
    if not os.path.exists(temp):
        os.mkdir(temp)
    for file in files[2]:
        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])
        
        arr=[] 
        for line in open(ann_path + file):
            for x in line.split(' '):
             arr.append(x)
             obj=arr
        load_arr=list(map(float,arr))
        a=len(load_arr)
    
        print(a)
        if a==5:
            if load_arr[0]==0.0:
               load_arr[0]="excavator"
            if load_arr[0]==1.0:
               load_arr[0]="bulldozer" 
            if load_arr[0]==2.0:
               load_arr[0]="hoist"
            a1=load_arr[1]
            a2=load_arr[2]
            load_arr[1]=int(width*load_arr[1]-width*load_arr[3]*0.5)
            load_arr[2]=int(height*load_arr[2]-height*load_arr[4]*0.5)
            load_arr[3]=int(width*a1+width*load_arr[3]*0.5)
            load_arr[4]=int(height*a2+height*load_arr[4]*0.5)
        ob=[str(x) for x in load_arr]  #字符转换
        filename = xml_path + os.path.splitext(file)[0] + '.xml'
        writeXml(temp, img_name, width, height, ob, filename)
    os.rmdir(temp)
    
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值