123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import os
- import random
- import shutil
- from sklearn.model_selection import train_test_split
- data_dir = 'D:\python\PycharmProjects\data_20250223\lcnn新T型板十字板增强后(复件)'
- output_dir = 'D:\python\PycharmProjects\data_20250223\lcnn_20250223'
- images_train_dir = os.path.join(output_dir, 'images', 'train')
- images_val_dir = os.path.join(output_dir, 'images', 'val')
- labels_train_dir = os.path.join(output_dir, 'labels', 'train')
- labels_val_dir = os.path.join(output_dir, 'labels', 'val')
- os.makedirs(images_train_dir, exist_ok=True)
- os.makedirs(images_val_dir, exist_ok=True)
- os.makedirs(labels_train_dir, exist_ok=True)
- os.makedirs(labels_val_dir, exist_ok=True)
- image_files = [f for f in os.listdir(data_dir) if f.endswith('.jpg')]
- json_files = {f.replace('.json', ''): f for f in os.listdir(data_dir) if f.endswith('.json')}
- image_names = [os.path.splitext(f)[0] for f in image_files]
- train_names, val_names = train_test_split(image_names, test_size=0.1, random_state=42)
- for name in train_names:
- image_file = name + '.jpg'
- json_file = json_files[name]
-
- shutil.copy(os.path.join(data_dir, image_file), os.path.join(images_train_dir, image_file))
-
- shutil.copy(os.path.join(data_dir, json_file), os.path.join(labels_train_dir, json_file))
- for name in val_names:
- image_file = name + '.jpg'
- json_file = json_files[name]
-
- shutil.copy(os.path.join(data_dir, image_file), os.path.join(images_val_dir, image_file))
-
- shutil.copy(os.path.join(data_dir, json_file), os.path.join(labels_val_dir, json_file))
- if __name__ == "__main__":
-
- os.makedirs(images_train_dir, exist_ok=True)
- os.makedirs(images_val_dir, exist_ok=True)
- os.makedirs(labels_train_dir, exist_ok=True)
- os.makedirs(labels_val_dir, exist_ok=True)
-
- train_names, val_names = train_test_split(image_names, test_size=0.1, random_state=42)
- for name in train_names:
- image_file = name + '.jpg'
- json_file = json_files[name]
-
- shutil.copy(os.path.join(data_dir, image_file), os.path.join(images_train_dir, image_file))
-
- shutil.copy(os.path.join(data_dir, json_file), os.path.join(labels_train_dir, json_file))
- for name in val_names:
- image_file = name + '.jpg'
- json_file = json_files[name]
-
- shutil.copy(os.path.join(data_dir, image_file), os.path.join(images_val_dir, image_file))
-
- shutil.copy(os.path.join(data_dir, json_file), os.path.join(labels_val_dir, json_file))
|