import os
import cv2
import sys
import random
import xml.etree.ElementTree as ET
def mkdir(path):
if not os.path.exists(path):
os.makedirs(path)
def divide(filename, trainval_percent, train_percent):
xmlfilepath = '%s/Annotations' % filename
txtsavepath = '%s/ImageSets' % filename
mkdir(txtsavepath)
try:
total_xml = os.listdir(xmlfilepath)
except:
mkdir(xmlfilepath)
print('--------Annotations is empty--------')
sys.exit()
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)
ftrainval = open('%s/ImageSets/trainval.txt' % filename, 'w')
ftest = open('%s/ImageSets/test.txt' % filename, 'w')
ftrain = open('%s/ImageSets/train.txt' % filename, 'w')
fval = open('%s/ImageSets/val.txt' % filename, 'w')
for i in list:
name = total_xml[i][:-4] + '\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftrain.write(name)
else:
fval.write(name)
else:
ftest.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
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, filename, classes):
in_file = open((filename + '/Annotations/%s.xml') % (image_id), encoding='utf-8')
out_file = open((filename + '/labels/%s.txt') % (image_id), 'w', encoding='utf-8')
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
if size is not None:
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
cls = obj.find('name').text
if cls not in classes:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text),
float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
def make_txt(filename, sets, classes):
for image_set in sets:
mkdir(filename + '/labels/')
image_ids = open('%s/ImageSets/%s.txt' % (filename, image_set)).read().strip().split()
list_file = open('%s/%s.txt' % (filename, image_set), 'w')
for image_id in image_ids:
list_file.write('%s/images/%s.jpg\n' % (filename, image_id))
convert_annotation(image_id, filename, classes)
list_file.close()
def read_pic(filename):
image_ids = open('%s/ImageSets/test.txt' % filename).read().strip().split()
list_file = open('%s/test.txt' % filename).read().strip().split()
for image in range(len(list_file)):
img = cv2.imread(list_file[image])
cv2.imwrite('%s/inference/%s.jpg' % (filename, image_ids[image]), img)
if __name__ == '__main__':
# (trainval = train + val) + test = 1
filename = '../yolov5/data'
trainval_percent = 0.8
train_percent = 0.75
sets = ['train', 'val', 'test']
classes = ["Car", "Bus"]
# 比例划分
divide(filename, trainval_percent, train_percent)
# 导出对应图片路径的txt
make_txt(filename, sets, classes)
# 根据txt提取测试集
read_pic(filename)
YOLO数据集划分脚本整合
于 2023-11-28 20:13:46 首次发布