参考自https://blog.youkuaiyun.com/pursuit_zhangyu/article/details/107719037
import os
import os.path
import xml.etree.ElementTree as ET
headstr = """\
<annotation>
<folder>VOC</folder>
<filename>%s</filename>
<path>zhangyt</path>
<source>
<database>My Database</database>
</source>
<size>
<width>%d</width>
<height>%d</height>
<depth>%d</depth>
</size>
<segmented>0</segmented>
"""
objstr = """\
<object>
<name>%s</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>%d</xmin>
<ymin>%d</ymin>
<xmax>%d</xmax>
<ymax>%d</ymax>
</bndbox>
</object>
"""
tailstr = '''\
</annotation>
'''
def write_xml(anno_path,head, objs, tail):
f = open(anno_path, "w")
f.write(head)
for obj in objs:
f.write(objstr%(obj[0],obj[1],obj[2],obj[3],obj[4]))
f.write(tail)
def convertxml(path_xml,files, save_xml):
for xmlFile in files:
dataset = []
print(xmlFile)
f = open(os.path.join(path_xml,xmlFile))
xml_text = f.read()
root = ET.fromstring(xml_text)
f.close()
image_name = xmlFile.replace("xml", "jpg")
# name = root.find('path').text
width = int(root.findtext("./size/width"))
height = int(root.findtext("./size/height"))
depth = int(root.findtext("./size/depth"))
head=headstr % (image_name, width, height, depth)
for obj in root.iter("object"):
name = str(obj.findtext("name"))
xmin = int(obj.findtext("bndbox/xmin"))
ymin = int(obj.findtext("bndbox/ymin"))
xmax = int(obj.findtext("bndbox/xmax"))
ymax = int(obj.findtext("bndbox/ymax"))
if xmax == xmin or ymax == ymin:
print(xmlFile)
dataset.append([name, xmin, ymin, xmax, ymax])
tail = tailstr
write_xml(os.path.join(save_xml, xmlFile),head, dataset, tail)
if __name__ == "__main__":
path_xml = "Annotations/"
save_xml = "Annotations_name/"
if not os.path.exists(save_xml):
os.mkdir(save_xml)
xmlFile = os.listdir(path_xml)
convertxml(path_xml,xmlFile,save_xml)