Use C++ and OpenCV for face recognition, including model training and calling process

introduction:

Face recognition is an important application in the field of computer vision, which can be used in security systems, face unlocking and facial expression analysis. This tutorial will show you how to implement basic face recognition functionality using C++ and the OpenCV library. We will split it into two parts, first the model training process and then the calling process.

Part 1: Model Training Process

Install OpenCV and C++ development environment:
Before starting, make sure you have installed OpenCV library and C++ development environment. You can download and install OpenCV from the official website of OpenCV, and set up the C++ compilation environment.

Prepare the dataset:
In order to train the face recognition model, you need a dataset of labeled faces. Collect a sufficient number of face images and label each face with a unique label.

Data preprocessing:
Before training, the data set needs to be preprocessed. Use the face detector of the OpenCV library to locate and crop the face region. Make sure all images are the same size and format.

Train the face recognition model:
Using the preprocessed dataset, we can start training the face recognition model. In OpenCV, models can be trained using algorithms such as LBPH (Local Binary Patterns Histograms), Eigenfaces or Fisherfaces. Choose an algorithm that suits your needs and train it using the training dataset.

Save the model:
After the training is complete, save the trained model to disk for subsequent calls.

Part 2: Calling Procedure

Load the face recognition model:
Before calling the face recognition function, first load the previously trained model. Load the model file from disk using the API provided by OpenCV.

Initialize the camera:
Before performing face recognition, the camera needs to be initialized to obtain video streams. Use OpenCV's VideoCapture class to open and read camera data.

Face detection and recognition:
In each image frame, a face detector is used to detect face regions. Then, compare the detected face with the previously trained model

Feature extraction and matching:
feature vectors are extracted from detected face regions. This can be achieved by converting face images to grayscale and applying preprocessing methods such as histogram equalization. Then, use the trained face recognition model to match the extracted feature vectors with known face labels for comparison.

Display results:
Based on the matching results, a bounding box can be drawn in the image and the recognition results displayed. Draw a rectangular frame around the recognized face area, and display the corresponding face label or identity information in the frame.

Real-time face recognition:
put the above steps into a loop to realize real-time face recognition function. Continuously read images from the camera, perform face detection, feature extraction and matching, and then display the recognition results.

Code

The following is a simple sample code that demonstrates the training process and calling process of the face recognition model. The training process reads images from folders, each folder representing a person.

Training process code:


#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <filesystem>

using namespace cv;
namespace fs = std::filesystem;

// 函数用于读取文件夹中的图像文件并提取人脸特征向量
void extractFaceFeatures(const std::string& folderPath, std::vector<cv::Mat>& images, std::vector<int>& labels)
{
    for (const auto& personDir : fs::directory_iterator(folderPath))
    {
        if (!fs::is_directory(personDir)) continue;
        int label = std::stoi(personDir.path().filename().string());

        for (const auto& imgPath : fs::directory_iterator(personDir))
        {
            cv::Mat image = cv::imread(imgPath.path().string(), cv::IMREAD_GRAYSCALE);
            if (image.empty()) continue;

            images.push_back(image);
            labels.push_back(label);
        }
    }
}

int main()
{
    std::string dataFolderPath = "path/to/data/folder";
    std::vector<cv::Mat> images;
    std::vector<int> labels;

    extractFaceFeatures(dataFolderPath, images, labels);

    // 创建并训练人脸识别模型(使用LBPH算法)
    Ptr<face::LBPHFaceRecognizer> model = face::LBPHFaceRecognizer::create();
    model->train(images, labels);

    // 保存模型到磁盘
    model->save("path/to/save/model.xml");

    std::cout << "模型训练完成并保存成功!" << std::endl;

    return 0;
}

Call procedure code:


#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;

int main()
{
    
    
    // 加载训练好的人脸识别模型
    Ptr<face::LBPHFaceRecognizer> model = face::LBPHFaceRecognizer::create();
    model->read("path/to/saved/model.xml");

    // 初始化摄像头
    VideoCapture capture(0);
    if (!capture.isOpened())
    {
    
    
        std::cerr << "无法打开摄像头!" << std::endl;
        return -1;
    }

    // 创建人脸检测器
    CascadeClassifier faceCascade;
    faceCascade.load("path/to/haarcascade_frontalface_default.xml");

    while (true)
    {
    
    
        Mat frame;
        capture >> frame;
        if (frame.empty()) break;

        // 转换为灰度图像
        Mat grayFrame;
        cvtColor(frame, grayFrame, COLOR_BGR2GRAY);

        // 人脸检测
        std::vector<Rect> faces;
        faceCascade.detectMultiScale(grayFrame, faces, 1.1, 3, 0, Size(80, 80));

        for (const Rect& face : faces)
        {
    
    
            // 人脸识别
            Mat faceImage = grayFrame(face);
            int label;
            double confidence;
            model->predict(faceImage, label, confidence);

            // 在图像上绘制矩形
			 rectangle(frame, face, Scalar(255, 0, 0), 2);

            // 显示识别结果
            std::string labelText = "Unknown";
            if (confidence < 70.0)  // 设置一个阈值进行识别结果判断
            {
    
    
                labelText = "Person " + std::to_string(label);
            }

            Point labelPosition(face.x, face.y - 10);
            putText(frame, labelText, labelPosition, FONT_HERSHEY_SIMPLEX, 0.9, Scalar(255, 0, 0), 2);
        }

        // 显示视频流
        imshow("Face Recognition", frame);

        // 按下ESC键退出
        if (waitKey(1) == 27) break;
    }

    return 0;
}

Please note that the above code only provides a basic example to demonstrate the training and calling process of face recognition. In practical applications, further optimization and exception handling may be required to improve accuracy and stability. At the same time, it is necessary to download and provide OpenCV's face detector and training data set for actual operation.

Summarize:

This tutorial covers the basic steps of face recognition using C++ and OpenCV. First, we explained the model training process, including preparing the dataset, data preprocessing, training the face recognition model, and saving the model. Then, we introduce the calling process, including loading the model, initializing the camera, face detection and recognition, feature extraction and matching, and displaying the recognition results. With this getting started guide, you can start building your own face recognition application using C++ and OpenCV.

Guess you like

Origin blog.csdn.net/qq_46017342/article/details/130660077