 CoCalc Public FilesPracticals / Day4_project / Big Mega Final Project Where We Recognise People By Their Faces.ipynb
Author: Thomas Douthwaite
Views : 29
In [ ]:



## Face recognition

### Data

The data consists of 10 snapshots of 40 different people.Each image is stored in a grayscale PNG file (110 rows by 92 columns) and we'll start by reading the data and making an array of targets that specifies which person each image corresponds to.

The images are stored in a directories named as, for example, 'orl/s15/3.png' which is the 3rd image for the 15th person.

%pylab inline
figsize(8,8)

In :
!tar xf orl_faces.tgz


Now we'll read all the images into a single array, X, so that each image is a row of X. The array t will contain the number specifying the individual person.

In :
X = []
t = []
for person in range(1, 41):    # Loop over people
for n in range(1, 11):    # Loop over their images
filename = f'orl_faces/s{person}/{n}.png'
X.append(x.ravel())
t.append(person)

X = asarray(X)
target = asarray(t)
width, height = x.shape
X.shape, target.shape, width, height

((400, 10304), (400,), 112, 92)

## PCA

Here we perform PCA on the data. Since there are only 400 face vectors there can be at most 400 dimensions. After reshaping this results in an array of 'eigenfaces', which are the principal component shaped to form images.

In :
from sklearn.decomposition import PCA

M = 400   # Number of PCA components
pca = PCA(n_components=M)
pca.fit(X)
eigenfaces = pca.components_.reshape((M, width, height))

eigenfaces.shape

(400, 112, 92)

## Plot Showing Fraction of Variance Explained With

In [ ]:
figure()
plot(arange(1, M+1), cumsum(eigenvalues)/sum(eigenvalues), 'o-')
ylabel('Fraction of explained variance')
grid()

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: