CoCalc Public FilesPracticals / Day4_project / Big Mega Final Project Where We Recognise People By Their Faces.ipynbOpen with one click!

In [ ]:

1

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.

2

%pylab inline figsize(8,8)

3

In [2]:

!tar xf orl_faces.tgz

4

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.

5

In [4]:

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 = imread(filename) X.append(x.ravel()) t.append(person) X = asarray(X) target = asarray(t) width, height = x.shape X.shape, target.shape, width, height

6

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

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.

7

In [6]:

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

8

(400, 112, 92)

9

In [ ]:

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

10

In [ ]:

11

In [ ]:

12

In [ ]:

13

In [ ]:

14

In [ ]:

15