本程序的功能是转换xml的数据为一行一行的数据。
xml示意:
<annotation>
<folder>东folder>
<filename>0.jpgfilename>
<path>D:\tttt\任务\东\0.jpgpath>
<source>
<database>Unknowndatabase>
source>
<size>
<width>720width>
<height>1280height>
<depth>3depth>
size>
<segmented>0segmented>
<object>
<name>carname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>122xmin>
<ymin>264ymin>
<xmax>335xmax>
<ymax>532ymax>
bndbox>
object>
<object>
<name>carname>
<pose>Unspecifiedpose>
<truncated>1truncated>
<difficult>0difficult>
<bndbox>
<xmin>547xmin>
<ymin>599ymin>
<xmax>720xmax>
<ymax>910ymax>
bndbox>
object>
<object>
<name>carname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>453xmin>
<ymin>317ymin>
<xmax>586xmax>
<ymax>501ymax>
bndbox>
object>
<object>
<name>carname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>443xmin>
<ymin>97ymin>
<xmax>592xmax>
<ymax>281ymax>
bndbox>
object>
annotation>
转化结果示意:
imgs/0.jpg 122,264,335,532,6 547,599,720,910,6 453,317,586,501,6 443,97,592,281,6
imgs/100.jpg 165,295,314,532,6 433,160,649,540,6
imgs/1000.jpg 135,278,345,556,6 355,666,653,1066,6 575,242,694,375,6 575,148,685,238,6
代码为:
import xml.etree.ElementTree as ET
import os
classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
inAddr = 'imgs/'
anno_in_Addr = 'annotations/%s'
anno_out_Addr = 'train.txt'
jpg_addrs_generator = os.walk(inAddr)
_,_,jpg_addrs = next(jpg_addrs_generator)
outfile = open(anno_out_Addr, 'w')
for jpg_addr in jpg_addrs:
xml_addr = jpg_addr.replace('.jpg','.xml')
xml_addr = anno_in_Addr%(xml_addr)
print(jpg_addr,xml_addr)
writeStr = inAddr+jpg_addr
tree=ET.parse(xml_addr)
root = tree.getroot()
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult)==1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymax').text))
boxStr = " %d,%d,%d,%d,%d"%(b[0],b[1],b[2],b[3],cls_id)
writeStr += boxStr
#print(writeStr)
writeStr = writeStr + '\n'
outfile.write(writeStr)
outfile.close()