将XML数据集转换为YOLOv3格式数据集

很多时候需要将XML格式的标注数据集转换为YOLOv3的txt格式,重复性的工作为了节省时间,给代码存一下档。

YOLOv3的Github:https://github.com/AlexeyAB/darknet

目的是要生成每一张图片对应的txt(统一放在labels文件夹下),以及记录所有数据集的train.txt和val.txt。根据darknet自带的voc_label.py文件修改后的代码如下:

# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import os
from os import listdir
from os.path import join

classes = ["excavator"]#自己数据集有哪些类别写哪些类,按照顺序

def convert(size, box):
    dw = 1./(size[0])
    dh = 1./(size[1])
    x = (box[0] + box[1])/2.0 - 1
    y = (box[2] + box[3])/2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)

def convert_annotation(image_id):
    in_file = open('Annotations/%s.xml'%(image_id), encoding = 'utf-8')
    out_file = open('lab
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值