欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

删除xml中不想要的类

程序员文章站 2022-05-24 09:49:53
...

代码功能:删除除了猫狗的其他类的标签框.

import xml.etree.cElementTree as ET
import os
path_root = ['./Annotations']
 
CLASSES = ["cat","dog"]
for anno_path in path_root:
    xml_list = os.listdir(anno_path)
    for axml in xml_list:
        path_xml = os.path.join(anno_path, axml)
        tree = ET.parse(path_xml)
        root = tree.getroot()
 
        for child in root.findall('object'):
            name = child.find('name').text
            if not name in CLASSES:
                root.remove(child)
 
        tree.write(os.path.join('./1', axml))