OpenCV笔记--人脸识别算法Eigenfaces和Fisherfaces

目录

1--前言

2--处理ORL数据集

3--Eigenfaces复现过程

4--Fisherfaces复现过程

5--分析


1--前言

①SYSU模式识别课程作业

②配置:基于Windows11、OpenCV4.5.5、VSCode、CMake(参考OpenCV配置方式)

③原理及源码介绍:Face Recognition with OpenCV

④数据集:ORL Database of Faces

2--处理ORL数据集

①源码:

import sys
import os.path

if __name__ == "__main__":

    BASE_PATH = './ORL/att_faces/orl_faces/'
    SEPARATOR = ";"
    dir_txt = open("./dir.txt", 'w')

    label = 0
    for dirname, dirnames, filenames in os.walk(BASE_PATH):
        # dirname当前路径; dirnames当前路径下所有目录名(不包含子目录);filenames当前路径下的所有文件名(不包含子目录)
        for subdirname in dirnames: # 遍历每一个目录
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                abs_path = "%s/%s" % (subject_path, filename)
                print("%s%s%d" % (abs_path, SEPARATOR, label))
                dir_txt.write(abs_path)
                dir_txt.write(SEPARATOR)
                dir_txt.write(str(label))
                dir_txt.write("\n")
            label = label + 1
    dir_txt.close()

②运行及结果:

python create_csv.py

OpenCV笔记--人脸识别算法Eigenfaces和Fisherfaces_第1张图片

3--Eigenfaces复现过程

①源码:

// 引用依赖
#include "opencv2/core.hpp"
#include "opencv2/face.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include 
#include 
#include 

// 使用相应的命名空间
using namespace cv;
using namespace cv::face;
using namespace std;

// 标准化函数
static Mat norm_0_255(InputArray _src) {
    Mat src = _src.getMat();
    // Create and return normalized image:
    Mat dst;
    switch(src.channels()) {
    case 1:
        cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
        break;
    case 3:
        cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
        break;
    default:
        src.copyTo(dst);
        break;
    }
    return dst;
}

// 读取CSV文件函数
static void read_csv(const string& filename, vector& images, vector& labels, char separator = ';') {
    std::ifstream file(filename.c_str(), ifstream::in);
    if (!file) {
        string error_message = "No valid input file was given, please check the given filename.";
        CV_Error(Error::StsBadArg, error_message);
    }
    string line, path, classlabel;
    while (getline(file, line)) {
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, classlabel);
        if(!path.empty() && !classlabel.empty()) {
            images.push_back(imread(path, 0));
            labels.push_back(atoi(classlabel.c_str()));
        }
    }
}
int main(int argc, const char *argv[]) {
    
    //检查argc是否符合要求
    if (argc < 2) {
        cout << "usage: " << argv[0] << "   " << endl;
        exit(1);
    }
    string output_folder = ".";
    if (argc == 3) {
        output_folder = string(argv[2]);
    }

    // CSV文件的路径
    string fn_csv = string(argv[1]);
    
    // 初始化存储imgs和labels的向量
    vector images;
    vector labels;

    // 读取CSV文件
    try {
        read_csv(fn_csv, images, labels);
    } catch (const cv::Exception& e) {
        cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
        exit(1);
    }

    // 判断img数目是否符合要求
    if(images.size() <= 1) {
        string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
        CV_Error(Error::StsError, error_message);
    }

    // images的高度
    int height = images[0].rows;

    // 从训练集中选择一张图片作为测试集
    Mat testSample = images[images.size() - 1];
    int testLabel = labels[labels.size() - 1];
    images.pop_back();
    labels.pop_back();

    // 创建模型,使用PCA特征脸算法
    Ptr model = EigenFaceRecognizer::create();
    model->train(images, labels); // 训练模型
    int predictedLabel = model->predict(testSample);  // 使用测试集测试模型

    // 打印准确率
    string result_message = format("Predicted class = %d / Actual class = %d.", predictedLabel, testLabel);
    cout << result_message << endl;
    // 获取模型的特征值
    Mat eigenvalues = model->getEigenValues();
    // 展示特征向量
    Mat W = model->getEigenVectors();
    // 从训练集中获取样本均值
    Mat mean = model->getMean();
    // 根据argc判断进行展示或保存操作
    if(argc == 2) {
        imshow("mean", norm_0_255(mean.reshape(1, images[0].rows)));
    } else {
        imwrite(format("%s/mean.png", output_folder.c_str()), norm_0_255(mean.reshape(1, images[0].rows)));
    }
    // 显示或保存特征脸
    for (int i = 0; i < min(10, W.cols); i++) {
        string msg = format("Eigenvalue #%d = %.5f", i, eigenvalues.at(i));
        cout << msg << endl;
        // 获取特征向量
        Mat ev = W.col(i).clone();
        // resize成原始大小,并归一化到0-255
        Mat grayscale = norm_0_255(ev.reshape(1, height));
        // 显示图像并应用Jet颜色图以获得更好的观感。
        Mat cgrayscale;
        applyColorMap(grayscale, cgrayscale, COLORMAP_JET);
        // 根据argc判断进行展示或保存操作
        if(argc == 2) {
            imshow(format("eigenface_%d", i), cgrayscale);
        } else {
            imwrite(format("%s/eigenface_%d.png", output_folder.c_str(), i), norm_0_255(cgrayscale));
        }
    }
    // 在一些预定义的步骤中显示或保存图像重建的过程:
    for(int num_components = min(W.cols, 10); num_components < min(W.cols, 300); num_components+=15) {
        // 从模型中分割特征向量
        Mat evs = Mat(W, Range::all(), Range(0, num_components));
        Mat projection = LDA::subspaceProject(evs, mean, images[0].reshape(1,1));
        Mat reconstruction = LDA::subspaceReconstruct(evs, mean, projection);
        // 归一化
        reconstruction = norm_0_255(reconstruction.reshape(1, images[0].rows));
        // 根据argc判断进行展示或保存操作
        if(argc == 2) {
            imshow(format("eigenface_reconstruction_%d", num_components), reconstruction);
        } else {
            imwrite(format("%s/eigenface_reconstruction_%d.png", output_folder.c_str(), num_components), reconstruction);
        }
    }
    // 如果没有写入输出文件夹,则等待键盘输入
    if(argc == 2) {
        waitKey(0);
    }
    return 0;
}

