将用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