制作VOC2007数据集

1.首先要制作ImageSets/Main文件夹下的train.txt和val.txt文件,这两个txt文件存储的是图片数据的名称,注意图片名称不包含后缀。

import os
from os import listdir, getcwd
from os.path import join
import random
if __name__ == '__main__':
    source_folder = '/home/lee/Desktop/myfiles/yolov4-2/darknet/scripts/VOCdevkit/VOC2007/JPEGImages'
    dest = '/home/lee/Desktop/myfiles/yolov4-2/darknet/scripts/VOCdevkit/VOC2007/ImageSets/Main/train.txt'  # train.txt文件路径
    dest2 = '/home/lee/Desktop/myfiles/yolov4-2/darknet/scripts/VOCdevkit/VOC2007/ImageSets/Main/val.txt'  # val.txt文件路径
    file_list = os.listdir(source_folder)
    train_file = open(dest, 'a')
    val_file = open(dest2, 'a')
    split_rate = 0.1
    file_num = len(file_list)
    random.shuffle(file_list)
    val_name = random.sample(file_list, int(split_rate*file_num))
    for file_obj in file_list:
        file_path = os.path.join(source_folder, file_obj)
        file_name, file_extend = os.path.splitext(file_obj)

        if file_obj in val_name:
            val_file.write(file_name + '\n')
        else:
            train_file.write(file_name + '\n')
    train_file.close()
    val_file.close()

你可能感兴趣的:(制作VOC2007数据集)