程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

(target detection) generate XML file script - Python

編輯:Python

( object detection ) Generate XML File script ——python

from xml.dom import minidom
def gen_xml_rect(xml_path, img_full_path, obj_list,width, height):
""" Generate rectangle dimension file :param xml_path: Mark the file path :param img_full_path: Image file path :param obj_list: Target location information list (xmin,ymin,xmax,ymax,label) :param width: Image width :param height: Image width """
# 1. establish DOM Tree object 
dom = minidom.Document()
# 2. Create a root node . Use... Every time DOM Object to create any node .
root_node = dom.createElement('annotation')
# 3. use DOM Object to add the root node 
dom.appendChild(root_node)
# use DOM Object to create element child nodes 
folder_node = dom.createElement('folder')
# Add element child nodes with parent node objects 
root_node.appendChild(folder_node)
img_folder_path=os.path.basename(img_full_path)
folder__node_text = dom.createTextNode(img_folder_path)
# Use the node object with text added ( As the parent node of the text node ) Add text node 
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:
# 4.writexml() The first parameter is the target file object , The second parameter is the indentation format of the root node , The third parameter is the indent format of other child nodes ,
# The fourth parameter sets the line feed format , The fifth parameter establishes xml Coding of content .
dom.writexml(fh, addindent=" ", newl="\n",encoding='UTF-8')
# print(' write in xml OK!')
except Exception as err:
print(' error message :{0}'.format(err))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved