0 1 2 3 4 5 6 7 8 9 10 11 12 13 | ''' ''' def csv_read(data_dir): images = [] labels = [] for sub_dir in os.listdir(data_dir): with open(os.path.join(data_dir, sub_dir, 'at.txt'), 'r') as f: lines = f.read().splitlines() for i in lines: img = cv2.imread(i, 0) images.append(img) labels.append(sub_dir) images = np.array(images, dtype=np.uint8) labels = np.array(labels, dtype=np.int32) return images, labels |
''' '''
/path/to/image0.ext;0
/path/to/image1.ext;0
/path/to/image2.ext;0
...
/path/to/image10.ext;10
The Eigenfaces algorithm is based on Principal Component Analysis (PCA), which reduces the dimensionality of the data by capturing the directions with the highest variance. In the context of face recognition, this helps to retain only the most important information from the images.
Since face images contain a lot of redundant pixel data, applying PCA helps remove unnecessary information and yields discriminating features. A classifier is then trained on these features to recognize faces.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ''' ''' import sys import os import argparse import numpy as np import cv2 def main(args): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument('dataset', help='Dataset directory') args = parser.parse_args() images, labels = csv_read(args.dataset) # PCA (Eigenface) recognizer pca_recognizer = cv2.face.EigenFaceRecognizer.create() pca_recognizer.train(images[:5], labels[:5]) label, confidence = pca_recognizer.predict(images[5]) print(f"Predicted label: {label}, Confidence: {confidence}") if __name__ == '__main__': main(sys.argv) |
Fisherfaces, based on Linear Discriminant Analysis (LDA), maximizes the ratio of inter-class variance to intra-class variance when reducing dimensionality. This makes it more effective than PCA when external factors, like lighting, generate much of the variance in the data.
After extracting features using LDA, a classifier is trained on these features to recognize faces. To use Fisherfaces, replace the PCA code in the previous example with the following:
0 1 2 3 4 5 | ''' ''' # Fisherface recognizer fisher_recognizer = cv2.face.FisherFaceRecognizer.create() fisher_recognizer.train(images[:5], labels[:5]) label, confidence = fisher_recognizer.predict(images[5]) print(f"Predicted label: {label}, Confidence: {confidence}") |
The Local Binary Patterns Histograms (LBPH) algorithm extracts local features by comparing pixel intensities with their neighbors. This comparison produces binary patterns that are robust against changes in lighting, rotation, and scale, making LBPH more reliable in challenging conditions.
To use LBPH for face recognition, replace the PCA code with the following:
0 1 2 3 4 5 | ''' ''' # LBPH recognizer lbph_recognizer = cv2.face.LBPHFaceRecognizer_create() lbph_recognizer.train(images[:5], labels[:5]) label, confidence = lbph_recognizer.predict(images[5]) print(f"Predicted label: {label}, Confidence: {confidence}") |
Figure 1: Face recognition result. Image source: Flickr