yolov5所需的格式如下图:
第一位是类别的索引,,,然后是归一化后的中心点x,y,宽高w,h。
转化代码如下:
GT_PATH路径下放所有的xml文件
# -*- coding: utf-8 -*-
"""
Time : 2022/5/14 17:18
Author : cong
"""
import sys
import os
import glob
import xml.etree.ElementTree as ET
names = ['hatch', 'cargo', 'aeroplane']
GT_PATH = 'datasets/coco128/labels/train2017/'
#print(GT_PATH)
os.chdir(GT_PATH)
xml_list = glob.glob('*.xml')
if not os.path.exists("backup"):
os.makedirs("backup")
for tmp_file in xml_list:
#print(tmp_file)
# 1. create new file (VOC format)
with open(tmp_file.replace(".xml", ".txt"), "a") as new_f:
root = ET.parse(tmp_file).getroot()
size = root.find('size')
for obj in root.findall('object'):
obj_name = obj.find('name').text
obj_index = names.index(obj_name)
bndbox = obj.find('bndbox')
image_w = int(size.find('width').text)
image_h = int(size.find('height').text)
x_min = int(bndbox.find('xmin').text)
x_max = int(bndbox.find('xmax').text)
y_min = int(bndbox.find('ymin').text)
y_max = int(bndbox.find('ymax').text)
x = ((x_min + x_max)/2)/image_w
y = ((y_min + y_max)/2)/image_h
w = (x_max - x_min) /image_w
h = (y_max - y_min) /image_h
new_f.write("%d %s %s %s %s\n" % (obj_index, x, y, w, h))
# 2. move old file (xml format) to backup
os.rename(tmp_file, os.path.join("backup", tmp_file))
print("Conversion completed!")