import os
import random
#将类别随机2-8分
def split(full_list, shuffle=True, ratio=0.2):
n_total = len(full_list)
offset = int(n_total * ratio) #计算一列数据的分割位置
if n_total == 0 or offset<1:
return [], full_list
if offset % 2 == 1:
offset += 1
if shuffle: #将原列表顺序打乱,按照分割点分成两个列表
random.shuffle(full_list)
sublist_1 = full_list[:offset]
sublist_2 = full_list[offset:]
return sublist_1, sublist_2
x = 0
all_list = []
for root, dirs, files in os.walk("./img/"):
#for d in dirs:
#print(d) #打印子资料夹的个数
for file in files:
x = x+1
#每一个文件的路径
print(root)
if int(root[7]) != 0:
xml_path = root[6:] +'/' + file + ' ' + root[7:9] #标注中文件夹名字第三位是类别信息,这里具体第几位数字根据实际情况变化;第二个' ' 中间是空格,满足分类网络标注要求
all_list.append(xml_path)
#all_list.append('\n') #换行
else:
xml_path = root[6:] +'/' + file + ' ' + root[8] #标注中文件夹名字第三位是类别信息,这里具体第几位数字根据实际情况变化;第二个' ' 中间是空格,满足分类网络标注要求
all_list.append(xml_path)
#all_list.append('\n')
list_1 , list_2 = split(all_list)
#追加的方式
f_train = open('train.txt','w')
f_val = open('val.txt', 'w')
f_train.writelines([line+'\n' for line in list_2]) #换行
f_val.writelines([line+'\n' for line in list_1])
f_train.close()
f_val.close() #完成写入
生成好的train.txt中内容如下
001dogs/dog226.jpg 1
001dogs/dog392.jpg 1
000cats/cat953.jpg 0
001dogs/dog501.jpg 1
000cats/cat630.jpg 0
000cats/cat117.jpg 0
001dogs/dog606.jpg 1
000cats/cat251.jpg 0
001dogs/dog697.jpg 1
000cats/cat594.jpg 0
000cats/cat365.jpg 0
000cats/cat722.jpg 0
000cats/cat31.jpg 0
000cats/cat213.jpg 0
000cats/cat979.jpg 0
000cats/cat390.jpg 0
001dogs/dog667.jpg 1
000cats/cat353.jpg 0
001dogs/dog922.jpg 1