Hello everyone, I am Wei Xue AI. Today I will introduce you to the application of computer vision 5-Using PCA dimensionality reduction method to realize a simple face recognition model. This article will introduce how to use principal component analysis (PCA) to realize a simple face recognition model. . First, we will briefly introduce the principle of PCA and its application in face recognition. Next, we will demonstrate how to use Python to implement PCA dimensionality reduction through examples, and give a complete code example.
Article directory
- I. Introduction
- 2. Principle of PCA
- 3. Application of PCA in face recognition
- 4. Realization of simple face recognition model
- 4.1 Data preprocessing
- 4.2 Realize PCA dimensionality reduction
- 4.3 Calculate Euclidean distance for face recognition
- 4.4 Code implementation
- 5. Summary
I. Introduction
Principal Component Analysis (PCA) is a widely used technique for data dimensionality reduction, compression, and visualization. It converts the original data into a set of new variables (principal components) through linear transformation, where each new variable is a linear combination of the original variables, and they are sorted according to importance, so that the first principal component retains the original as much as possible information in the data, while subsequent principal components in turn capture the remaining information. Therefore, PCA can compress the raw data into fewer dimensions, which makes data analysis and visualization easier. Specifically, assuming there are m n-dimensional data samples, they are represented as an mxn matrix X. In the field of face recognition, PCA can be used to extract the main features of face images, thereby reducing the data dimension, reducing the amount of calculation, while maintaining a high recognition rate.
2. Principle of PCA
The goal of PCA is to project high-dimensional data into a low-dimensional space while maintaining the main characteristics of the data. The specific steps are as follows :
1. Calculate the mean vector and covariance matrix of the data;
2. Calculate the eigenvalues and eigenvectors of the covariance matrix;
3. Arrange the eigenvalues in descending order, and select the eigenvectors corresponding to the top k largest eigenvalues to form a projection matrix (k is the dimension after dimension reduction);
4. Project the data onto the projection matrix to obtain the dimensionally reduced data.
3. Application of PCA in face recognition
In the face recognition problem, we can think of image data as points in a high-dimensional space. Through PCA dimensionality reduction, we can project the image into a low-dimensional space while preserving the main feature information. Then, we can measure the similarity between images by calculating the Euclidean distance and other methods, so as to realize face recognition.
4. Realization of simple face recognition model
4.1 Data preprocessing
Before starting to implement PCA dimensionality reduction, we need to preprocess the data. Specific steps are as follows:
1. Read face image data;
2. Convert the image to a grayscale image;
3. Convert the grayscale image to a one-dimensional vector;
4. Stack all image vectors into a matrix.
4.2 Realize PCA dimensionality reduction
When implementing PCA dimensionality reduction, we can use the functions provided by the NumPy library in Python to complete the calculation steps in the aforementioned PCA principle.
4.3 Calculate Euclidean distance for face recognition
After completing the PCA dimension reduction, we can calculate the Euclidean distance between the test image and the training image in the dimension reduction space, and then select the image with the closest distance as the recognition result.
4.4 Code implementation
The following is the complete code for implementing a simple face recognition model using PCA:
import numpy as np
import cv2
import os
def load_images(path):
images = []
labels = []
for subdir, dirs, files in os.walk(path):
for file in files:
img_path = os.path.join(subdir, file)
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (248, 248))
img_vector = img.flatten()
images.append(img_vector)
labels.append(subdir.split("/")[-1])
return np.array(images), np.array(labels)
def pca(X, k):
#print(X.shape)
mean = np.mean(X, axis=0)
X_centered = X - mean
cov_matrix = np.cov(X_centered.T)
# 使用 atleast_2d 函数将 cov_matrix 转换为至少有两个维度的数组
cov_matrix = np.atleast_2d(cov_matrix)
eig_vals, eig_vecs = np.linalg.eig(cov_matrix)
sorted_indices = np.argsort(eig_vals)[::-1]
top_k_eig_vecs = eig_vecs[:, sorted_indices[:k]]
X_centered = X_centered.reshape(-1, 1)
print(X_centered.shape)
#top_k_eig_vecs = top_k_eig_vecs.T
X_pca = X_centered.dot(top_k_eig_vecs)
return X_pca, top_k_eig_vecs, mean
def euclidean_distance(a, b):
return np.sqrt(np.sum((a - b) ** 2))
def face_recognition(test_image, train_images, train_labels, eig_vecs, mean):
test_image_centered = test_image - mean
test_image_centered =test_image_centered.reshape(-1, 1)
test_image_pca = test_image_centered.dot(eig_vecs)
print(test_image_pca.shape)
min_distance = float("inf")
best_match = None
train_images =[train_images]
for i, train_image_pca in enumerate(train_images):
print(test_image_pca.shape)
distance = euclidean_distance(test_image_pca, train_image_pca)
if distance < min_distance:
min_distance = distance
best_match = train_labels[i]
return best_match
if __name__ == "__main__":
train_images_path = "图片文件夹"
test_image_path = "1.png"
# Load and preprocess images
train_images, train_labels = load_images(train_images_path)
print(train_images, train_labels)
test_image = cv2.imread(test_image_path, cv2.IMREAD_GRAYSCALE)
test_image = cv2.resize(test_image, (248, 248))
test_image_vector = test_image.flatten()
#train_images = np.stack(train_images, axis=1)
# Perform PCA on training images
k = 50
train_images_pca, eig_vecs, mean = pca(train_images[0], k)
# Perform face recognition
result = face_recognition(test_image_vector, train_images_pca, train_labels, eig_vecs, mean)
print("测试的图片类别是:", result)
Note the replacement "图片文件夹"
and "1.png" with the actual training image and test image paths.
V. Summary
This article introduces the principle and implementation process of using PCA dimensionality reduction method to realize a simple face recognition model. Through PCA dimensionality reduction, we can reduce the amount of computation while preserving the main features of the image. This method may need further optimization and improvement in practical applications, such as using other distance measurement methods or combining other feature extraction methods.
Past works:
Deep Learning Practical Projects
1. Deep learning practice 1-(keras framework) enterprise data analysis and prediction
2. Deep learning practice 2-(keras framework) enterprise credit rating and prediction
3. Deep Learning Practice 3-Text Convolutional Neural Network (TextCNN) News Text Classification
5. Deep Learning Practice 5-Convolutional Neural Network (CNN) Chinese OCR Recognition Project
7. Deep learning practice 7-Sentiment analysis of e-commerce product reviews
8. Deep Learning Combat 8-Life Photo Transformation Comic Photo Application
9. Deep learning practice 9-text generation image-local computer realizes text2img
12. Deep Learning Practice 12 (Advanced Edition) - Using Dewarp to Correct Text Distortion
17. Deep Learning Practice 17 (Advanced Edition) - Construction and Development Case of Intelligent Assistant Editing Platform System
18. Deep Learning Combat 18 (Advanced Edition) - 15 tasks of NLP fusion system, which can realize the NLP tasks you can think of on the market
28. Deep learning practice 28-AIGC project: use ChatGPT to generate customized PPT files
(pending upgrade)