Python制作本地数据集

 1 #####################################################
 2 # 功能:Python制作本地数据集
 3 #####################################################
 4 
 5 # 导入相应包
 6 import os
 7 from PIL import Image
 8 import numpy as np
 9 from keras.utils import to_categorical
10 
11 dir_path = 'E:/prim/Sum'  # 图片文件路径
12 labels = []
13 images = []
14 
15 dir_len = len(os.listdir(dir_path))   # os.listdir():返回指定目录下的所有文件和目录名   len() 方法返回对象(字符、列表、元组等)长度或项目个数
16 
17 # 直接读取单个中文文件夹
18 for i, name in enumerate(os.listdir(dir_path)):    # enumerate用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
19     print('%d/%d'%(i+1, dir_len))
20 
21     if not name.endswith('.jpg'):
22         continue
23     # 准备标签
24     label = np.array(int(name.split('_')[0][4:]))   # 以下划线取第一个数字
25 
26     # 准备图片
27     img = Image.open(os.path.join(dir_path, name)).resize((100, 100))  # 将每张图片固定成同样大小
28     img = np.array(img).astype('float32') / 0xff   # 转换成二进制数
29 
30     labels.append(label)
31     images.append(img)
32 
33 # 转numpy数组
34 images = np.array(images)
35 labels = np.array(labels)
36 labels = to_categorical(labels)
37 
38 print("images:", images.shape)
39 print("labels:", labels.shape)
40 
41 np.save('images', images)
42 np.save('labels', labels)
43 
44 print('done!')
Python制作本地数据集

转载于:https://www.cnblogs.com/Junlong/p/11320320.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值