改進
# coding=utf-8 import os def makeFileLists(imgPath, fileName='list.txt', withLabel=False, ext=['jpg','bmp','png']): ''' makeFileList 函數用於包含多層目錄的文件列表創建 Params: imgPath :最上層的目錄路徑 fileName : 列表文件名稱 withLabel : 默認為`False`,如果需要為每個圖片路勁添加label, 則將該參數設置為`True`,圖片所在目錄的名稱即為 該圖片的label ext : 圖片格式 Usage: makeFileLists('imagesRootPath', 'imageList.txt', False) ''' # 判斷路徑是否存在 if not os.path.exists(imgPath): print imagesPath, 'IS NOT EXIST, PLEASE CHECK IT!' # 判斷路徑是否為目錄,如果是,則對目錄下的子目錄做遞歸操作 elif os.path.isdir(imgPath): subPath = os.listdir(imgPath) subPath = [os.path.join(imgPath,path) for path in subPath] for path in subPath: makeFileLists(path, fileName, withLabel) # 如果路徑不是目錄,則為圖片的相對路徑 else: # 只保存指定格式的圖片 if imgPath[-3:] in ext: # 以追加的方式打開文件 f = open(fileName,'a') # 如果需要添加label,則將圖片所在目錄的名稱作為label if withLabel: line = imgPath+' '+(imgPath.split('/'))[-2]+'\n' else: line = imgPath+'\n' # 寫入文件 f.writelines(line) f.close() if __name__ == "__main__": imagesPath = 'val' fileName = 'val.txt' makeFileLists(imagesPath, fileName, True)
使用遞歸方式生成包含子目錄的文件列表
#coding=utf-8 import os import shutil import random def getFileList(filePath='./images'): print filePath fileNames = os.listdir(filePath) paths = filePath.split('/') print paths if len(paths) == 4: fw = paths[2] f = open('train_'+fw+'.txt','a') fileList = os.listdir(filePath) # ./images/-45_45/x9/9_18_-1.29865.jpg # 根據路徑自己組合 saveRootPath = paths[2]+'/'+paths[3] fileListEnd = [saveRootPath+'/'+line+' '+paths[3][1]+'\n' for line in fileList] # 不要忘記加label f.writelines(fileListEnd) f.close() for fn in fileNames: subPath = filePath+'/'+fn if os.path.isdir(subPath): getFileList(subPath) getFileList()
#coding=utf-8 #對一批訓練數據,里面包含多個文件夾,每個文件夾下面存放的是相同類別的物體 # 根據這些文件夾生成列表、切分驗證、訓練集數據 import os import shutil import random #因為caffe中,不允許文件名中有空格,所有需要重命名去除空格 def stdrename(imgfiles): for l in imgfiles: x_list=l.split(' ') y = ''.join(x_list) if l!=y: print 'rename' os.rename(l,y) def GetFileList(FindPath,FlagStr=[]): FileList=[] FileNames=os.listdir(FindPath) if len(FileNames)>0: for fn in FileNames: if len(FlagStr)>0: if IsSubString(FlagStr,fn): fullfilename=os.path.join(FindPath,fn) FileList.append(fullfilename) else: fullfilename=os.path.join(FindPath,fn) FileList.append(fullfilename) if len(FileList)>0: FileList.sort() return FileList def spiltdata(path_root,valratio=0.15): classify_temp=os.listdir(path_root) classify_file=[] for c in classify_temp: classify_file.append(os.path.join(path_root,c)) for f in classify_file: imgfiles=GetFileList(f) stdrename(imgfiles)#caffe 文件名不允許有空格 for c in classify_temp: imgfiles=os.listdir(os.path.join(path_root,c)) nval=int(len(imgfiles)*valratio) print nval imgfvals=imgfiles[:nval] #驗證數據文件列表 for j in imgfvals: if os.path.exists(os.path.join(path_root+'/'+'val',c)) is False: os.makedirs(os.path.join(path_root+'/'+'val',c)) newname=os.path.join(path_root+'/'+'val',c)+'/'+j oldname=os.path.join(path_root,c)+'/'+j shutil.move(oldname,newname) #訓練數據文件列表 imgftrains=imgfiles[nval:] for j in imgftrains: if os.path.exists(os.path.join(path_root+'/'+'train',c)) is False: os.makedirs(os.path.join(path_root+'/'+'train',c)) newname=os.path.join(path_root+'/'+'train',c)+'/'+j oldname=os.path.join(path_root,c)+'/'+j shutil.move(oldname,newname) def writetrainlist(path_root): classify_temp=os.listdir(path_root)#['cropblack','cropbrown','cropwhite','cropyellow'] classify_file=[] for c in classify_temp: classify_file.append(os.path.join(path_root,c)) for f in classify_file: imgfiles=GetFileList(f) stdrename(imgfiles)#caffe 文件名不允許有空格 sorted(classify_file) strlist='' for i,f in enumerate(classify_file): imgfiles=GetFileList(f) for image in imgfiles: print image strlist+=image+' '+str(i)+'\n' txtlist=open(path_root+'.txt','w') txtlist.write(strlist) txtlist.close() '''spiltdata('../headangle/data')''' # writetrainlist('../faceshape/data/train') # writetrainlist('../faceshape/data/val') #spiltdata('../hair/data') #writetrainlist('../hair/data/train') #writetrainlist('../hair/data/val') writetrainlist('../data/train') writetrainlist('../data/val')