A Linear Algebra mini-project showing how eigenvalues, eigenvectors, orthogonal projection, and distance in vector spaces can be used for dimensionality reduction and face recognition.
This project applies Principal Component Analysis (PCA) to face images from the Olivetti dataset. Each image is treated as a vector in a high-dimensional space, and PCA is used to find a lower-dimensional subspace that captures the most important facial variations. The basis vectors of this subspace are called eigenfaces.
The project demonstrates that core linear algebra ideas are not only theoretical. They directly support:
- dimensionality reduction
- image approximation
- data compression
- noise reduction
- pattern recognition
The goal is to show how linear algebra can solve a real problem:
- Represent each face image as a vector.
- Find the principal directions of variation using eigenvectors of the covariance matrix.
- Project faces into a smaller subspace.
- Compare projected faces using Euclidean distance for recognition.
This project is a strong application of linear algebra because it uses:
- Vector spaces: each image is a point in
R^4096 - Mean centering: shifts the dataset so PCA works around the origin
- Covariance matrices: capture how pixel values vary together
- Eigenvalues and eigenvectors: identify the most informative directions
- Orthonormal bases: the eigenfaces form a basis for the PCA subspace
- Projection: every face is mapped onto a lower-dimensional subspace
- Norms and distance: recognition uses Euclidean distance in eigenspace
Each grayscale face image has size 64 x 64, so it is flattened into a vector in R^4096.
If the training faces are x_1, x_2, ..., x_n, then the mean face is
mu = (1/n) sum x_i
We subtract the mean face from every image:
A = X - mu
Each row of A is a centered face vector.
The full covariance matrix is
C = A^T A
Since this matrix is very large (4096 x 4096), the project uses the standard PCA trick:
C' = A A^T
This smaller matrix has size n x n, making the computation much more efficient.
If v is an eigenvector of A A^T, then A^T v gives an eigenvector of A^T A. These transformed eigenvectors become the eigenfaces.
The top eigenfaces correspond to the largest eigenvalues, which means they capture the most variance in the dataset.
If U_k contains the top k eigenfaces, then the coordinates of a face x in eigenspace are:
w = U_k^T (x - mu)
This reduces a 4096-dimensional vector to only k values.
A face can be approximated from its projection by:
x_hat = mu + sum (w_i * u_i)
or equivalently,
x_hat = mu + U_k w
Recognition is performed by nearest-neighbor comparison in eigenspace:
distance = ||w_1 - w_2||_2
The training face with minimum distance to the test face is chosen as the predicted identity.
This project shows several applications of linear algebra:
- Compression: storing a face using
kcoefficients instead of 4096 raw pixels - Approximation: reconstructing an image from a low-dimensional basis
- Feature extraction: using principal components as meaningful features
- Classification: comparing faces using distances in a reduced space
- Noise filtering: ignoring small variations not captured by leading components
miniproject/
├── README.md
├── requirements.txt
├── eigenfaces.py
├── fastapi_app.py
├── utils.py
├── input/
│ ├── heldout_1_george_w_bush.png
│ ├── heldout_2_colin_powell.png
│ ├── heldout_3_ariel_sharon.png
│ ├── heldout_4_junichiro_koizumi.png
│ ├── heldout_5_hugo_chavez.png
│ └── ...
└── output/
├── mean_face.png
├── eigenfaces.png
├── reconstruction.png
├── variance_capture.png
├── projection_breakdown.png
└── accuracy_curve.png
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python eigenfaces.pyTo run the interactive FastAPI web app:
uvicorn fastapi_app:app --reloadThen open the local URL shown by Uvicorn, usually:
http://127.0.0.1:8000
The web app uses the larger LFW People dataset from scikit-learn instead of the smaller Olivetti dataset. It lets you upload a face image from the browser, converts it to grayscale, crops/resizes it to the LFW face shape, projects it into the eigenface subspace, and returns the closest matching LFW training-set person. The first run may download the LFW dataset if it is not already cached.
Sample upload images are available in the input/ folder. Open the web app,
click Choose File, and select one of those PNG files. These examples are
held-out test images, so the exact photos are not used to train the eigenface
model, but the people are present in the known LFW classes.
Important limitation: if the uploaded person is not part of the LFW training set, the app still returns the closest dataset person. That means the result is a nearest mathematical match, not a guaranteed real identity.
When the demo runs, it:
- Loads the Olivetti face dataset.
- Splits it into training and testing sets.
- Computes the mean face.
- Computes eigenfaces using PCA.
- Projects training faces into eigenspace.
- Saves visualizations in the
output/folder. - Evaluates recognition accuracy for different numbers of components.
- Prints linear algebra diagnostics for presentation.
Shows the average of all training faces. Geometrically, this is the center of the dataset in face space.
Shows the principal basis vectors of the face subspace. These are the eigenvectors associated with the largest eigenvalues.
Shows how a face is reconstructed with different numbers of components. This demonstrates approximation in a subspace.
Shows cumulative explained variance. This justifies why a small number of eigenfaces is enough to preserve most information.
Shows:
- original face
- mean face
- projected reconstruction
- residual error
This is one of the best figures to explain orthogonal projection and approximation error.
Shows how recognition accuracy changes with the number of principal components.
Typical results on the Olivetti dataset are:
- around
87%accuracy with10components - around
94%accuracy with50components - around
94%accuracy with100components - about
94%explained variance with100components
These values show that a much smaller subspace can still preserve most useful facial information.
If you want a clear classroom explanation, present it in this order:
- A face image is converted into a vector in
R^4096. - The dataset is centered by subtracting the mean face.
- PCA finds the most important directions of variation.
- These directions become eigenfaces.
- Each face is projected into a low-dimensional eigenspace.
- Similar faces stay close together in this reduced space.
- Reconstruction shows what information the subspace preserves.
- Residual error shows what information is lost.
If someone asks, "Where is linear algebra used here?", a concise answer is:
Each face image is treated as a vector, PCA computes eigenvectors of the covariance structure, those eigenvectors form an orthogonal basis called eigenfaces, and each face is projected onto that basis for compression, reconstruction, and recognition using Euclidean distance.
This project demonstrates that linear algebra is a practical tool for data analysis and machine learning. Using eigenvalues, eigenvectors, covariance matrices, and subspace projection, it becomes possible to represent high-dimensional images compactly and perform accurate face recognition in a mathematically elegant way.
- Turk, M., and Pentland, A. "Eigenfaces for Recognition." Journal of Cognitive Neuroscience, 1991.
- Belhumeur, P. N., Hespanha, J. P., and Kriegman, D. J. "Eigenfaces vs. Fisherfaces." IEEE Transactions on Pattern Analysis and Machine Intelligence, 1997.
- scikit-learn documentation for the Olivetti faces dataset.
- scikit-learn documentation for the LFW People faces dataset.
Rohit Pujari
Linear Algebra Mini-Project, 4th Semester