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)
相关推荐
2501_941805315 分钟前
边缘计算:引领智能化未来的新技术
人工智能
福尔摩斯张15 分钟前
二维数组详解:定义、初始化与实战
linux·开发语言·数据结构·c++·算法·排序算法
大佬,救命!!!22 分钟前
C++函数式策略模式代码练习
开发语言·c++·学习笔记·学习方法·策略模式·迭代加深·多文件编译
T***160730 分钟前
JavaScript打包
开发语言·javascript·ecmascript
qq_3363139331 分钟前
java基础-常用的API
java·开发语言
没有钱的钱仔36 分钟前
深度学习概念
人工智能·深度学习
百锦再36 分钟前
第21章 构建命令行工具
android·java·图像处理·python·计算机视觉·rust·django
星尘安全40 分钟前
研究人员发现严重 AI 漏洞,Meta、英伟达及微软推理框架面临风险
人工智能·microsoft·网络安全·程序员必看
共绩算力42 分钟前
【共绩 AI 小课堂】Class 5 Transformer架构深度解析:从《Attention Is All You Need》论文到现代大模型
人工智能·架构·transformer·共绩算力