python对目标检测数据集xml文件操作

博主在制作目标检测数据集时,需要将之前的标签进行替换,将person替换为rider,使用python批量处理xml格式文件如下:

import os, sys
import glob
from xml.etree import ElementTree as ET
#批量读取Annotations下的xml文件
#per=ET.parse(r'C:\Users\rockhuang\Desktop\Annotations\000003.xml')
xml_dir=r'C:\Users\rockhuang\Desktop\Annotations'
xml_list= glob.glob(xml_dir + '/*.xml')
for xml in xml_list:
    print(xml)
    per=ET.parse(xml)
    p=per.findall('/object')

    for oneper in p:  #找出person节点
        child=oneper.getchildren()[0] #找出person节点的子节点
        if child.text=='person':
            child.text='rider'
        

    per.write(xml)
    print(child.tag,':',child.text) 

修改之前的xml文件为:
python对目标检测数据集xml文件操作_第1张图片
修改之后:
python对目标检测数据集xml文件操作_第2张图片
需要将 xmin,ymin,xmax,ymax坐标和width,height统一缩小

# coding=utf-8
# 读xml文件中的一个rect
import xml.etree.ElementTree as ET
import os, sys
import glob


xml_dir=r'C:\Users\rockhuang\Desktop\rider_xml'
xml_list= glob.glob(xml_dir + '\*.xml')
for xml in xml_list:
    tree = ET.parse(xml)
    root = tree.getroot()

    for size in root.findall('size'):
        for width in size.iter('width'):
            width.text=str(round(int(width.text)/4))
           # print(width.text)
        for height in size.iter('height'):
            height.text=str(round(int(height.text)/4))
           # print(height.text)
    for ob in root.findall('object'):
        for bndbox in ob.iter('bndbox'):
            # for l in bndbox:
            #     print(l.text)
            for xmin in bndbox.iter('xmin'):
                xmin.text= str(round(int(xmin.text)/4))
            for ymin in bndbox.iter('ymin'):
                ymin.text =str(round(int(ymin.text)/4))
            for xmax in bndbox.iter('xmax'):
                xmax.text =str(round(int(xmax.text)/4)) 
            for ymax in bndbox.iter('ymax'):
                ymax.text = str(round(int(ymax.text)/4))
            #print(xmin.text,ymin.text,xmax.text,ymax.text)
    tree.write(xml)

你可能感兴趣的:(python)