加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

用python制作训练集和测试集的图片名列表文本

发布时间:2020-12-20 12:56:43 所属栏目:Python 来源:网络整理
导读:1 # -*- coding: utf-8 -*- 2 from pathlib import Path # 从pathlib中导入Path 3 import os 4 import fileinput 5 import random 6 root_path= ‘ /home/tay/Videos/trash/垃圾分类项目/total/ ‘ 7 train = open( ‘ ./trash_train.txt ‘ , ‘ a ‘ ) 8 t
 1 # -*- coding: utf-8 -*-
 2 from pathlib import Path  #从pathlib中导入Path
 3 import os
 4 import fileinput
 5 import random
 6 root_path=/home/tay/Videos/trash/垃圾分类项目/total/
 7 train =  open(./trash_train.txt,a)
 8 test = open(./trash_test.txt,a)
 9 pwd = os.getcwd() +/# the val data path 训练集的路径
10 
11 
12 def gen_txt():
13     i =0
14     for file in os.listdir(root_path):
15         print(file is{}.format(str(file)))
16         for init in os.listdir(os.path.join(root_path,file)): #子文件夹
17             print(init is{}.format(str(init)))
18             i += 1
19             pathDir = os.listdir(os.path.join(root_path,file,init)) #
20             print(pathDir is,pathDir)
21             file_num = len(pathDir)
22             rate = 0.2
23             pick_num = int(file_num * rate)
24             sample = random.sample(pathDir,pick_num)  #随机选取20%的pathDir字符串
25             print(sample is,sample)
26             for pick_name in sample:
27                 test.write(root_path.split(total/)[-1] +file + / + init +/ + pick_name +   + str(i) + n)
28             # for name in pathDir: #文件夹中的图片名
29                 # print(‘name is{}‘.format(str(name)))
30                 # if test
31                 # total.write(root_path.split(‘total/‘)[-1] +file + ‘/‘ + init +‘/‘ + name + ‘ ‘ + str(i) + ‘n‘ )
32             same = [x for x in pathDir if x in sample] #列表中相同的内容
33             diff = [y for y in (sample + pathDir) if y not in same] #列表中不同的内容
34             print(different,diff)
35             print(same,same)
36             for train_name in diff:
37                 train.write(root_path.split(total/)[-1] +file + / + init +/ + train_name +   + str(i) + n)
38 gen_txt()

采用了random.sample函数来随机选取特定数量的文件名作为测试集,通过比较两个列表中不同的元素来获取训练集的文件名。

总体上就是在进行字符串操作。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读