Generating Eigenfaces from smile data


In [112]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

def genEigVectors(faces):
#     faces -= np.mean(faces, axis=0)
    cov = np.cov(faces.T)
    eigValues, eigVectors = np.linalg.eig(cov)
    idx = eigValues.argsort()[::-1]   
    eigValues  = eigValues[idx]
    eigVectors  = eigVectors[:,idx]
    return eigVectors

def filterFaces(faces, labels, faceType):
    if (faceType == 'smile'):
        idx = np.where(labels == 1)
        return faces[idx], labels[idx]
    elif (faceType == 'nonsmile'):
        idx = np.where(labels == 0)
        return faces[idx], labels[idx]
    else:
        return faces, labels

if __name__ == "__main__":
    # Load data
    if ('trainingFaces' not in globals()):
        trainingFaces = np.load("FacesData/trainingFaces.npy")
        trainingLabels = np.load("FacesData/trainingLabels.npy")
        testingFaces = np.load("FacesData/testingFaces.npy")
        testingLabels = np.load("FacesData/testingLabels.npy")

Eigenfaces for smiling faces


In [113]:
smilingFaces, smilingLabels = filterFaces(trainingFaces, trainingLabels, 'smile')
    eigSmilingFaces = genEigVectors(smilingFaces)
    num_images = 10
    
    aggregateMat = np.zeros((24,24))
    for i in range(num_images):
        reshaped_eigFaces = np.reshape(eigSmilingFaces[:,i],(24,24))
        real_reshaped = np.real(reshaped_eigFaces)
        aggregateMat += real_reshaped
        plt.figure(figsize=(100,2))
        plt.show(plt.imshow(real_reshaped, cmap='gray'))       
   
    print("The combined image is:\n")
    plt.figure(figsize=(100,2))
    plt.show(plt.imshow(aggregateMat, cmap='gray'))


The combined image is:

The 2nd picture, 5th, 6th and 7th seem like combination of two half faces

Eigenfaces for non-smiling faces


In [114]:
nonsmilingFaces, nonsmilingLabels = filterFaces(trainingFaces, trainingLabels, 'nonsmile')
    eigNonSmilingFaces = genEigVectors(nonsmilingFaces)
    num_images = 10
    
    aggregateMat = np.zeros((24,24))
    for i in range(num_images):
        reshaped_eigFaces = np.reshape(eigNonSmilingFaces[:,i],(24,24))
        real_reshaped = np.real(reshaped_eigFaces)
        aggregateMat += real_reshaped
        plt.figure(figsize=(100,2))
        plt.show(plt.imshow(real_reshaped, cmap='gray'))       
   
    print("The combined image is:\n")
    plt.figure(figsize=(100,2))
    plt.show(plt.imshow(aggregateMat, cmap='gray'))


The combined image is:

We can observe some half faces here as well.

Eigenfaces for all-faces


In [115]:
allFaces, allFacesLabel = trainingFaces, trainingLabels
    eigFaces = genEigVectors(allFaces)
    num_images = 10
    
    for i in range(num_images):
        reshaped_eigFaces = np.reshape(eigFaces[:,i],(24,24))
        real_reshaped = np.real(reshaped_eigFaces)
        plt.figure(figsize=(100,2))
        plt.show(plt.imshow(real_reshaped, cmap='gray'))       
    
    
    print("The combined image is:\n")
    plt.figure(figsize=(100,2))
    plt.show(plt.imshow(aggregateMat, cmap='gray'))


The combined image is:

Here we can see a mixture of smiling and non smiling facee