import glob
import os
from shutil import copy
import xml.dom.minidom as minidom
with open("F:/pedestrian/seq03-img-left.idl") as file_obj:
for line in file_obj:
line_break = line.strip(";\n").split(":")
img_name = line_break[0].strip("\"").strip("left/")
if len(line_break) == 2:
doc = minidom.Document()##在内存中创建一个空的xml 文档
root = doc.createElement("annotation")
doc.appendChild(root)
# filename
filename_node = doc.createElement("filename")
filename_value = doc.createTextNode(img_name)
filename_node.appendChild(filename_value)
root.appendChild(filename_node)
# size
size_node = doc.createElement("size")
width_node = doc.createElement("width")
width_value = doc.createTextNode("640")
width_node.appendChild(width_value)
height_node = doc.createElement("height")
height_value = doc.createTextNode("480")
height_node.appendChild(height_value)
depth_node = doc.createElement("depth")
depth_value = doc.createTextNode("3")
depth_node.appendChild(depth_value)
size_node.appendChild(width_node)
size_node.appendChild(height_node)
size_node.appendChild(depth_node)
root.appendChild(size_node)
people_position = line_break[1].replace(" ", '')
people_position = people_position.split(")")
del people_position[-1]
for single in people_position:
single = single.strip(",(")
single = single.split(',')
print(single)
# object
object_node = doc.createElement("object")
name_node = doc.createElement("name")
name_value = doc.createTextNode("person")
name_node.appendChild(name_value)
object_node.appendChild(name_node)
bndbox_node = doc.createElement("bndbox")
xmin_node = doc.createElement("xmin")
xmin_value = doc.createTextNode(single[0])
xmin_node.appendChild(xmin_value)
xmax_node = doc.createElement("xmax")
xmax_value = doc.createTextNode(single[2])
xmax_node.appendChild(xmax_value)
ymin_node = doc.createElement("ymin")
ymin_value = doc.createTextNode(single[1])
ymin_node.appendChild(ymin_value)
ymax_node = doc.createElement("ymax")
ymax_value = doc.createTextNode(single[3])
ymax_node.appendChild(ymax_value)
bndbox_node.appendChild(xmin_node)
bndbox_node.appendChild(xmax_node)
bndbox_node.appendChild(ymin_node)
bndbox_node.appendChild(ymax_node)
object_node.appendChild(bndbox_node)
root.appendChild(object_node)
img_name = img_name.strip(".png")
path = os.path.join("F:/pedestrian/seq03-img-left",img_name) + ".xml"
with open(path, 'w') as f:
# 缩进 - 换行 - 编码
doc.writexml(f, addindent=' ', newl='\n')
ETH行人检测数据集,annotation转换xml
最新推荐文章于 2023-03-18 16:30:06 发布