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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import os import shutil from sklearn.model_selection import train_test_split
data_path = 'leapGestRecog'
train_ratio = 0.8 test_ratio = 0.2
train_data_path = 'dataset/train' test_data_path = 'dataset/test'
os.makedirs(train_data_path, exist_ok=True) os.makedirs(test_data_path, exist_ok=True)
def split_data(class_dir, dest_train_dir, dest_test_dir): files = [f for f in os.listdir(class_dir) if f.endswith('.png')] labels = [f.replace('.png', '.txt') for f in files] train_files, test_files = train_test_split(files, test_size=test_ratio, random_state=42) for file in train_files: src_img = os.path.join(class_dir, file) src_label = os.path.join(class_dir, file.replace('.png', '.txt')) dst_img = os.path.join(dest_train_dir, file) dst_label = os.path.join(dest_train_dir, file.replace('.png', '.txt')) shutil.copy(src_img, dst_img) shutil.copy(src_label, dst_label) for file in test_files: src_img = os.path.join(class_dir, file) src_label = os.path.join(class_dir, file.replace('.png', '.txt')) dst_img = os.path.join(dest_test_dir, file) dst_label = os.path.join(dest_test_dir, file.replace('.png', '.txt')) shutil.copy(src_img, dst_img) shutil.copy(src_label, dst_label)
for group in os.listdir(data_path): group_path = os.path.join(data_path, group) if not os.path.isdir(group_path): continue
for gesture in os.listdir(group_path): gesture_path = os.path.join(group_path, gesture) if not os.path.isdir(gesture_path): continue
dest_train_dir = os.path.join(train_data_path, group, gesture) dest_test_dir = os.path.join(test_data_path, group, gesture) os.makedirs(dest_train_dir, exist_ok=True) os.makedirs(dest_test_dir, exist_ok=True)
split_data(gesture_path, dest_train_dir, dest_test_dir)
print("数据集划分完成!")
|