voc数据集批量修改标签名字

在此附上代码:

import io
import sys
import os
import xml.etree.ElementTree as ET
#sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
##anno_path修改为你存放xml文件的位置,最后末尾需要加上/
#old_annotation是修要修改的标签名,new_annotation是修改后的标签名字
anno_path = '/home/jun/VOC2007/Annotations/'
old_annotation = '2'
new_annotation = 'word'
del_annotations = ['occlusion']
#replace = True使用替换功能,False使用删除功能
REPLACE = True 
def _main():
    filelist = os.listdir(anno_path)
    i = 0
    if REPLACE == True:
        for file in filelist:
            n_ = _Replace_Annotation(file)
            if n_ > 0:
                i += 1
    else:
        for file in filelist:
            n_ = _Del_Annotation(file)
            if n_ >0:
                i += 1
    print('the number of xmlfile is :' + str(i))
 
def _Replace_Annotation(filepath):
    if os.path.exists(anno_path + filepath) == False:
        print(filepath+' :not found')
    #建立xml树状结构
    i = 0
    while Replace_(filepath) == False:
        i += 1
 
    return i
 
def Replace_(filepath):
    if os.path.exists(anno_path + filepath) == False:
        print(filepath+' :not found')
    #建立xml树状结构
    tree = ET.parse(anno_path + filepath)
    #遍历xml文件 查找'name'
    for annoobject in tree.iter():
        if 'object' in annoobject.tag:
            for element in list(annoobject):
                if 'name' in element.tag:
                    #替换标签
                    if element.text == old_annotation:
                        element.text = new_annotation
                        print(filepath)
                        #重新写入xml,使修改生效
                        tree.write(anno_path+filepath, encoding="utf-8", xml_declaration=True)
                        return False
    return True
 
def _Del_Annotation(filepath):
    if os.path.exists(anno_path + filepath) == False:
        print(filepath+' :not found')
    #建立xml树状结构
    i = 0
    while Delete_(filepath) == False:
        i += 1
    return i
    
def Delete_(filepath):
    if os.path.exists(anno_path + filepath) == False:
        print(filepath+' :not found')
    #建立xml树状结构
    tree = ET.parse(anno_path + filepath)
    #遍历xml文件 查找'name'
    root = tree.getroot()
    for annoobject in root.iter():
        if 'object' in annoobject.tag:
            for element in list(annoobject):
                if 'name' in element.tag:
                    #删除标签
                    for anno in del_annotations:
                        if element.text == anno:
                            #从根节点下删除第一个子节点
                            root.remove(annoobject)
                            print(filepath)
                            #重新写入xml,使修改生效
                            tree = ET.ElementTree(root)
                            tree.write(anno_path+filepath, encoding="utf-8", xml_declaration=True)
                            return False
    return True
 
if __name__ == '__main__':
    _main()
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小俊俊的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值