Application of Computer Vision 5-Using PCA Dimensionality Reduction Method to Realize a Simple Face Recognition Model

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

4. Deep Learning Combat 4 - Convolutional Neural Network (DenseNet) Mathematical Graphics Recognition + Topic Pattern Recognition

5. Deep Learning Practice 5-Convolutional Neural Network (CNN) Chinese OCR Recognition Project

6. Deep Learning Combat 6- Convolutional Neural Network (Pytorch) + Cluster Analysis to Realize Air Quality and Weather Prediction

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

10. Deep learning practice 10-mathematical formula recognition-converting pictures to Latex (img2Latex)

11. Deep learning practice 11 (advanced version) - fine-tuning application of BERT model - text classification case

12. Deep Learning Practice 12 (Advanced Edition) - Using Dewarp to Correct Text Distortion

13. Deep learning practice 13 (advanced version) - text error correction function, good luck for friends who often write typos

14. Deep learning practice 14 (advanced version) - handwritten text OCR recognition, handwritten notes can also be recognized

15. Deep Learning Combat 15 (Advanced Edition) - Let the machine do reading comprehension + you can become a question maker and ask questions

16. Deep learning practice 16 (advanced version) - virtual screenshot recognition text - can do paper contract and form recognition

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

19. Deep Learning Combat 19 (Advanced Edition) - SpeakGPT's local implementation deployment test, based on ChatGPT to implement SpeakGPT function on your own platform

20. Deep Learning Combat 20 (Advanced Edition) - File Intelligent Search System, which can search for keywords based on file content and quickly find files

21. Deep Learning Practice 21 (Advanced Edition)-AI Entity Encyclopedia Search, an encyclopedia that can be searched for any noun

22. Deep learning practice 22 (advanced version)-AI comic video generation model, make your own comic video

23. Deep Learning Combat 23 (Advanced Edition) - Semantic Segmentation Combat, to achieve the effect of character image matting (computer vision)

24. Deep Learning Combat 24- Artificial intelligence (Pytorch) builds a transformer model, really runs through the transformer model, and deeply understands the structure of the transformer

25. Deep learning practice 25-artificial intelligence (Pytorch) builds T5 model, really runs through the T5 model, and uses the T5 model to generate digital addition and subtraction results

26. Deep Learning Combat 26-(Pytorch) Building TextCNN to realize the task of multi-label text classification

27. Deep learning practice 27-Pytorch framework + BERT to realize the relationship extraction of Chinese text

28. Deep learning practice 28-AIGC project: use ChatGPT to generate customized PPT files

29. Deep Learning Combat 29-AIGC Project: Use GPT-2 (CPU environment) for text continuation and lyrics generation tasks

(pending upgrade)

Guess you like

Origin blog.csdn.net/weixin_42878111/article/details/130796899