Python implements the method of generating txt files with labels for pictures in files

不言
Release: 2018-04-27 11:14:28
Original
2953 people have browsed it

下面为大家分享一篇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)
Copy after login

运行该脚本,会在image文件夹中生成一个list.txt文件,并且每张图片带有标签1.

相关推荐:

Python实现删除时保留特定文件夹和文件的示例

Python实现去除列表中重复元素的方法

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template