import os
import shutil
import random
import time
# root_dir = "/data/jjg/codes/datasets/M3FD_Detection/"
# imgs_dir = os.path.join(root_dir, "images")
# labs_dir = os.path.join(root_dir, "labels")
imgs_dir = "/data/jjg/codes/yolov5/datasets/OSU_TPD/images/train/"
labs_dir = "/data/jjg/codes/yolov5/datasets/OSU_TPD/labels/train/"
img_train = "/data/jjg/codes/datasets/zzbs-hw/images/train"
img_val = "/data/jjg/codes/datasets/zzbs-hw/images/val"
lab_train = "/data/jjg/codes/datasets/zzbs-hw/labels/train"
lab_val = "/data/jjg/codes/datasets/zzbs-hw/labels/val"
imgs_list = os.listdir(imgs_dir)
labs_list = os.listdir(labs_dir)
imgs_list.sort()
labs_list.sort()
lens = len(imgs_list)
idx_list = [i for i in range(lens)]
random.shuffle(idx_list) # 将列表进行了打乱
for i, idx in enumerate(idx_list):
img_name = imgs_list[idx]
lab_name = labs_list[idx]
img_path = os.path.join(imgs_dir, img_name)
lab_path = os.path.join(labs_dir, lab_name)
print(img_path)
print(lab_path)
if i < lens * 0.75: # 作为训练集
save_name = str(round(time.time() * 1000)) + "-osu"
img_train_path = os.path.join(img_train, save_name + ".bmp")
lab_train_path = os.path.join(lab_train, save_name + ".txt")
shutil.copyfile(img_path, img_train_path)
shutil.copyfile(lab_path, lab_train_path)
else:
save_name = str(round(time.time() * 1000)) + "-osu"
img_val_path = os.path.join(img_val, save_name + ".bmp")
lab_val_path = os.path.join(lab_val, save_name + ".txt")
shutil.copyfile(img_path, img_val_path)
shutil.copyfile(lab_path, lab_val_path)
Python 从原训练集拆分出训练和测试集
最新推荐文章于 2024-03-07 16:53:24 发布