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

莱莱  论坛元老 | 2024-10-7 16:25:42 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1867|帖子 1867|积分 5601

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
介绍

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

  1. import argparse
  2. import xml.etree.ElementTree as ET
  3. import os
  4. classes = ['thin_smoke']
  5. def GetImgNameByEveryDir(file_dir, videoProperty):
  6.     FileNameWithPath, FileName, FileDir = [], [], []
  7.     for root, dirs, files in os.walk(file_dir):
  8.         for file in files:
  9.             if os.path.splitext(file)[1] in videoProperty:
  10.                 FileNameWithPath.append(os.path.join(root, file))  # 保存图片路径
  11.                 FileName.append(file)  # 保存图片名称
  12.                 FileDir.append(root[len(file_dir):])  # 保存图片所在文件夹
  13.     return FileName, FileNameWithPath, FileDir
  14. def GetBoxInfo(xmlfile):
  15.     try:
  16.         tree = ET.parse(xmlfile)
  17.         root = tree.getroot()
  18.         size = root.find('size')
  19.         w = int(size.find('width').text)
  20.         h = int(size.find('height').text)
  21.     except:
  22.         return False, 0
  23.     else:
  24.         tree = ET.parse(xmlfile)
  25.         root = tree.getroot()
  26.         size = root.find('size')
  27.         w = int(size.find('width').text)
  28.         h = int(size.find('height').text)
  29.     for obj in root.findall('object'):
  30.         print(xmlfile, obj.find('name').text, obj)
  31.         if obj.find('name').text == 'fulll':
  32.             obj.find('name').text = "full"
  33.     print('-' * 66)
  34.     tree.write(xmlfile)
  35.     return 1
  36. def Process(ProcessDir):
  37.     xmlDirs = ProcessDir  # + 'Labels/'
  38.     FileName1, FileNameWithPath1, FileDir1 = GetImgNameByEveryDir(xmlDirs, '.xml')
  39.     for k in range(len(FileName1)):
  40.         annfile = xmlDirs + FileName1[k][:-4] + '.xml'
  41.         result = GetBoxInfo(annfile)
  42. if __name__ == '__main__':
  43.     parser = argparse.ArgumentParser()
  44.     parser.add_argument('--ProcessDir', type=str, default='/home/build/smoke_ori/VOCdevkit/VOC2007/Annotations/')
  45.     args = parser.parse_args()
  46.     ProcessDir = args.ProcessDir
  47.     Process(ProcessDir)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莱莱

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表