参考:https://blog.csdn.net/u010417185/article/details/52119863(最详细)
代码:运行环境spyder,python3.6
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
#<pre class="python" name="code">#coding:utf-8
'''
Created on Jul 29, 2016
@author: sgg
'''
"<span style=""font-family:Arial;font-size:18px;"">"
"<span style=""font-size:18px;"">"
"<span style=""font-size:18px;"">"
import os
def IsSubString(SubStrList,Str):
flag=True
for substr in SubStrList:
if not(substr in Str):
flag=False
return flag
#扫面文件
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
train_txt=open('E:/caffe/MRI/MRI_yihang/train.txt','w')
#制作标签数据,如果是狗的,标签设置为0,如果是猫的标签为1
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/train_normal')#将数据集放在与.py文件相同目录下
for img in imgfile:
img=img.replace('_normal','') #将_文件夹名中的_normal删掉
img=img.replace('/','\\')
str1=img+' '+'0'+'\n' #用空格代替转义字符 \t
train_txt.writelines(str1)
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/train_tumor')
for img in imgfile:
img=img.replace('_tumor','')
img=img.replace('/','\\')
str2=img+' '+'1'+'\n'
train_txt.writelines(str2)
train_txt.close()
#测试集文件列表
test_txt=open('E:/caffe/MRI/MRI_yihang/test.txt','w')
#制作标签数据,如果是男的,标签设置为0,如果是女的标签为1
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/test_normal')#将数据集放在与.py文件相同目录下
for img in imgfile:
img=img.replace('_normal','')
img=img.replace('/','\\')
str3=img+' '+'0'+'\n'
test_txt.writelines(str3)
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/test_tumor')
for img in imgfile:
img=img.replace('_tumor','')
img=img.replace('/','\\')
str4=img+' '+'1'+'\n'
test_txt.writelines(str4)
test_txt.close()
print("成功生成文件列表")
结果:
自动创建txt文件
txt内容:前面没有截的是路径部分
但是这样会出问题。因为每个标签都是用整个文件路径(e:picture.jpg 0),后面转换数据时,程序会自动帮你加上路径,变成:e:picture.jpge:picture.jpg 0 ,重复了。所以要吧前面的路径删掉,只保留文件名和标签(注意文件名最前面要加\)
最终代码:
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
#<pre class="python" name="code">#coding:utf-8
'''
Created on Jul 29, 2016
@author: sgg
'''
"<span style=""font-family:Arial;font-size:18px;"">"
"<span style=""font-size:18px;"">"
"<span style=""font-size:18px;"">"
import os
def IsSubString(SubStrList,Str):
flag=True
for substr in SubStrList:
if not(substr in Str):
flag=False
return flag
#扫面文件
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
train_txt=open('E:/caffe/MRI/MRI_yihang/train.txt','w')
#制作标签数据,如果是狗的,标签设置为0,如果是猫的标签为1
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/train_normal')#将数据集放在与.py文件相同目录下
for img in imgfile:
# img=img.replace('_normal','') #将_文件夹名中的_normal删掉
# img=img.replace('/','\\')
str1=img
str1=str1.split('\\')[-1]
str1=str1+' '+'0'+'\n' #用空格代替转义字符 \t
str0='\\'
str0=str0+str1
train_txt.writelines(str0)
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/train_tumor')
for img in imgfile:
# img=img.replace('_tumor','')
# img=img.replace('/','\\')
str2=img
str2=str2.split('\\')[-1]
str2=str2+' '+'1'+'\n'
str0='\\'
str0=str0+str2
train_txt.writelines(str0)
train_txt.close()
#测试集文件列表
test_txt=open('E:/caffe/MRI/MRI_yihang/test.txt','w')
#制作标签数据,如果是男的,标签设置为0,如果是女的标签为1
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/test_normal')#将数据集放在与.py文件相同目录下
for img in imgfile:
# img=img.replace('_normal','')
# img=img.replace('/','\\')
str3=img
str3=str3.split('\\')[-1]
str3=str3+' '+'0'+'\n'
str0='\\'
str0=str0+str3
test_txt.writelines(str0)
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/test_tumor')
for img in imgfile:
# img=img.replace('_tumor','')
# img=img.replace('/','\\')
str4=img
str4=str4.split('\\')[-1]
str4=str4+' '+'1'+'\n'
str0='\\'
str0=str0+str4
test_txt.writelines(str0)
test_txt.close()
print("成功生成文件列表")