python提取并修改VOC数据集中xml格式文件的bounding box坐标

下面一段代码是VOC数据集中的一个xml格式的文件,其包含两个person的目标信息:

<annotation>
	<folder>JPEGImagesfolder>
	<filename>00002.jpgfilename>
	<path>/media/mts/Document/Datasets/VOC2007/JPEGImages/00002.jpgpath>
	<source>
		<database>Unknowndatabase>
	source>
	<size>
		<width>1920width>
		<height>1080height>
		<depth>3depth>
	size>
	<segmented>0segmented>
	<object>
		<name>personname>
		<pose>Unspecifiedpose>
		<truncated>0truncated>
		<difficult>0difficult>
		<bndbox>
			<xmin>1176xmin>
			<ymin>59ymin>
			<xmax>1277xmax>
			<ymax>205ymax>
		bndbox>
	object>
    <object>
		<name>personname>
		<pose>Unspecifiedpose>
		<truncated>0truncated>
		<difficult>0difficult>
		<bndbox>
			<xmin>1118xmin>
			<ymin>827ymin>
			<xmax>1212xmax>
			<ymax>939ymax>
		bndbox>
    object>
annotation>

核心代码:

import xml.etree.ElementTree as ET
etree = ET.parse('1.xml')
root = etree.getroot()
for obj in root.iter('object'):
    xmin,ymin,xmax,ymax = (x.text for x in obj.find('bndbox'))
    print(xmin,ymin,xmax,ymax)
    for x in obj.find('bndbox'):
        x.text = '0'
    xmin,ymin,xmax,ymax = (x.text for x in obj.find('bndbox'))
    print(xmin,ymin,xmax,ymax)
etree.write('1-1.xml')

输出结果:

>>>python .\test.py
1176 59 1277 205
0 0 0 0
1118 827 1212 939
0 0 0 0

你可能感兴趣的:(python)