"""
By Fivethousand
输入图片二级目录根目录,随机生成该图片集的验证集(图片对+label)
"""
import os
import random
correct_label="1"
error_label="0"
def V_S_G(loadpath,savepath="./AutoSaved.txt",split=",",same_num=10,diff_num=50):
f = open(savepath, 'a+')
f.seek(0)
f.truncate()
subfile_list1 = os.listdir(loadpath)
dictionary={}
for file in subfile_list1:
subfile_list2=os.listdir(os.path.join(loadpath,file) )
dictionary[file]=subfile_list2
for file in subfile_list1:
count=2*same_num
if count>len(dictionary[file]):
count=len(dictionary[file])
if count%2 is not 0:
count=count-1
rdlist=random.sample(range(0,len(dictionary[file])),count)
for i in range(int(count/2)):
first=os.path.join(loadpath,file,dictionary[file][rdlist[2*i]])
second=os.path.join(loadpath, file, dictionary[file][rdlist[2 * i + 1]])
f.write(first+split+second+split+correct_label+"\n")
wrong_pair_num=diff_num
if(diff_num>len(subfile_list1)):
wrong_pair_num=len(subfile_list1)
for i in range(wrong_pair_num):
rdpair=random.sample(range(0,len(subfile_list1)),2)
first_file_name=subfile_list1[rdpair[0]]
first_rd=random.sample(range(0,len(dictionary[first_file_name])),1)[0]
first=os.path.join(loadpath,first_file_name,dictionary[first_file_name][first_rd])
second_file_name=subfile_list1[rdpair[1]]
second_rd=random.sample(range(0,len(dictionary[second_file_name])),1)[0]
second=os.path.join(loadpath,second_file_name,dictionary[second_file_name][second_rd])
f.write(first+split+second+split+error_label+"\n")
f.close()
V_S_G("F:\python_programs\\tasks_from_King\datasets\CASIA-WebFace-part")