学习Python:将xml转换为txt/csv

将用labelImg.exe标注后生成的xml文件转换为方便处理的txt/csv,具体信息根据实际情况提取。

import os
import csv
import glob

def XML2TXT(xmlPath: str):
    dataList = []
    with open(xmlPath, 'r') as fp:
        for p in fp:
            if '<filename>' in p:
                fileName = p.split('>')[1].split('<')[0]
            if '<object>' in p:
                d = [next(fp).split('>')[1].split('<')[0] for _ in range(9)]    # 类别
                strType = d[0]
                x1 = int(d[-4])     # 边界框
                y1 = int(d[-3])
                x2 = int(d[-2])
                y2 = int(d[-1])
                dataList.append([os.path.join('JPEGImages', fileName), x1, y1, x2, y2, strType])
    txtPath = xmlPath.replace(".xml", ".txt")   # or csv
    with open(txtPath, 'w', newline='') as fp:
        csv_writer = csv.writer(fp, dialect='excel')
        csv_writer.writerows(dataList)

path = glob.glob('*.xml')
for xmlFile in path:
    XML2TXT(xmlFile)

以下为labelimg生成的xml文件

<annotation verified="yes">
	<folder>8bit</folder>
	<filename>xxx.png</filename>
	<path>D:/xxx.png</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>2656</width>
		<height>5310</height>
		<depth>1</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>pos_screen_b5</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>128</xmin>
			<ymin>672</ymin>
			<xmax>179</xmax>
			<ymax>842</ymax>
		</bndbox>
	</object>
	<object>
		<name>pos_screen_b4</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>132</xmin>
			<ymin>3550</ymin>
			<xmax>180</xmax>
			<ymax>3735</ymax>
		</bndbox>
	</object>
	<object>
		<name>pos_screen_b1</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>2450</xmin>
			<ymin>4450</ymin>
			<xmax>2505</xmax>
			<ymax>4631</ymax>
		</bndbox>
	</object>
	<object>
		<name>pos_screen_b2</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>2447</xmin>
			<ymin>3438</ymin>
			<xmax>2507</xmax>
			<ymax>3615</ymax>
		</bndbox>
	</object>
	<object>
		<name>pos_screen_b3</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>2456</xmin>
			<ymin>1965</ymin>
			<xmax>2516</xmax>
			<ymax>2142</ymax>
		</bndbox>
	</object>
</annotation>

转换结果

JPEGImages\xxx.png,128,672,179,842,pos_screen_b5
JPEGImages\xxx.png,132,3550,180,3735,pos_screen_b4
JPEGImages\xxx.png,2450,4450,2505,4631,pos_screen_b1
JPEGImages\xxx.png,2447,3438,2507,3615,pos_screen_b2
JPEGImages\xxx.png,2456,1965,2516,2142,pos_screen_b3

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值