Human Facial recognition to a large extent is a solved problem and is being ubiquitously used for numerous security applications. Although it is hard for an algorithm to recognize faces at different lighting conditions, poses, scales, and to some extent occlusions with high fidelity, such algorithms have been deployed in the newly released Iphone X and Samsung Galaxy S8 which peform amazingly well. The success and performance of these algorithms can be attributed to deep neural networks which are increasingly being used widely for recognition tasks.

## Dataset

In this post we will develop an intuition on how facial recognition works. We will use a small but extremely popular dataset known as the Yale Face Dataset. This contains images of the faces of 15 individuals. For each individual there are 11 images taken under a variety of conditions e.g., the person makes a happy expression, wears glasses etc.

We first build a simple human facial recognizer by using a Support Vector Machine trained on a lower dimensional representation of faces using a dimensionality reduction technique called PCA. All the code developed is in Python3.

## Pipeline

All the images in the dataset are in .gif format which is a sequence of frames. We need to extract the frames and convert them into a format that is suitable for matrix operations. This could be done using the Image class() from the PIL package in Python3.

First we import the dependencies.

import os
import numpy as np
import pandas as pd
import cv2
from scipy import ndimage
from PIL import Image
from sklearn import preprocessing
import matplotlib.pyplot as plt


Then, we read the images from the dataset one-by-one and convert them to a Numpy 2-D matrix. These matrices are stored in an array faces_data. At the same time we also retreive the class that the image belongs to, in this case the Class ID of the person and store it in an array class_label.

# hello
data = 'yalefaces/'
faces_data = []
class_label = []
for filename in os.listdir(data):
if filename.endswith(".txt") or filename.endswith(".DS_Store") :
continue
else:
img_array = np.array(Image.open(data+filename)).astype(float)
img_array = cv2.resize(img_array, (80,61))
faces_data.append(img_array)
label = filename[7:9]
class_label.append(label)
faces_data = np.array(faces_data)
class_label = np.array(class_label)


### Visualizing a sample image from the dataset def normalize(img):
return img/255.

X = []
for img in faces_data:
img = normalize(img)
X.append(np.reshape(img, (1, img.shape*img.shape)))
X = np.array(X)
X = np.reshape(X, (X.shape, X.shape))
mu = np.mean(X, axis=0)
X = X - mu