import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
#b=(xmin, xmax, ymin, ymax)
#size(w,h)
def convert(size, box):
#获取矩形框中心坐标
x_center = (box[0] + box[1]) / 2.0
y_center = (box[2] + box[3]) / 2.0
#yolo txt中,坐标归一化
x = round(x_center / size[0],6)
y = round(y_center / size[1],6)
w = round((box[1] - box[0]) / size[0],6)
h =round ((box[3] - box[2]) / size[1],6)
return (x, y, w, h)
def convert_annotation(xml_files_path, save_txt_files_path, classes):
#获取文件中的xml名称
#xml_files = os.listdir(xml_files_path)
xml_files = [file for file in os.listdir(xml_files_path) if file.endswith('.xml')]
#遍历
for xml_name in xml_files:
#xml路径
xml_file = os.path.join(xml_files_path, xml_name)
#保存路径
out_txt_path = os.path.join(save_txt_files_path, xml_name.split('.')[0] + '.txt')
out_txt_f = open(out_txt_path, 'w')
tree = ET.parse(xml_file)
root = tree.getroot()
#获取图片w,h
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
#遍历object,获取矩形框信息
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
#检查cls和difficult
if cls not in classes or int(difficult) == 1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text),
float(xmlbox.find('xmax').text),
float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
# b=(xmin, xmax, ymin, ymax)
#调用convert函数,xml格式:(xmin, xmax, ymin, ymax)----->.txt格式:(x,y,w,h)
bb = convert((w, h), b)
#检查bb的是否有异常值,大于1的值
for value in bb:
# 检查值是否大于1
if value > 1:
print(bb)
print(xml_name)
out_txt_f.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
if __name__ == "__main__":
classes1 = ['normal','rot','special-shaped','special-shaped_rot','stem']
xml_files1 = 'G:\工业相机拍照/黄芪片_1106_xml_txt_json'
save_txt_files1 = 'G:\工业相机拍照/11'
convert_annotation(xml_files1, save_txt_files1, classes1)
思路:(1)从xml size中读取图片尺寸---w,h 。
(2)遍历object(矩形框),获取name(类别),获取xmin,xmax,ymin,ymax。
(3)通过convert函数,将数据转为 txt的格式。
其中txt中数据格式为(cls, x,y,w,h), cls为类别, (x,y)为矩形框中心坐标, w,h分别为 矩形框的宽、高。 都是归一化后的值,都在0-1之间。
(4)保存数据到txt中。