代码展示
import xml.dom.minidom
# 写xml文件。
def creat_xml(image_name, image_path, xml_path, width, height, managerList):
doc = xml.dom.minidom.Document()
root = doc.createElement('annotation')
doc.appendChild(root)
nodefolder = doc.createElement('folder')
nodefolder.appendChild(doc.createTextNode("VOC2007"))
root.appendChild(nodefolder)
nodefilename = doc.createElement('filename')
nodefilename.appendChild(doc.createTextNode(image_name))
root.appendChild(nodefilename)
nodepath = doc.createElement('path')
nodepath.appendChild(doc.createTextNode(image_path))
root.appendChild(nodepath)
nodesource = doc.createElement('source')
nodedatabase = doc.createElement('database')
nodedatabase.appendChild(doc.createTextNode("Unknown"))
nodesource.appendChild(nodedatabase)
root.appendChild(nodesource)
nodesize = doc.createElement('size')
nodewidth = doc.createElement('width')
nodewidth.appendChild(doc.createTextNode(str(width)))
nodeheight = doc.createElement('height')
nodeheight.appendChild(doc.createTextNode(str(height)))
nodedepth = doc.createElement('depth')
nodedepth.appendChild(doc.createTextNode(str(3)))
nodesize.appendChild(nodewidth)
nodesize.appendChild(nodeheight)
nodesize.appendChild(nodedepth)
root.appendChild(nodesize)
for i in range(len(managerList)):
nodeobject = doc.createElement('object')
nodename = doc.createElement('name')
#记录标签名字
nodename.appendChild(doc.createTextNode(managerList[i][4]))
nodepose = doc.createElement('pose')
nodepose.appendChild(doc.createTextNode('Unspecified'))
nodetruncated = doc.createElement('truncated')
nodetruncated.appendChild(doc.createTextNode('0'))
nodedifficult = doc.createElement('difficult')
nodedifficult.appendChild(doc.createTextNode('0'))
nodebndbox = doc.createElement('bndbox')
nodexmin = doc.createElement('xmin')
#左上角的横坐标
nodexmin.appendChild(doc.createTextNode(str(managerList[i][1])))
#左上角的纵坐标
nodeymin = doc.createElement('ymin')
nodeymin.appendChild(doc.createTextNode(str(managerList[i][0])))
#右下角的横坐标
nodexmax = doc.createElement('xmax')
nodexmax.appendChild(doc.createTextNode(str(managerList[i][3])))
#右下角的纵坐标
nodeymax = doc.createElement('ymax')
nodeymax.appendChild(doc.createTextNode(str(managerList[i][2])))
nodebndbox.appendChild(nodexmin)
nodebndbox.appendChild(nodeymin)
nodebndbox.appendChild(nodexmax)
nodebndbox.appendChild(nodeymax)
nodeobject.appendChild(nodename)
nodeobject.appendChild(nodepose)
nodeobject.appendChild(nodetruncated)
nodeobject.appendChild(nodedifficult)
nodeobject.appendChild(nodebndbox)
root.appendChild(nodeobject)
fp = open(os.path.join(xml_path, image_name[:-3] + 'xml'), 'w')
doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
if __name__ == "__main__":
creat_xml(img_name,os.path.join(dir_save_path, img_name),os.path.join(xml_save_path),width,height,out_boxes)
代码解释
注:代码无法直接运行 只是骨干代码
1.首先导入 xml.dom.minidom 库。
2. 调用函数creat_xml 生成xml文件。
creat_xml 函数 参数解释
1.img_name:图像文件名字
2. image_path:图像完整路径
3. xml_path:xml文件的保存路径
4. width:图像的宽度
5. height:图像的高度
6. managerList: 数据格式如下(记得看代码注释。)
[[ 526.5313 809.7515 624.607 847.394 "label"]
[ 118.86459 1158.7527 186.91324 1208.3164 "label"]
[ 86.163086 1270.4873 151.6998 1355.0828 "label"]
[1066.3881 627.704 1139.1725 672.5404 "label"]
[ 404.68246 818.03143 464.3928 854.34467 "label"]
[ 721.30334 1547.5111 807.04926 1596.5492 "label"]
[ 372.2357 1466.9078 435.7041 1508.1536 "label"]
[ 676.0221 629.3024 721.3986 693.4014 "label"]
[1152.097 843.35645 1197.9905 892.74475 "label"]]