python实现对文件中图片生成带标签的txt文件方法

yipeiwu_com6年前Python基础

在深度学习中经常需要生成带标签的图片名称列表,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.

以上这篇python实现对文件中图片生成带标签的txt文件方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python+selenium实现163邮箱自动登陆的方法

python+selenium实现163邮箱自动登陆的方法

本文介绍了 让我们先来预览一下代码运行效果吧: 首先分析163邮箱登陆页面的网页结构(按F12或单击鼠标右键选择审查元素) 1、定位到登陆框(注意登录框是一个iframe,如果不定位...

基于MSELoss()与CrossEntropyLoss()的区别详解

基于MSELoss()与CrossEntropyLoss()的区别详解

基于pytorch来讲 MSELoss()多用于回归问题,也可以用于one_hotted编码形式, CrossEntropyLoss()名字为交叉熵损失函数,不用于one_hotted编...

python调用xlsxwriter创建xlsx的方法

详细的官方文档可见:http://xlsxwriter.readthedocs.io/ 通过pip安装xlsxwriter pip install xlsxwriter 下面进行基...

pandas DataFrame 警告(SettingWithCopyWarning)的解决

刚接触python不久,编程也是三脚猫,所以对常用的这几个工具还没有一个好的使用习惯,毕竟程序语言是头顺毛驴。所以最近在工作中使用的时候在使用pandas的DataFrame时遇到了以下...

pandas删除行删除列增加行增加列的实现

创建df: >>> df = pd.DataFrame(np.arange(16).reshape(4, 4), columns=list('ABCD'), ind...