这篇博文是对之前的博文的改写。之前博文请看:利用Python PIL、cPickle读取和保存图像数据库
- 首先:
- 统计所有图片的张数,用来初始化存放样本的向量和标签向量;
图片是放在data下的第2层文件中,其中自己文件的不同,自行调节循环。
Python代码如下:
import cPickle
import os
import json
import pylab
import numpy
from PIL import Image
k = 0;
for filename1 in os.listdir(r'data'):
print filename1
filename1 = 'data/' + filename1
# 或者通过这种方式来获取根目录
# fileList = os.listdir(path)
# allFile = []
# for fileName in fileList:
# filePath = os.path.join(path, fileName)
# if os.path.isdir(filePath):
# dirList(filePath)
# allFile.append(filePath)
for filename2 in os.listdir(filename1):
print filename2
filename2 = filename1 + '/' +filename2
for filename3 in os.listdir(filename2):
print filename3
if(filename3!='Thumbs.db'):
k = k + 1
# k是用来调节加载所有图片次数的
k = k * 10
print k
k就是图片的总张数,用来初始化olivettifaces和olivettifaces。
我这是加载的表情的图片,标签值设置的是0~5;
i = 0;
j=0;
label =0;
olivettifaces=numpy.empty((k,28*28))
olivettifaces_label=numpy.empty(k)
# j是用来调节加载所有图片次数的 。
while(j<10):
for filename1 in os.listdir(r'data'):
print filename1
filename1 = 'data/' + filename1
for filename2 in os.listdir(filename1):
print filename2
label = label + 1
if(label == 7):
label = 1
filename2 = filename1 + '/' +filename2
for filename3 in os.listdir(filename2):
print filename3
if(filename3!='Thumbs.db'):
filename3 = filename2 + '/' +filename3
imgage = Image.open(filename3)
imgage = imgage.resize((28,28))
img_ndarray = numpy.asarray(imgage, dtype='float64')/256
olivettifaces[i]=numpy.ndarray.flatten(img_ndarray)
olivettifaces_label[i]=label-1
i = i + 1
j = j+1
print i
已经产生了,样本和标签数据,下边就要转化为pkl格式的文件了;
Python代码如下:
我在这里分了:训练集,验证集和测试集;
olivettifaces_label=olivettifaces_label.astype(numpy.int)
write_file=open('olivettifaces.pkl','wb')
cPickle.dump([[olivettifaces[0:28000],olivettifaces_label[0:28000]],
[olivettifaces[10001:13000],olivettifaces_label[10001:13000]],
[olivettifaces[13001:16499],olivettifaces_label[13001:16499]]],write_file,-1)
write_file.close()
用来验证读取pkl格式的文件代码:
read_file=open('olivettifaces.pkl','rb')
faces1,faces2,faces3=cPickle.load(read_file)
read_file.close()
face,label = faces1
img0=face[4].reshape(28,28)
pylab.imshow(img0)
pylab.gray()
pylab.show()
print face[28*27:28*28]
print label[1:165]