This is a Python rendition of principal component analysis in the context of facial recognition using the Extended Yale Faces Database B which you can download here. Originally done in R, this was written in order to experiment with the sklearn library. If you have any questions about this notebook, please do not hesitate to contact me at [email protected].

This image_grid_ function reshapes the array into its original dimensions and plots the image in a grid. The col parameter allows you to specify the number of images in each row.

In [3]:

defimage_grid(D,H,W,cols=10,scale=1):""" display a grid of images H,W: Height and width of the images cols: number of columns = number of images in each row scale: 1 to fill screen """n=np.shape(D)[0]rows=int(math.ceil((n+0.0)/cols))fig=plt.figure(1,figsize=[scale*20.0/H*W,scale*20.0/cols*rows],dpi=300)foriinrange(n):plt.subplot(rows,cols,i+1)fig=plt.imshow(np.reshape(D[i,:],[H,W]),cmap=plt.get_cmap("gray"))plt.axis('off')

create_filenames allows the users to specify the current working directory where the CroppedYale folder resides and the image view for the subjects.

In [4]:

defcreate_filenames(data_dir,view_list):# loads the pictures into a list# data_dir: the CroppedYale folder# view_list: the views you wish to grabdir_list=os.listdir(data_dir)file_list=[]fordirindir_list:forviewinview_list:filename="%s/%s_%s.png"%(dir,dir,view)file_list.append(filename)return(file_list)view_list=['P00A+000E+00','P00A+005E+10','P00A+005E-10','P00A+010E+00']file_list=create_filenames(data_dir,view_list)len(file_list)

# open imageim=Image.open(file_list[0]).convert("L")# get original dimensionsH,W=np.shape(im)print'shape=',(H,W)im_number=len(file_list)# fill array with rows as image# and columns as pixelsarr=np.zeros([im_number,H*W])foriinrange(im_number):im=Image.open(file_list[i]).convert("L")arr[i,:]=np.reshape(np.asarray(im),[1,H*W])image_grid(arr,H,W)