from lxml import etree
import os
def parse_xml_to_dict(xml):
    """
    将xml文件解析成字典形式,参考tensorflow的recursive_parse_xml_to_dict
    Args:
        xml: xml tree obtained by parsing XML file contents using lxml.etree
    Returns:
        Python dictionary holding XML contents.
    """
    if len(xml) == 0:  
        return {xml.tag: xml.text}
    result = {}
    for child in xml:
        child_result = parse_xml_to_dict(child)  
        if child.tag != 'object':
            result[child.tag] = child_result[child.tag]
        else:
            if child.tag not in result:  
                result[child.tag] = []
            result[child.tag].append(child_result[child.tag])
    return {xml.tag: result}
a = sorted([i for i in os.listdir(r'你自己的\gt_xml')])
for path in a:
    with open(r'你自己的/gt_xml' + '/' + str(path)) as fid:
        xml_str = fid.read()
    xml = etree.fromstring(xml_str)  
    data = parse_xml_to_dict(xml)["annotation"]
    box = []
    for obj in data["object"]:
        xmin = int(obj["bndbox"]["xmin"])
        xmax = int(obj["bndbox"]["xmax"])
        ymin = int(obj["bndbox"]["ymin"])
        ymax = int(obj["bndbox"]["ymax"])
    data_width = xmax - xmin
    data_height = ymax - ymin
    box.append([xmin,ymin,data_width,data_height])
    write_line = "%d,%d,%d,%d\n" %(box[0][0],box[0][1],box[0][2], box[0][3])
    f = open("t.txt", "a+")
    f.write(write_line)
    f.close()
  
- 效果
   
                
                
                
        
    
 
 |