当前位置:   article > 正文

OpenCV使用EigenFaceRecognizer来实现人脸识别

eigenfacerecognizer

1、概述

  案例:使用EigenFaceRecognizer来实现人脸识别算法

  实现步骤:

    1.准备人脸数据(人脸和人脸对应的标签),ps:预留一个或几个样本用来测试

    2.将样本数据和样本对应的标签数据从文件中读取出来并分别存入集合

    3.实例化EigenFaceRecognizer

    4.将准备好的人脸集合和标签集合放入EigenFaceRecognizer.train函数中进行训练

    5.训练好数据后执行predict方法进行预测

    6.假如预留样本的标签值与执行predict预测后的标签值是一致的就说明我们预测成功了。

  ps:使用这个算法来实现人脸识别时样本图像和实际的图像大小必须要要一致,否则算法会出现不工作的情况。

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

2、代码示例

  1. Face_Eigen_Face_Recognizer::Face_Eigen_Face_Recognizer(QWidget *parent)
  2. : MyGraphicsView{parent}
  3. {
  4. this->setWindowTitle("特征脸识别器");
  5. QPushButton * btn = new QPushButton(this);
  6. btn->setText("读取数据");
  7. connect(btn,&QPushButton::clicked,[=](){
  8. QString srcDirPath = QFileDialog::getExistingDirectory(
  9. this, "choose src Directory",
  10. "/Users/yangwei/Documents/tony/opencv/orl_faces");
  11. if (srcDirPath.isEmpty())
  12. {
  13. return;
  14. }
  15. else
  16. {
  17. string filename = string("/Users/yangwei/Documents/tony/opencv/orl_faces/targetData.txt");
  18. out.open(filename,ios::out);
  19. qDebug() << "srcDirPath=" << srcDirPath;
  20. srcDirPath += "/";
  21. prepareImageData(srcDirPath.toStdString().c_str(),"");
  22. out.close();
  23. }
  24. });
  25. QPushButton *btnShow = new QPushButton(this);
  26. btnShow->move(0,btn->y()+btn->height()+5);
  27. btnShow->setText("开始检测特征脸");
  28. connect(btnShow,&QPushButton::clicked,[=](){
  29. showEgenFaceRecoginzer("");
  30. });
  31. }
  32. void Face_Eigen_Face_Recognizer::dropEvent(QDropEvent *event){
  33. path = event->mimeData()->urls().at(0).toLocalFile();
  34. showEgenFaceRecoginzer(path.toStdString().c_str());
  35. }
  36. void Face_Eigen_Face_Recognizer::showEgenFaceRecoginzer(const char * filePath){
  37. string filename = string("/Users/yangwei/Documents/tony/opencv/orl_faces/targetData.txt");
  38. ifstream file(filename,ifstream::in);
  39. string line,path,classLabel;//行、路径、标签
  40. vector<Mat> images;
  41. vector<int> labels;
  42. while(getline(file,line)){
  43. stringstream liness(line);
  44. getline(liness,path,' ');
  45. getline(liness,classLabel);
  46. // if (!path.empty() && !labels.empty()) {
  47. cout << "path :"<< classLabel.c_str()<<endl;;
  48. images.push_back(imread(path, 0));
  49. labels.push_back(atoi(classLabel.c_str()));
  50. // }
  51. }
  52. file.close();
  53. if (images.size() < 1 || labels.size() < 1) {
  54. qDebug()<<"invalid image path...\n";
  55. return;
  56. }
  57. int width = images[0].cols;
  58. int height = images[0].rows;
  59. cout << "width:"<<width<<"|"<<"height:"<<height<<endl;
  60. //准备测试数据和测试label
  61. Mat testMatSample = images[images.size()-1];
  62. int testLabel = labels[labels.size()-1];
  63. imshow("testMatSample",testMatSample);
  64. images.pop_back();
  65. labels.pop_back();
  66. //接下来就是最重要的步骤
  67. //1.训练
  68. Ptr<BasicFaceRecognizer> model = EigenFaceRecognizer::create();
  69. model->train(images,labels);
  70. //2.预测
  71. int predictedLabel = model->predict(testMatSample);
  72. //此处如果样本和预测结果是一致的就说明此次识别是算法是成功的
  73. cout << "testLabel:"<<testLabel<<endl;
  74. cout <<"predictedLabel:"<<predictedLabel<<endl;
  75. //从训练结果中获取均值、特征向量、特征值矩阵
  76. Mat eigenvalues = model->getEigenValues();
  77. Mat eigenvectors = model->getEigenVectors();
  78. Mat mean = model->getMean();
  79. //得到均值脸
  80. Mat meanFace = mean.reshape(1,height);
  81. Mat dst;
  82. //归一化0~255并输出
  83. if(meanFace.channels()==1){//单通道图像
  84. normalize(meanFace,dst,0,255,NORM_MINMAX,CV_8UC1);
  85. }else{//多通道图像
  86. normalize(meanFace,dst,0,255,NORM_MINMAX,CV_8UC3);
  87. }
  88. imshow("dist",dst);
  89. // //输出特征脸
  90. // for (int i = 0; i < min(16, eigenvectors.cols); i++) {
  91. // Mat ev = eigenvectors.col(i).clone();
  92. // Mat grayscale;
  93. // Mat eigenFace = ev.reshape(1, height);
  94. // if (eigenFace.channels() == 1) {
  95. // normalize(eigenFace, grayscale, 0, 255, NORM_MINMAX, CV_8UC1);
  96. // }
  97. // else if (eigenFace.channels() == 3) {
  98. // normalize(eigenFace, grayscale, 0, 255, NORM_MINMAX, CV_8UC3);
  99. // }
  100. // Mat colorface;
  101. // applyColorMap(grayscale, colorface, COLORMAP_BONE);
  102. // char* winTitle = new char[128];
  103. // sprintf(winTitle, "eigenface_%d", i);
  104. // imshow(winTitle, colorface);
  105. // }
  106. // for (int num = 0; num < min(eigenvectors.cols, 16); num++) {
  107. // Mat evs = eigenvectors.col(num);
  108. // Mat projection = LDA::subspaceProject(evs, mean, images[0].reshape(1, 1));
  109. // Mat reconstruction = LDA::subspaceReconstruct(evs, mean, projection);
  110. // Mat result = reconstruction.reshape(1, height);
  111. // if (result.channels() == 1) {
  112. // normalize(result, reconstruction, 0, 255, NORM_MINMAX, CV_8UC1);
  113. // }
  114. // else if (result.channels() == 3) {
  115. // normalize(result, reconstruction, 0, 255, NORM_MINMAX, CV_8UC3);
  116. // }
  117. // char* winTitle = new char[128];
  118. // sprintf(winTitle, "recon_face_%d", num);
  119. // imshow(winTitle, reconstruction);
  120. // }
  121. }

3、演示图像

  预测结果:

 本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/278549
推荐阅读
相关标签
  

闽ICP备14008679号