栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python对xml的指定标签进行重命名

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python对xml的指定标签进行重命名

#!/usr/bin/env python
#-*- coding: utf-8 -*-
# 把voc xml格式转换为txt格式. 自己部门标注的人脸数据,需要转换
# 目录结构如下
# subDir
#  --annotations
#  --Images

import os
import sys
import time
import random
import argparse
import shutil
import argparse
from xml.dom import minidom 

from multiprocessing.dummy import Pool as ThreadPool
from lxml.etree import Element, SubElement, tostring
from xml.dom.minidom import parseString
import pprint
import xml.etree.ElementTree as ET
import os

classes   = ['y']
        
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 == 'y':
            obj.find('name').text = "yw_nc"

    print('-'*66)
    root.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='./annotation/')
    args = parser.parse_args()
    ProcessDir      = args.ProcessDir
    Process(ProcessDir)

此代码功能为将annotation文件夹中的xml中标签为y的标签替换为yw_nc。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/923593.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号