②编译过程:

CMakeLists.txt如下:

cmake_minimum_required(VERSION 3.24)  # 指定 cmake的 最小版本
project(test) # 设置项目名称

find_package(Opencv REQUIRED)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
add_executable(eigenfaces_demo eigenfaces.cpp) # 生成可执行文件
target_link_libraries(eigenfaces_demo ${OpenCV_LIBS} ) # 设置target需要链接的库
mkdir build

cd build

cmake ..

cd ..

mingw32-make

③运行及结果展示:

./eigenfaces_demo.exe ./dir.txt ./Engenfaces_Result

OpenCV笔记--人脸识别算法Eigenfaces和Fisherfaces_第2张图片

特征图:(简单修改源程序生成的文件名,再按顺序进行拼接即可生成拼接图,拼接程序参考)

OpenCV笔记--人脸识别算法Eigenfaces和Fisherfaces_第3张图片

重建过程:

OpenCV笔记--人脸识别算法Eigenfaces和Fisherfaces_第4张图片

均值图:

4--Fisherfaces复现过程

①源码:

// 引用依赖
#include "opencv2/core.hpp"
#include "opencv2/face.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include 
#include 
#include 

// 使用相应的命名空间
using namespace cv;
using namespace cv::face;
using namespace std;

// 标准化函数
static Mat norm_0_255(InputArray _src) {
    Mat src = _src.getMat();
    Mat dst;
    switch(src.channels()) {
    case 1:
        cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
        break;
    case 3:
        cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
        break;
    default:
        src.copyTo(dst);
        break;
    }
    return dst;
}

// 读取csv文件函数
static void read_csv(const string& filename, vector& images, vector& labels, char separator = ';') {
    std::ifstream file(filename.c_str(), ifstream::in);
    if (!file) {
        string error_message = "No valid input file was given, please check the given filename.";
        CV_Error(Error::StsBadArg, error_message);
    }
    string line, path, classlabel;
    while (getline(file, line)) {
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, classlabel);
        if(!path.empty() && !classlabel.empty()) {
            images.push_back(imread(path, 0));
            labels.push_back(atoi(classlabel.c_str()));
        }
    }
}

