• Python 读取文件夹的文件


     1 def get_files(filename):
     2     class_train = []
     3     label_train = []
     4     for train_class in os.listdir(filename):
     5          for pic in os.listdir(filename+train_class):
     6              class_train.append(filename+train_class+'/'+pic)
     7              label_train.append(train_class)
     8     temp = np.array([class_train,label_train])
     9     temp = temp.transpose()
    10     #shuffle the samples
    11     np.random.shuffle(temp)
    12     #after transpose, images is in dimension 0 and label in dimension 1
    13     image_list = list(temp[:,0])
    14     label_list = list(temp[:,1])
    15     label_list = [int(i) for i in label_list]
    16     #print(label_list)
    17     return image_list,label_list
    18 TrainData,labels=get_files(path)
     1 import numpy as np
     2 import glob
     3 from skimage import io
     4 from skimage import transform 
     5 #读取图片
     6 path='C:/Users/hsy/Desktop/train/'
     7 def read_img(path):
     8     cate=[path+x for x in os.listdir(path) if os.path.isdir(path+x)]
     9     imgs=[]
    10     labels=[]
    11     for idx,folder in enumerate(cate):
    12         for im in glob.glob(folder+'/*.jpg'):
    13             print('reading the images:%s'%(im))
    14             img=io.imread(im)
    15             img=transform.resize(img,(64,64))
    16             imgs.append(img)
    17             labels.append(idx)
    18     return np.asarray(imgs,np.float32),np.asarray(labels,np.int32)
    19 data,label=read_img(path)
    20      
  • 相关阅读:
    Golang Gin 框架 Route备注
    golang的时区和神奇的time.Parse
    Linux下查看内存使用情况方法总结
    Golang 图片上绘制文字
    在linux中安装字体
    一键解决 go get golang.org/x 包失败
    go如何进行交叉编译
    Ubuntu中apt与apt-get命令的区别
    linux dns 工具包 -- bind-utils
    nohup 和 &的含义
  • 原文地址:https://www.cnblogs.com/hsy1941/p/11750750.html
Copyright © 2020-2023  润新知