python实用脚本(二):删除xml标签下的指定类别

介绍

在目标检测中,有些时候会遇到标注好的类别不想要了的情况,这时我们可以运行下面的代码来批量删除不需要的类别节省时间。

代码实现:

python 复制代码
import argparse

import xml.etree.ElementTree as ET
import os

classes = ['thin_smoke']


def GetImgNameByEveryDir(file_dir, videoProperty):
    FileNameWithPath, FileName, FileDir = [], [], []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] in videoProperty:
                FileNameWithPath.append(os.path.join(root, file))  # 保存图片路径
                FileName.append(file)  # 保存图片名称
                FileDir.append(root[len(file_dir):])  # 保存图片所在文件夹
    return FileName, FileNameWithPath, FileDir


def GetBoxInfo(xmlfile):
    try:
        tree = ET.parse(xmlfile)
        root = tree.getroot()
        size = root.find('size')
        w = int(size.find('width').text)
        h = int(size.find('height').text)
    except:
        return False, 0
    else:
        tree = ET.parse(xmlfile)
        root = tree.getroot()
        size = root.find('size')
        w = int(size.find('width').text)
        h = int(size.find('height').text)

    for obj in root.findall('object'):
        print(xmlfile, obj.find('name').text, obj)
        if obj.find('name').text == 'fulll':
            obj.find('name').text = "full"

    print('-' * 66)
    tree.write(xmlfile)

    return 1


def Process(ProcessDir):
    xmlDirs = ProcessDir  # + 'Labels/'
    FileName1, FileNameWithPath1, FileDir1 = GetImgNameByEveryDir(xmlDirs, '.xml')

    for k in range(len(FileName1)):
        annfile = xmlDirs + FileName1[k][:-4] + '.xml'
        result = GetBoxInfo(annfile)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--ProcessDir', type=str, default='/home/build/smoke_ori/VOCdevkit/VOC2007/Annotations/')
    args = parser.parse_args()
    ProcessDir = args.ProcessDir
    Process(ProcessDir)
相关推荐
zhangfeng11336 分钟前
R geo 然后读取数据的时候 make.names(vnames, unique = TRUE): invalid multibyte string 9
开发语言·chrome·r语言·生物信息
VR最前沿9 分钟前
Xsens运动捕捉技术彻底改变了数字化运动方式,摆脱实验室局限,将生物力学引入现实
人工智能·科技
Sally璐璐10 分钟前
Go组合式继承:灵活替代方案
开发语言·后端·golang
zzzsde10 分钟前
【c++】类和对象(4)
开发语言·c++
码熔burning12 分钟前
从 new 到 GC:一个Java对象的内存分配之旅
java·开发语言·jvm
晨非辰13 分钟前
#C语言——刷题攻略:牛客编程入门训练(十二):攻克 循环控制(四)、循环输出图形(一),轻松拿捏!
c语言·开发语言·经验分享·笔记·其他·学习方法·visual studio
gou1234123414 分钟前
Go语言io.Copy深度解析:高效数据复制的终极指南
开发语言·golang·php
白玉cfc26 分钟前
【OC】单例模式
开发语言·ios·单例模式·objective-c
十六点五27 分钟前
Java NIO的底层原理
java·开发语言·python
猿究院-赵晨鹤27 分钟前
Java I/O 模型:BIO、NIO 和 AIO
java·开发语言