随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

OpenCV之使用FisherFaceRecognizer来实现人脸识别

一、概述

  案例:使用FisherFaceRecognizer来实现人脸识别

  主要代码

Ptr<BasicFaceRecognizer> model = FisherFaceRecognizer::create();
    model->train(images,labels);//训练

    //预测
    int predictLabel = model->predict(testMat);

  实现步骤

    1.准备训练用的人脸数据及标签数据

    2.准备测试用的测试数据

    3.实例化BasicFaceRecognizer(人脸识别实例)-->model

    4.利用model->trans进行数据的训练

    5.利用model->predict进行数据的预测

    6.比较测试图片对应的标签和预测图片结果对应的标签是否一致,如果一直说明人脸识别成功,否则人脸识别失败。

二、代码示例

复制代码
 ifstream file(filePath,ifstream::in);
    if(!file){
        qDebug()<<"file count not found";
        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()));
    }
    cout <<"准备数据完成:"<<images.size()<<endl;

    //检测准备的训练数据是否正常
    if(images.size()<1||labels.size()<1){
        qDebug()<<"准备的数据异常";
        return;
    }

    //测试查样本数据的宽高
    int width = images[0].cols;
    int height = images[0].rows;
    cout << "width:"<<width<<",height:"<<height<<endl;

    //准备测试数据
    Mat testMat = images[images.size()-1];
    int testLabel = labels[labels.size()-1];
    images.pop_back();
    labels.pop_back();

    //创建Fisher人脸识别实例,并开始训练数据
    Ptr<BasicFaceRecognizer> model = FisherFaceRecognizer::create();
    model->train(images,labels);//训练

    //预测
    int predictLabel = model->predict(testMat);
    //如果预测的标签值和测试的标签值一致就说明人脸识别是成功的。
    cout <<"testLabel:"<<testLabel <<",predictLabel:"<<predictLabel<<endl;
复制代码

 

三、演示图像

  

 

posted on   飘杨......  阅读(491)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示