caffe图像标签生成

参考: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("成功生成文件列表")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值