int main(int argc, const char *argv[]) {

    //检查argc是否符合要求
    if (argc < 2) {
        cout << "usage: " << argv[0] << "   " << endl;
        exit(1);
    }
    string output_folder = ".";
    if (argc == 3) {
        output_folder = string(argv[2]);
    }

    // CSV文件的路径
    string fn_csv = string(argv[1]);

    // 初始化存储imgs和labels的向量
    vector images;
    vector labels;
    
    // 读取CSV文件
    try {
        read_csv(fn_csv, images, labels);
    } catch (const cv::Exception& e) {
        cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
        exit(1);
    }

    // 判断img数目是否符合要求
    if(images.size() <= 1) {
        string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
        CV_Error(Error::StsError, error_message);
    }

    // images的高度
    int height = images[0].rows;
    
    // 从训练集中选择一张图片作为测试集
    Mat testSample = images[images.size() - 1];
    int testLabel = labels[labels.size() - 1];
    images.pop_back();
    labels.pop_back();
    
    // 创建模型,使用LDA线性判别分析
    Ptr model = FisherFaceRecognizer::create();
    model->train(images, labels); // 训练模型

    int predictedLabel = model->predict(testSample); // 使用测试集测试模型
    
    // 打印准确率
    string result_message = format("Predicted class = %d / Actual class = %d.", predictedLabel, testLabel);
    cout << result_message << endl;
    // 获取模型的特征值
    Mat eigenvalues = model->getEigenValues();
    // 展示特征向量
    Mat W = model->getEigenVectors();
    // 从训练集中获取样本均值
    Mat mean = model->getMean();
    // 根据argc判断进行展示或保存操作
    if(argc == 2) {
        imshow("mean", norm_0_255(mean.reshape(1, images[0].rows)));
    } else {
        imwrite(format("%s/mean.png", output_folder.c_str()), norm_0_255(mean.reshape(1, images[0].rows)));
    }
    // 显示或保存特征脸
    for (int i = 0; i < min(16, W.cols); i++) {
        string msg = format("Eigenvalue #%d = %.5f", i, eigenvalues.at(i));
        cout << msg << endl;
        // 获取特征向量
        Mat ev = W.col(i).clone();
        // resize成原始大小,并归一化到0-255
        Mat grayscale = norm_0_255(ev.reshape(1, height));
        // 显示图像并应用Jet颜色图以获得更好的观感。
        Mat cgrayscale;
        applyColorMap(grayscale, cgrayscale, COLORMAP_BONE);
        // 根据argc判断进行展示或保存操作
        if(argc == 2) {
            imshow(format("fisherface_%d", i), cgrayscale);
        } else {
            imwrite(format("%s/fisherface_%d.png", output_folder.c_str(), i), norm_0_255(cgrayscale));
        }
    }
    // 在一些预定义的步骤中显示或保存图像重建的过程:
    for(int num_component = 0; num_component < min(16, W.cols); num_component++) {
        // 从模型中分割特征向量
        Mat ev = W.col(num_component);
        Mat projection = LDA::subspaceProject(ev, mean, images[0].reshape(1,1));
        Mat reconstruction = LDA::subspaceReconstruct(ev, mean, projection);
        // 归一化
        reconstruction = norm_0_255(reconstruction.reshape(1, images[0].rows));
        // 根据argc判断进行展示或保存操作
        if(argc == 2) {
            imshow(format("fisherface_reconstruction_%d", num_component), reconstruction);
        } else {
            imwrite(format("%s/fisherface_reconstruction_%d.png", output_folder.c_str(), num_component), reconstruction);
        }
    }
    // 如果没有写入输出文件夹,则等待键盘输入
    if(argc == 2) {
        waitKey(0);
    }
    return 0;
}

②编译过程:

CMakeLists.txt如下:

cmake_minimum_required(VERSION 3.24)  # 指定 cmake的 最小版本
project(test) # 设置项目名称

find_package(Opencv REQUIRED)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
#add_executable(eigenfaces_demo eigenfaces.cpp) # 生成可执行文件
#target_link_libraries(eigenfaces_demo ${OpenCV_LIBS} ) # 设置target需要链接的库
add_executable(fisherfaces_demo fisherfaces.cpp) # 生成可执行文件
target_link_libraries(fisherfaces_demo ${OpenCV_LIBS} ) # 设置target需要链接的库
mkdir build

cd build

cmake ..

cd ..

mingw32-make

③运行及结果展示:

./fisherfaces_demo.exe ./dir.txt ./Fisherfaces_Result

OpenCV笔记--人脸识别算法Eigenfaces和Fisherfaces_第5张图片

特征图:

OpenCV笔记--人脸识别算法Eigenfaces和Fisherfaces_第6张图片

重建过程:

OpenCV笔记--人脸识别算法Eigenfaces和Fisherfaces_第7张图片

均值图:

5--分析

未完待续!

你可能感兴趣的:(opencv,人工智能,计算机视觉)