代码没报错,却得不到想要的效果
目标功能:将face文件夹里的0.jpg到23999.jpg放进文件夹cnn_train,将其他图片放进文件夹cnn_val。对每张图片标记属于哪一个类别,存放在dataset.csv中,分别在刚刚训练集和测试集执行标记任务。实际实现功能:创建了cnn_train\dataset.csv和cnn_val\dataset.csv,但是既没有把face里的图放到另两个文件夹里,新建出来的两个scv里也没有数据
代码:
import os
import pandas as pd
def data_label(path):
# 读取label文件
df_label = pd.read_csv('cnn_label.csv', header = None)
# 查看该文件夹下所有文件
files_dir = os.listdir(path)
# 用于存放图片名
path_list = []
# 用于存放图片对应的label
label_list = []
# 遍历该文件夹下的所有文件
for file_dir in files_dir:
# 如果某文件是图片,则将其文件名以及对应的label取出,分别放入path_list和label_list这两个列表中
if os.path.splitext(file_dir) == ".jpg":
path_list.append(file_dir)
index = int(os.path.splitext(file_dir))
label_list.append(df_label.iat)
# 将两个列表写进dataset.csv文件
path_s = pd.Series(path_list)
label_s = pd.Series(label_list)
df = pd.DataFrame()
df['path'] = path_s
df['label'] = label_s
df.to_csv(path+'\\dataset.csv', index=False, header=False)
def main():
# 指定文件夹路径
train_path = '/home/jiang/chenji/chenji/cnn_train'
val_path = '/home/jiang/chenji/chenji/cnn_val'
data_label(train_path)
data_label(val_path)
if __name__ == "__main__":
main()
import os
import csv
import shutil
def main():
files = os.listdir('.')
files.remove('cnn_train')
files.remove('cnn_val')
with open('result.csv', 'w', encoding='utf-8', newline='') as csvfile:
writer = csv.writer(csvfile)
for file in files:
filename, filesuffix = os.path.splitext(file)
if filename.isdigit():
if 0 <= int(filename) <= 2999:
writer.writerow()
shutil.move(file, r'cnn_train\\' + file)
else:
writer.writerow()
shutil.move(file, r'cnn_val\\' + file)
if __name__ == "__main__":
main()
页:
[1]