1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import os import shutil from sklearn.model_selection import train_test_split
images_folder = r'C:\Users\XY\Desktop\zhuizhong\data2\workshop01\img' labels_folder = r'C:\Users\XY\Desktop\zhuizhong\data2\workshop01\txt'
dataset_folder = r'C:\Users\XY\Desktop\zhuizhong\data2\1'
val_ratio = 0.15
os.makedirs(os.path.join(dataset_folder, 'images', 'train'), exist_ok=True) os.makedirs(os.path.join(dataset_folder, 'images', 'val'), exist_ok=True) os.makedirs(os.path.join(dataset_folder, 'labels', 'train'), exist_ok=True) os.makedirs(os.path.join(dataset_folder, 'labels', 'val'), exist_ok=True)
image_files = [f for f in os.listdir(images_folder) if f.endswith('.jpg')]
train_images, val_images = train_test_split(image_files, test_size=val_ratio)
for image_file in train_images: shutil.copy(os.path.join(images_folder, image_file), os.path.join(dataset_folder, 'images', 'train', image_file)) label_file = os.path.splitext(image_file)[0] + '.txt' shutil.copy(os.path.join(labels_folder, label_file), os.path.join(dataset_folder, 'labels', 'train', label_file))
for image_file in val_images: shutil.copy(os.path.join(images_folder, image_file), os.path.join(dataset_folder, 'images', 'val', image_file)) label_file = os.path.splitext(image_file)[0] + '.txt' shutil.copy(os.path.join(labels_folder, label_file), os.path.join(dataset_folder, 'labels', 'val', label_file))
|