很多时候需要将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