将Pascal VOC格式转为YOLO格式
import os
import xml.etree.ElementTree as ET
# 归一化处理
def convert(size, box):
x_center = (box[0] + box[1]) / 2.0
y_center = (box[2] + box[3]) / 2.0
# 分别计算纵坐标和横坐标的中心点
x = x_center / size[0]
y = y_center / size[1]
w = (box[1] - box[0]) / size[0]
h = (box[3] - box[2]) / size[1]
# print(x, y, w, h)
return (x, y, w, h)
def convert_voc_to_yolo(voc_folder, output_folder,classes):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for xml_file in os.listdir(voc_folder):
if xml_file.endswith('.xml'):
tree = ET.parse(os.path.join(voc_folder, xml_file))
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
yolo_file = os.path.splitext(xml_file)[0] + '.txt'
yolo_path = os.path.join(output_folder, yolo_file)
with open(yolo_path, 'w') as f:
for obj in root.findall('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)
bbox = obj.find('bndbox')
b = (float(bbox.find('xmin').text), float(bbox.find('xmax').text), float(bbox.find('ymin').text),float(bbox.find('ymax').text))
# b=(xmin, xmax, ymin, ymax)
bb = convert((w,h),b)
yolo_line = f"{cls_id} {round(bb[0],6)} {round(bb[1],6)} {round(bb[2],6)} {round(bb[3],6)}\n"
f.write(yolo_line)
# 指定yolo类别
classes = ['scratches', 'missing components', 'incline', 'welding problems']
voc_folder = r'E:\label\newphoto\zhong3_label'
output_folder = r'E:\label\newphoto\zhong3_labb'
convert_voc_to_yolo(voc_folder, output_folder,classes)
参考代码:
https://blog.csdn.net/Silver__Wolf/article/details/132346930
https://blog.csdn.net/qq_64997449/article/details/139199166?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7ECtr-3-139199166-blog-121931849.235%5Ev43%5Epc_blog_bottom_relevance_base1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7ECtr-3-139199166-blog-121931849.235%5Ev43%5Epc_blog_bottom_relevance_base1&utm_relevant_index=4