创建数据集
import os # 训练测试文件夹 def train_test_file(root,dir): file_txt = open(dir+'.txt','w') path = os.path.join(root,dir) for roots,directories,files in os.walk(path): if len(directories)!=0: dirs = directories else: now_dir = roots.split('\\') for file in files: path_1 = os.path.join(roots,file) print(path_1) file_txt.write(path_1+''+str(dirs.index(now_dir[-1]))+'\n') file_txt.close() root = r'.\食物分类\food_dataset' train_dir = 'train' test_dir = 'test' train_test_file(root,train_dir) train_test_file(foot,test_dir)
M54