Home >Backend Development >Python Tutorial >Python implements the method of generating txt files with labels for pictures in files
下面为大家分享一篇python实现对文件中图片生成带标签的txt文件方法,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧
在深度学习中经常需要生成带标签的图片名称列表,xxxlist.txt文件,下面写一个简单的python脚本生成该文件列表。
import os def generate(dir,label): files = os.listdir(dir) files.sort() print '****************' print 'input :',dir print 'start...' listText = open(dir+'\\'+'list.txt','w') for file in files: fileType = os.path.split(file) if fileType[1] == '.txt': continue name = file + ' ' + str(int(label)) +'\n' listText.write(name) listText.close() print 'down!' print '****************' if __name__ == '__main__': generate('C:\\Users\\Desktop\\image',1)
运行该脚本,会在image文件夹中生成一个list.txt文件,并且每张图片带有标签1.
相关推荐:
The above is the detailed content of Python implements the method of generating txt files with labels for pictures in files. For more information, please follow other related articles on the PHP Chinese website!