批量刪除VOC的XML中的某些節點

import xml.etree.cElementTree as ET
import os
path_root = ['E:\data-VOC0712\VOC2007\Annotations',
             'E:\data-VOC0712\VOC2012\Annotations']

CLASSES = [
           "bottle",
           "cat", "chair",  "diningtable",
           "dog", "motorbike", "person",
           "pottedplant","sofa",
           "tvmonitor"]
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('E:\data-myVOC0712\Annotations', axml))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章