将普通label转化为voc格式

from lxml.etree import Element,SubElement,tostring
from xml.dom.minidom import parseString
import os


node_root   = Element('annotation')

node_folder = SubElement(node_root,'folder')
node_filename   = SubElement(node_root,'filename')
node_source = SubElement(node_root,'source')
node_size   = SubElement(node_root,'size')
node_segmented  = SubElement(node_root,'segmented')

node_database   = SubElement(node_source,'database')
node_width  = SubElement(node_size,'width')
node_height = SubElement(node_size,'height')
node_depth  = SubElement(node_size,'depth')

node_folder.text    = ''
node_filename.text  = ''
node_database.text  = 'Unknown'
node_width  = ''
node_height = ''
node_depth  = '3'
node_segmented  = '0'


object_num  = 3
for i in range(object_num):
    node_object = SubElement(node_root,'object')
    node_name   = SubElement(node_object,'name')
    node_pose   = SubElement(node_object,'pose')
    node_truncated  = SubElement(node_object,'truncated')
    node_difficult  = SubElement(node_object,'difficult')
    node_bndbox = SubElement(node_object,'bndbox')
    node_xmin   = SubElement(node_bndbox,'xmin')
    node_ymin   = SubElement(node_bndbox,'ymin')
    node_xmax   = SubElement(node_bndbox,'xmax')
    node_ymax   = SubElement(node_bndbox,'ymax')

    node_name.text  = ''
    node_pose.text  = 'Unspecified'
    node_truncated.text = '0'
    node_difficult.text = '0'
    node_xmin   = ''
    node_ymin   = ''
    node_xmax   = ''
    node_ymax   = ''


xml = tostring(node_root,pretty_print=True)
dom = parseString(xml)
with open('aa.xml','wb') as f:
    f.write(xml)

你可能感兴趣的:(python)