Read and save image databases with Python PIL, cpickle
@author: Wepon
@blog: http://blog.csdn.net/u012162613/article/details/43226127
Computer vision, machine learning tasks, often with images, in C + + has mature OpenCV can be used, in Python also has an image processing library pil (Python image Library), Of course PIL does not have OPENCV so many functions (such as some human face detection algorithm), but on Python, we use PIL to do some basic image reading and saving work, because the algorithm, Python has a lot of powerful algorithm library (machine learning Library Sklearn, Deep Learning Library Theano).
Taking a face image database Olivetti faces as an example, this paper shows how to use PIL module, Cpickle module to read and save the image database as a pkl file.
regarding the use of the Cpickle module, I have mentioned in this article: Deeplearning Tutorial (2) The Machine learning algorithm saves parameters during the training process. The following will not be repeated.
One, face Image Library Olivetti faces introduction Olivetti faces is a small face Bank of New York University, consisting of 400 images of 40 people, that is, 10 images for each person's face. Each image has a grayscale level of 8 bits, and the grayscale size of each pixel is between 0-255 and the size of each image is 64x64. For example, this picture size is 1190*942, altogether has 20*20 Zhang person face, therefore each face size is (1190/20) * (942/20) namely 57*47=2679:
second, using Python PIL, cpickle Read and save Olivetti FacesFirst Use pil.image open to get this picture, in order to facilitate the numerical calculation, convert it to the Numpy.array type, and then each picture is amortized into a one-dimensional vector 1*2679, because there are 400, so got 400* 2679 Numpy.array, then use the Cpickle module to save the converted Numpy.array as a pkl file. Note that this is data without labels, we can manually give them the category 0~39, each category has 10 samples, so create a new 400*1 label, as the corresponding category of each picture.
The code is as follows:
#读取人脸库olivettifaces, and stored as PKL file import numpyfrom PIL import Imageimport cpickle# Read the original image and convert it to Numpy.ndarray, converting the grayscale value from 0~256 to 0~1img = Image.open ('/home/wepon/olivettifaces.gif ') Img_ndarray = Numpy.asarray (IMG, dtype= ' float64 ')/256# picture large hours 1190*942, altogether 20*20 personal face graph, therefore each face figure size is (1190/20) * (942/20) namely 57*47=2679# Store all 400 samples as an array of 400*2679, each representing a face graph, and No. 0 to 9th, 10~19, 20~29 ... The lines belong to the same person's face. In addition, the Olivettifaces_label represents the category of each sample, it is a 400-dimensional vector, there are 40 classes of 0~39, representing 40 different people. Olivettifaces=numpy.empty ((400,2679)) for row in range: for column in range: olivettifaces[row*20+column]= Numpy.ndarray.flatten (Img_ndarray [row*57: (row+1) *57,column*47: (column+1) *47]) #建一个 <span style= "font-family: SimSun; " >olivettifaces_label</span>olivettifaces_label=numpy.empty (+) for label in range (+): Olivettifaces_ Label[label*10:label*10+10]=labelolivettifaces_label=olivettifaces_label.astype (Numpy.int) # Save Olivettifaces and Olivettifaces_label to olivettifaces.pkl file Write_file=open ('/home/wepon/olivettifaces.pkl ', ' WB ') Cpickle.dump (olivettifaces,write_file,-1) Cpickle.dump (olivettifaces_label,write_file,-1) write_file.close ()
In this way, a olivettifaces.pkl file is obtained under directory/home/wepon/. This file stores a vector of 400*2679 and a vector of 400*1, representing the sample and the sample category.
to read a single picture from the OLIVETTIFACES.PKL:
If you want to view a single picture, you must first reshape the 2679-dimensional vector representing the picture, such as: Faces[1].reshape (57,47). Call Pylab to display the picture.
Import Cpickleimport pylabread_file=open ('/home/wepon/olivettifaces.pkl ', ' RB ') Faces=cpickle.load (read_file) Read_file.close () img1=faces[1].reshape (57,47) pylab.imshow (IMG) pylab.gray () pylab.show ()
How to use OLIVETTIFACES.PKL in machine learning algorithms ?
In machine learning algorithms, we typically split samples into training samples, validation samples, test samples, and corresponding labels. How do I split it? The code is as follows:
Read Olivettifaces.pkl file, divided into training set (40*8 samples), validation set (40*1 samples), test set (40*1 samples) import Cpickleread_file=open ('/home/wepon/ Olivettifaces.pkl ', ' RB ') faces=cpickle.load (read_file) label=cpickle.load (read_file) Read_ File.close () Train_data=numpy.empty ((320,2679)) Train_label=numpy.empty ((valid_data=numpy.empty)) 40,2679 Valid_label=numpy.empty (Test_data=numpy.empty) ((40,2679)) Test_label=numpy.empty (+) for I in range (+): Train_ Data[i*8:i*8+8]=faces[i*10:i*10+8]train_label[i*8:i*8+8]=label[i*10:i*10+8]valid_data[i]=faces[i*10+8]valid_ LABEL[I]=LABEL[I*10+8]TEST_DATA[I]=FACES[I*10+9]TEST_LABEL[I]=LABEL[I*10+9]
Read and save image databases with Python PIL, cpickle