笔者因科研任务需要使用ETH行人数据集,网上搜到的数据集格式转换代码有问题,在这里修改了一些细节部分,以做记录。
import os
import xml.dom.minidom as minidom
with open("sunnyday/refined.idl") as file_obj:#这里输入idl文件所在目录
for line in file_obj:
line_break = line.strip(";\n").split(":")
img_name = line_break[0].split('/')[1].strip('"')#split('/')[1]这里的参数‘1’适用于eth01和eth03,对于eth02就要修改成7
print(img_name)
if len(line_break):
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)
index=1
for i in range(len(line_break)):
if i>=index and i<len(line_break)-1:
# index+=1
p_d=line_break[i]
xz=p_d.replace(' ','')
xz=xz.strip('(').strip(')')
zx=[xz]
people_position = line_break[i].split('(')[1]
people_position = [people_position.split(")")[0]]
# del people_position[-1]
for single in people_position:
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("sunnyday/xml/", img_name) + ".xml"#更换成自己xml输出的目录
with open(path, 'w') as f:
# 缩进 - 换行 - 编码
doc.writexml(f, addindent=' ', newl='\n')
print('success!')