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)
相关推荐
集和诚JHCTECH2 分钟前
BRAV-7120加持,让有毒有害气体无处遁形
大数据·人工智能·嵌入式硬件
iwhitney21 分钟前
【次方量化】3分钟搞懂什么是量化策略
python
高洁0142 分钟前
大模型部署资源不足?轻量化部署解决方案
python·深度学习·机器学习·数据挖掘·transformer
机械X人43 分钟前
Encoder-Decoder PLM
人工智能·深度学习
小锋java123443 分钟前
天天说的 Agent,到底是啥???
人工智能
阿里云大数据AI技术1 小时前
MaxFrame 视频帧智能分析:从视频到语义向量的端到端分布式处理
人工智能·python
大模型任我行1 小时前
谷歌:大模型规划最优性超越传统算法
人工智能·语言模型·自然语言处理·论文笔记
两万五千个小时1 小时前
为什么你的 Agent 读了文件,却好像什么都没读到?
人工智能·程序员·架构
淘矿人1 小时前
从0到1:用Claude启动你的第一个项目
开发语言·人工智能·git·python·github·php·pygame
cany10001 小时前
C++ -- 模板的声明和定义
开发语言·c++