代码
import xml.etree.ElementTree as ET
import os
def xml2txt(classes, xml_path, txt_path):
file = open(xml_path)
xml_text = file.read()
root = ET.fromstring(xml_text)
file.close()
width = float(root.find('size').find('width').text)
height = float(root.find('size').find('height').text)
with open(txt_path, 'w') as file:
pass
for obj in root.iter('object'):
name = obj.find('name').text
if name not in classes:
print(name)
continue
cls_id = classes.index(name)
xmlbox = obj.find('bndbox')
xmin = float(xmlbox.find('xmin').text)
xmax = float(xmlbox.find('xmax').text)
ymin = float(xmlbox.find('ymin').text)
ymax = float(xmlbox.find('ymax').text)
x = (xmin + xmax) / 2.0
y = (ymin + ymax) / 2.0
w = xmax - xmin
h = ymax - ymin
x /= width
w /= width
y /= height
h /= height
with open(txt_path, 'a') as file:
file.write(str(cls_id) + ' ' + str(x) + ' ' + str(y) + ' ' + str(w) + ' ' + str(h) + '\n')
if __name__ == '__main__':
classes = ['smoke']
inDir_path = 'Annotations'
outDir_path = 'labels'
for file_name in os.listdir(inDir_path):
xml_path = inDir_path + '/' + file_name
txt_path = outDir_path + '/' + file_name.replace('xml', 'txt')
xml2txt(classes, xml_path, txt_path)
使用说明
我的目录如下:Annotations为原xml标注的文件夹,labels为新建的用于存放输出的空文件夹。
xml2txt函数定义了一个标注从xml格式到txt格式的转换,可根据需要在主函数自定义输入输出路径,然后调用xml2txt。