当前位置: 首页 > news >正文

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

介绍

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

代码实现:


import argparseimport xml.etree.ElementTree as ET
import osclasses = ['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, FileDirdef 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, 0else: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 1def 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.ProcessDirProcess(ProcessDir)

http://www.mrgr.cn/news/39746.html

相关文章:

  • 基于yolov8的100种蝴蝶智能识别系统python源码+pt模型+训练日志+精美GUI界面
  • 在PC端连接苹果手机(iPhone)时,即使已经开启了开发者模式(开发者权限),但仍然无法成功连接,是什么原因?
  • 特征工程——一门提高机器学习性能的艺术
  • 【LeetCode】动态规划—5. 最长回文子串(附完整Python/C++代码)
  • JDBC进阶
  • 【vs code(cursor) ssh连不上服务器(2)】但是 Terminal 可以连上,问题解决 ✅
  • C#名片识别接口集成方式、文字识别API
  • C++ STL(1)迭代器
  • 微信小程序 图片的上传
  • InnerClassLambdaMetafactory 内部类Lambda元工厂 源码解析
  • [Cocoa]_[初级]_[绘制文本如何设置断行方式]
  • 内核级理解套接字和全连接队列
  • 物联网智能设备:未来生活的变革者
  • centos发送邮件教程:从配置到发送全攻略!
  • 头戴式耳机性价比排名怎样?头戴式耳机性价比之王推荐!
  • 新考纲下的PMP考试有多难?
  • 谁在「力挺」激光雷达
  • 理解 Vue 的 setup 应用程序钩子
  • CDN与高防服务:区别与应用场景
  • YOLO11 | 一年更三版 版版不一样 | 关键改进及网络结构图【全网首发】