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)
相关推荐
beijixinghe1 小时前
第15节 指针作为函数参数的工程实战用法
开发语言·c++·c++基础·c++入门·几何引擎c++
Thomas.Sir1 小时前
第10课:PyTorch|学习率调度策略全面精讲【训练过程的“自适应巡航”】
人工智能·pytorch·学习
xsd202411181 小时前
无人机航拍生产库区目标检测与墙体裂缝识别:从航线规划到AI量化分析
人工智能·目标检测·无人机
SEO_juper1 小时前
2026年用Python分析网站访问日志:看清Googlebot和AI爬虫怎么爬你的站(附完整代码)
开发语言·前端·seo·独立站·谷歌优化
Thomas.Sir1 小时前
第4课:PyTorch|自动微分Autograd机制深度解析【深度学习的“发动机”】
人工智能·pytorch·深度学习
aneasystone本尊1 小时前
学习大模型推理的批处理:Continuous Batching
人工智能
OKkankan1 小时前
Python常用容器与导入语法详解(二)
数据结构·python
IT_陈寒1 小时前
Vue的响应式让我加班到凌晨,问题竟出在这个不起眼的地方
前端·人工智能·后端
和裕1 小时前
平口开槽箱 vs 飞机盒 vs 扣底盒:自动化、展示效果与成本核心区别
大数据·运维·网络·人工智能·算法·自动化