voc格式转为yolo格式

将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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值