|
import os |
|
import shutil |
|
import random |
|
|
|
|
|
image_dir = 'images' |
|
image_files = [f for f in os.listdir(image_dir) if f.endswith(('.png', '.jpg', '.jpeg'))] |
|
|
|
|
|
random.shuffle(image_files) |
|
|
|
|
|
split_point = len(image_files) // 2 |
|
|
|
|
|
os.makedirs('dataset_1', exist_ok=True) |
|
os.makedirs('dataset_2', exist_ok=True) |
|
|
|
|
|
for i, img_file in enumerate(image_files): |
|
|
|
txt_file = os.path.splitext(img_file)[0] + '.txt' |
|
|
|
|
|
target_dir = 'dataset_1' if i < split_point else 'dataset_2' |
|
|
|
|
|
shutil.move(os.path.join(image_dir, img_file), os.path.join(target_dir, img_file)) |
|
if os.path.exists(os.path.join(image_dir, txt_file)): |
|
shutil.move(os.path.join(image_dir, txt_file), os.path.join(target_dir, txt_file)) |
|
|
|
print(f'已将{len(image_files)}个图文对平均分配到dataset_1和dataset_2文件夹中') |
|
|
|
|
|
|