(目标检测)生成XML文件脚本——python
from xml.dom import minidom
def gen_xml_rect(xml_path, img_full_path, obj_list,width, height):
"""
生成矩形框标注文件
:param xml_path: 标注文件路径
:param img_full_path: 图像文件路径
:param obj_list: 目标位置信息列表 (xmin,ymin,xmax,ymax,label)
:param width:图像宽
:param height:图像宽
"""
dom = minidom.Document()
root_node = dom.createElement('annotation')
dom.appendChild(root_node)
folder_node = dom.createElement('folder')
root_node.appendChild(folder_node)
img_folder_path=os.path.basename(img_full_path)
folder__node_text = dom.createTextNode(img_folder_path)
folder_node.appendChild(folder__node_text)
filename_node = dom.createElement('filename')
root_node.appendChild(filename_node)
file_name=img_full_path.split('\\')[-1]
filename_node_text = dom.createTextNode(file_name)
filename_node.appendChild(filename_node_text)
path_node = dom.createElement('path')
root_node.appendChild(path_node)
path_node_text = dom.createTextNode(img_full_path)
path_node.appendChild(path_node_text)
source_node = dom.createElement('source')
root_node.appendChild(source_node)
database_node = dom.createElement('database')
source_node.appendChild(database_node)
database_node_text = dom.createTextNode('Unknown')
database_node.appendChild(database_node_text)
size_node = dom.createElement('size')
root_node.appendChild(size_node)
width_node = dom.createElement('width')
size_node.appendChild(width_node)
width_node_text = dom.createTextNode(str(width))
width_node.appendChild(width_node_text)
height_node = dom.createElement('height')
size_node.appendChild(height_node)
height_node_text = dom.createTextNode(str(height))
height_node.appendChild(height_node_text)
depth_node = dom.createElement('depth')
size_node.appendChild(depth_node)
depth_node_node_text = dom.createTextNode(str(3))
depth_node.appendChild(depth_node_node_text)
segmented_node = dom.createElement('segmented')
root_node.appendChild(segmented_node)
segmented_node_text = dom.createTextNode(str(0))
segmented_node.appendChild(segmented_node_text)
for obj in obj_list:
object_node = dom.createElement('object')
root_node.appendChild(object_node)
name_node = dom.createElement('name')
object_node.appendChild(name_node)
name_node_text = dom.createTextNode(str(obj[4]))
name_node.appendChild(name_node_text)
pose_node = dom.createElement('pose')
object_node.appendChild(pose_node)
pose_node_text = dom.createTextNode('Unspecified')
pose_node.appendChild(pose_node_text)
truncated_node = dom.createElement('truncated')
object_node.appendChild(truncated_node)
truncated_node_text = dom.createTextNode(str(0))
truncated_node.appendChild(truncated_node_text)
difficult_node = dom.createElement('difficult')
object_node.appendChild(difficult_node)
difficult_node_text = dom.createTextNode('0')
difficult_node.appendChild(difficult_node_text)
bndbox_node = dom.createElement('bndbox')
object_node.appendChild(bndbox_node)
xmin_node = dom.createElement('xmin')
bndbox_node.appendChild(xmin_node)
xmin_node_text = dom.createTextNode(str(obj[0]))
xmin_node.appendChild(xmin_node_text)
ymin_node = dom.createElement('ymin')
bndbox_node.appendChild(ymin_node)
ymin_node_text = dom.createTextNode(str(obj[1]))
ymin_node.appendChild(ymin_node_text)
xmax_node = dom.createElement('xmax')
bndbox_node.appendChild(xmax_node)
xmax_node_text = dom.createTextNode(str(obj[2]))
xmax_node.appendChild(xmax_node_text)
ymax_node = dom.createElement('ymax')
bndbox_node.appendChild(ymax_node)
ymax_node_text= dom.createTextNode(str(obj[3]))
ymax_node.appendChild(ymax_node_text)
try:
with open(xml_path, 'w', encoding='UTF-8') as fh:
dom.writexml(fh, addindent=" ", newl="\n",encoding='UTF-8')
except Exception as err:
print('错误信息:{0}'.format(err))