OpenCV人脸识别二(根据ORL人脸库训练模型)

OpenCV版本3.4.1

人脸识别算法采用:LBPHFaceRecognizer

.pro文件代码

QT += core
QT -= gui

CONFIG += c++11

TARGET = opencvTest
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp \
    fisherdemo.cpp \
    lbphdemo.cpp \
    lbphdemo2.cpp \
    lbphdemo3.cpp

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
INCLUDEPATH += G:/OpenCV/opencv/sources-build/install/include/opencv \
               G:/OpenCV/opencv/sources-build/install/include/opencv2 \
               G:/OpenCV/opencv/sources-build/install/include
LIBS +=-LG:/OpenCV/opencv/sources-build/install/x86/mingw/lib/ \
       -llibopencv_core341 \
       -llibopencv_highgui341 \
       -llibopencv_imgcodecs341 \
       -llibopencv_imgproc341 \
       -llibopencv_face341 \
       -llibopencv_features2d341 \
       -llibopencv_calib3d341

main.cpp代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace cv;
using namespace face;
using namespace std;

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(CV_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()));
        }
    }
}
/**LBPHFaceRecognizer训练模型
 * @brief main
 * @param argc
 * @param argv
 * @return
 */
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // Get the path to your CSV.
    string fn_csv="G:/opencvImage/at.txt";
    // These vectors hold the images and corresponding labels.
    vector images;
    vector labels;
    // Read in the data. This can fail if no valid
    // input filename is given.
    try{
        read_csv(fn_csv,images,labels);
    }
    catch(cv::Exception& e){
        cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
        // nothing more we can do
        exit(1);
    }
    // Quit if there are not enough images for this demo.
    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(CV_StsError,error_message);
    }
    Ptr model=LBPHFaceRecognizer::create();
    model->train(images,labels);
    model->save("G:/opencvImage/lbph_face.xml");
    cout << "train done." << endl;
    return a.exec();
}

你可能感兴趣的:(OpenCV,OpenCV3.4.1)