1. 程式人生 > 其它 >OpenCV之使用LBPHFaceRecognizer來實現人臉識別

OpenCV之使用LBPHFaceRecognizer來實現人臉識別

一、概述

  案例:使用LBPHFaceRecognizer來實現人臉識別

  主要程式碼展示

 //例項化LBPH人臉識別演算法例項
    Ptr<LBPHFaceRecognizer> model = LBPHFaceRecognizer::create();
    model->train(images,labels);//訓練

    //預測
    int predictLabel = model->predict(testMat);

  實現步驟

    1.準備訓練資料

    2.準備測試資料

    3.例項化LBPFaceRecognizer model

    4.開始訓練model->trans

    5.開始預測model->predict

    6.如果測試標籤值與訓練標籤值一致則說明人臉識別成功,否則失敗

二、程式碼示例

 ifstream file(filePath,ifstream::in);
    if(!file){
        qDebug()<<"載入人臉訓練資料失敗";
        return;
    }
    //準備訓練資料
    string line ,path,classLabel;
    vector<Mat> images;
    vector<int
> labels; while(getline(file,line)){ stringstream liness(line); getline(liness,path,' '); getline(liness,classLabel); images.push_back(imread(path,0)); labels.push_back(atoi(classLabel.c_str())); } //準備測試資料 Mat testMat = images[images.size()-1
]; int testLabel = labels[labels.size()-1]; images.pop_back(); labels.pop_back(); //例項化LBPH人臉識別演算法例項 Ptr<LBPHFaceRecognizer> model = LBPHFaceRecognizer::create(); model->train(images,labels);//訓練 //預測 int predictLabel = model->predict(testMat); //如果測試標籤值和預測標籤值抑制則說明人臉識別成功,否則不成功 cout <<"測試標籤值:"<<testLabel<<",預測標籤值:"<<predictLabel<<endl;

三、演示影象