import utilities as ut import pca import os path = '../data/' database = 'orl_faces' subspace = 'orl_subspace.npz' components = 400 rows = 112 columns = 92 if not os.path.exists(path + subspace): M = ut.load_images(database) eigenvalues, W, mu = pca.create_subspace(M, components) pca.save_subspace(path + subspace, eigenvalues, W, mu) else: eigenvalues, W, mu = pca.load_subspace(path + subspace) img = ut.create_image(rows,columns, 3) x = pca.project_image(img,W,mu) y = pca.reverse_projection(x,W,mu) image = ut.normalize_image(ut.unflatten_image(y,rows,columns)) original = ut.unflatten_image(img,rows,columns) ut.display_image(image) ut.display_image(original)
projections = 'orl_projections_' + str(components) + '.npz' image_path = database + 's10/1.pgm' if not os.path.exists(path + subspace): M = ut.load_images(database) eigenvalues, W, mu = pca.create_subspace(M, components) pca.save_subspace(path + subspace, eigenvalues, W, mu) else: eigenvalues, W, mu = pca.load_subspace(path + subspace) if not os.path.exists(path + projections): for j in range(40): index = j + 1 img = ut.load_image(database + 's' + str(index) + '/9.pgm') x = pca.project_image(img,W,mu) ut.save_projection('face ' + str(index), x, projections) print index img = ut.load_image(image_path) x = pca.project_image(img,W,mu) y = pca.reverse_projection(x,W,mu) image = ut.normalize_image(ut.unflatten_image(y,rows,columns)) ut.display_image(image, str(400) + ' components') name, projection = pca.find_closest(x, path + projections) closest_img = pca.reverse_projection(projection,W,mu) closest_image = ut.normalize_image(ut.unflatten_image(closest_img,rows,columns)) print name ut.display_image(closest_image, name[0])
import utilities as ut import pca import os path = '../data/' database = 'orl_faces' subspace = 'orl_subspace.npz' components = 400 rows = 112 columns = 92 if not os.path.exists(path + subspace): M = ut.load_images(database) eigenvalues, W, mu = pca.create_subspace(M, components) pca.save_subspace(path + subspace, eigenvalues, W, mu) else: eigenvalues, W, mu = pca.load_subspace(path + subspace) for i in range(15): ut.display_image(ut.unflatten_image(ut.normalize_image(W[:,i]), rows, columns), str(i))
# test.py # used to quickly test functionality of code # requires orl_faces images to be saved in data folder import utilities as utils import pca components = 400 rows = 112 columns = 92 M = utils.load_images('orl_faces') img = utils.create_image(rows, columns, 2) eigenvalues, W, mu = pca.create_subspace(M,components) print W.shape, img.shape, mu.shape x = pca.project_image(img,W,mu) print x.shape y = pca.reverse_projection(x,W,mu) print y.shape image = utils.normalize_image(utils.unflatten_image(y,rows,columns)) for i in range(1): print 'eigenface ' + str(i) + ': ', utils.normalize_image(utils.unflatten_image(W[:,i],rows,columns)).shape utils.display_image(utils.normalize_image(utils.unflatten_image(W[:,i],rows,columns))) utils.display_image(image) utils.display_image(utils.unflatten_image(img,rows,columns))
import pca import os path = '../data/' database = 'orl_faces/' subspace = 'orl_subspace.npz' image_path = database + 's10/1.pgm' components = 400 rows = 112 columns = 92 if not os.path.exists(path + subspace): M = ut.load_images(database) eigenvalues, W, mu = pca.create_subspace(M, components) pca.save_subspace(path + subspace, eigenvalues, W, mu) else: eigenvalues, W, mu = pca.load_subspace(path + subspace) img = ut.load_image(image_path) num_components = [1,2,3,4,5,6,7,8,9,10,15,20,50,75,100,150,200,400] for i in num_components: x = pca.project_image(img,W[:,1:i],mu) y = pca.reverse_projection(x,W[:,1:i],mu) image = ut.normalize_image(ut.unflatten_image(y,rows,columns)) original = ut.unflatten_image(img,rows,columns) ut.display_image(image, str(i) + ' components') ut.display_image(original, 'original')