import os, random, shutil path="images_1" file_name_list = os.listdir(path) print("图片总数目",len(file_name_list ),file_name_list) zu_name=[] for i in file_name_list: zu_name.append(i[:3]) zu_name=sorted(list(set(zu_name))) print(len(
path="images_1"
file_name_list = os.listdir(path)
print("图片总数目",len(file_name_list ),file_name_list)
zu_name=[]
for i in file_name_list:
zu_name.append(i[:3])
zu_name=sorted(list(set(zu_name)))
print(len(zu_name),zu_name)
train=[]
test=[]
for name in zu_name:
temp=[]
for i in file_name_list:
if i[:3]==name:
temp.append(i)
temp=sorted(temp, key=lambda item: int(item[4:6]))
train.extend(temp[:-1])
test.append(temp[-1])
print("train",len(train),train)
print("test",len(test),test)
if os.path.exists("train"):pass
else:os.mkdir("train")
if os.path.exists("test"):pass
else:os.mkdir("test")
for name in train:
shutil.copy("images_1/"+ name, "train/"+ name)
for name in test:
shutil.copy("images_1/" + name, "test/" + name)