【python3】批量删除voc数据集xml文件里的某些节点,得到单独某一类别的数据集(代码清晰,易操作!)

举例说明,比如说是VOC 2007 train+val,只留下人和车类,其他类别去除掉:
  1. 下面代码去除掉xml文件中,不需要的 类别的 节点:
    注意:使用代码时路径均使用绝对路径;
import xml.etree.cElementTree as ET
import os

# VOC 2007 train+val
path_root = "/*/VOCdevkit/VOC2007/Annotations/"
 
CLASSES = ["person","car"]
xml_list = os.listdir(path_root)
count = 0

for axml in xml_list:
    path_xml = os.path.join(path_root, 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("/*/VOCdevkit/VOC2007/Annotations1/", axml))
    print(axml)
    count = count + 1
    
print(count)
  1. 下面代码用于删掉刚才去除其他节点后不包含人和车类的xml文件
  • 如果使用的是tensorflow object detection API来进行voc数据集到tfrecord的转换需要记得更新/*/trainval/VOCdevkit/VOC2007/ImageSets/Main/aeroplane_trainval.txt文件为新的xml文件对应的图片情况;
# 1.delete the xml that no person or car object 
# 2.update /*/trainval/VOCdevkit/VOC2007/ImageSets/Main/aeroplane_trainval.txt
# for transer to tfrecord
path_root = "/*/VOCdevkit/VOC2007/Annotations1/"
 

xml_list = os.listdir(path_root)
count = 0

for axml in xml_list:
    path_xml = os.path.join(path_root, axml)
    tree = ET.parse(path_xml)
    root = tree.getroot()
    size = len(root.findall('object'))
    #print(size)
    if size == 0:
        print(axml + " " +'\\')
    else:
        count = count + 1
        print(axml[0:6])
    
print(count)
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值