0713日上传数据处理代码,部分代码未写完
parent
509e860c5c
commit
0b22bc6742
@ -0,0 +1,44 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
def save_file_paths_with_labels(folder_path, train_ratio, valid_ratio, test_ratio, label):
|
||||||
|
# 获取文件夹下所有文件的路径
|
||||||
|
file_paths = []
|
||||||
|
for root, dirs, files in os.walk(folder_path):
|
||||||
|
for file in files:
|
||||||
|
file_path = os.path.join(root, file)
|
||||||
|
file_paths.append(file_path)
|
||||||
|
|
||||||
|
# 计算每个集合的文件数目
|
||||||
|
total_files = len(file_paths)
|
||||||
|
train_files = int(total_files * train_ratio)
|
||||||
|
valid_files = int(total_files * valid_ratio)
|
||||||
|
test_files = int(total_files * test_ratio)
|
||||||
|
|
||||||
|
# 根据比例拆分文件路径列表
|
||||||
|
train_paths = file_paths[:train_files]
|
||||||
|
valid_paths = file_paths[train_files:train_files + valid_files]
|
||||||
|
test_paths = file_paths[train_files + valid_files:train_files + valid_files + test_files]
|
||||||
|
|
||||||
|
# 为文件路径添加标签
|
||||||
|
train_paths_labeled = [path + ' ' + str(label) for path in train_paths]
|
||||||
|
valid_paths_labeled = [path + ' ' + str(label) for path in valid_paths]
|
||||||
|
test_paths_labeled = [path + ' ' + str(label) for path in test_paths]
|
||||||
|
|
||||||
|
# 将文件路径保存到txt文件
|
||||||
|
save_to_txt(train_paths_labeled, 'train.txt')
|
||||||
|
save_to_txt(valid_paths_labeled, 'valid.txt')
|
||||||
|
save_to_txt(test_paths_labeled, 'test.txt')
|
||||||
|
|
||||||
|
def save_to_txt(file_paths, filename):
|
||||||
|
with open(filename, 'w') as file:
|
||||||
|
for file_path in file_paths:
|
||||||
|
file.write(file_path + '\n')
|
||||||
|
|
||||||
|
# 示例用法
|
||||||
|
folder_path = 'dataset/video_seg_re_hand' # 替换为实际的文件夹路径
|
||||||
|
train_ratio = 0.8 # 训练集比例
|
||||||
|
valid_ratio = 0.2 # 验证集比例
|
||||||
|
test_ratio = 0.0 # 测试集比例
|
||||||
|
label = '1' # 替换为实际的标签
|
||||||
|
|
||||||
|
save_file_paths_with_labels(folder_path, train_ratio, valid_ratio, test_ratio, label)
|
Loading…
Reference in New Issue