OpenCV中文网站

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 安装 配置
查看: 178|回复: 0

QT5.9+OpenCV3.10_contrib扩展包,GitHub上的人脸识别代码,但是找...

[复制链接]
发表于 2017-9-13 21:44:07 | 显示全部楼层 |阅读模式
配置了很多天的环境,在GitHub上下的人脸识别的代码,网上了解到opencv3以后face.hpp被放到contrib扩展包里,但是环境配置好之后还是有如下毛病:(之前opencv2.410也是有如下毛病)

如果在头文件加上:
[code=c]#include<opencv2/contrib/contrib.hpp>
#include<opencv2/face.hpp>
[/code]
就会报错:
error: opencv2/face.hpp: No such file ordirectory

如果把#include<opencv2/face.hpp>注释掉([code=c]#include<opencv2/contrib/contrib.hpp>依然保留),就会报错:

1. error: 'face' is not a namespace-name
using namespace face;
                 ^
2. error: expected namespace-name before ';' token
using namespace face;
                     ^
3. error: 'LBPHFaceRecognizer' was not declared in this scope
     Ptr<LBPHFaceRecognizer> model;
         ^

4. error: template argument 1 is invalid
     Ptr<LBPHFaceRecognizer> model;
                           ^
即没有找到face的名词空间和face相关的类


真的很想实现一个人脸识别的功能,希望有前辈指点一下,下面贴上源代码:(UI就不放了)
  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2015-11-11T08:11:51
  4. #
  5. #-------------------------------------------------

  6. QT       += core gui

  7. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

  8. TARGET = face_recognition
  9. TEMPLATE = app


  10. SOURCES += main.cpp\
  11.         mainwindow.cpp

  12. HEADERS  += mainwindow.h

  13. FORMS    += mainwindow.ui


  14. INCLUDEPATH+=C:\OpenCV_contrib\include\opencv\
  15.              C:\OpenCV_contrib\include\opencv2\
  16.              C:\OpenCV_contrib\include

  17. LIBS += -LC:/OpenCV_contrib/lib -lopencv_core2410.dll \
  18.             -lopencv_highgui2410.dll -lopencv_imgproc2410.dll -lopencv_features2d2410.dll \
  19.             -lopencv_calib3d2410.dll \
  20.            -lopencv_objdetect2410.dll  \
  21.            -lopencv_contrib2410.dll


  22. //mainwindow.h
  23. #ifndef MAINWINDOW_H
  24. #define MAINWINDOW_H

  25. #include<QMainWindow>
  26. #include<QCloseEvent>
  27. #include<opencv2/highgui/highgui.hpp>
  28. #include<opencv2/imgproc/imgproc.hpp>
  29. #include<opencv2/core/core.hpp>
  30. #include<opencv2/objdetect/objdetect.hpp>
  31. #include<opencv2/contrib/contrib.hpp>
  32. //#include<opencv2/face.hpp>
  33. #include<iostream>

  34. using namespace std;
  35. using namespace cv;
  36. using namespace face;

  37. namespace Ui {
  38. class MainWindow;
  39. }

  40. class MainWindow : public QMainWindow
  41. {
  42.     Q_OBJECT

  43. public:
  44.     explicit MainWindow(QWidget *parent = 0);
  45.     ~MainWindow();

  46. private slots:
  47.     void on_loadButton_clicked();
  48.     void on_testButton_clicked();
  49.     void on_regButton_clicked();
  50.     void closeEvent(QCloseEvent *e);

  51. private:
  52.     Ui::MainWindow *ui;
  53.     Ptr<LBPHFaceRecognizer> model;
  54.     QString fileName,saveXml,saveName,name[10];
  55. };

  56. #endif // MAINWINDOW_H


  57. //mainwindow.cpp
  58. #include "mainwindow.h"
  59. #include "ui_mainwindow.h"
  60. #include<QDebug>
  61. #include<QFileDialog>
  62. #include<QPixmap>
  63. #include<QFile>
  64. #include<QTextStream>

  65. //正面,上,下,左,右5张.阉值85.00

  66. MainWindow::MainWindow(QWidget *parent):
  67.     QMainWindow(parent),
  68.     ui(new Ui::MainWindow)
  69. {
  70.     ui->setupUi(this);

  71.     saveName = "Names.txt";
  72.     saveXml = "att_model.xml";

  73.     model = createLBPHFaceRecognizer();

  74.     if(QFile::exists(saveXml)&&QFile::exists(saveName))
  75.     {
  76.         model->load(saveXml.toStdString());
  77.         QFile file(saveName);
  78.         if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  79.             return;
  80.         QTextStream in(&file);
  81.         QString lineText;
  82.         while(!in.atEnd())
  83.         {
  84.             lineText = in.readLine();
  85.             QString i = lineText.split(":").first();
  86.             name[i.toInt()] = lineText.split(":").last();
  87.         }
  88.     }
  89. //    for(int i=1;i<11;i++)
  90. //        for(int j=1;j<10;j++)
  91. //        {
  92. //            QString file = "att_faces/s%1/%2.pgm";
  93. //            images.push_back(imread(file.arg(i).arg(j).toStdString(), CV_LOAD_IMAGE_GRAYSCALE));
  94. //            labels.push_back(i);
  95. //        }

  96. //    model = createLBPHFaceRecognizer();
  97. //    //model->train(images, labels);
  98. //    //model->save("att_model.xml");

  99. //    model->load("att_model.xml");

  100. }

  101. MainWindow::~MainWindow()
  102. {
  103.     delete ui;
  104. }

  105. void MainWindow::closeEvent(QCloseEvent *e)
  106. {
  107.     model->save(saveXml.toStdString());
  108.     QFile file(saveName);
  109.     if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
  110.         return;
  111.     QTextStream out(&file);
  112.     for(int i=0;i<10;i++)
  113.     {
  114.         if(name[i].isEmpty())
  115.             continue;
  116.         out<<i<<":"<<name[i]<<"\n";
  117.     }
  118.     e->accept();
  119. }

  120. void MainWindow::on_loadButton_clicked()
  121. {
  122.     fileName = QFileDialog::getOpenFileName(this,tr("选择图片"),tr("."));
  123.     if(fileName.isEmpty())
  124.         return;
  125.     ui->showLabel->setPixmap(QPixmap(fileName));
  126.     ui->textBrowser->append(tr("打开图片%1").arg(fileName.split("/").last()));
  127. }

  128. void MainWindow::on_testButton_clicked()
  129. {
  130.     if(fileName.isEmpty()||ui->nameEdit->text().isEmpty())
  131.         return;
  132.     vector<Mat> images;
  133.     vector<int> labels;
  134.     images.push_back(imread(fileName.toStdString(),CV_LOAD_IMAGE_GRAYSCALE));
  135.     labels.push_back(ui->labelBox->value());
  136.     name[ui->labelBox->value()] = ui->nameEdit->text();
  137.     ui->textBrowser->append(tr("准备训练: 姓名:%1 标签:%2 ...").arg(ui->nameEdit->text()).arg(ui->labelBox->value()));
  138.     model->update(images,labels);
  139.     ui->textBrowser->append(tr("训练完成"));
  140. }

  141. void MainWindow::on_regButton_clicked()
  142. {
  143.     ui->nameLabel->clear();
  144.     if(fileName.isEmpty())
  145.         return;
  146.     Mat image = imread(fileName.toStdString(), CV_LOAD_IMAGE_GRAYSCALE);
  147.     model->setThreshold(ui->doubleSpinBox->value());
  148.     ui->textBrowser->append(tr("准备识别Threshold:%1 ...").arg(ui->doubleSpinBox->value()));
  149.     int result = model->predict(image);
  150.     ui->textBrowser->append(tr("识别完成"));
  151.     if(result < 0)
  152.         ui->nameLabel->setText(tr("无法识别此人"));
  153.     else
  154.         ui->nameLabel->setText(tr("%1").arg(name[result]));
  155. }

  156. //main.cpp:
  157. #include "mainwindow.h"
  158. #include <QApplication>

  159. int main(int argc, char *argv[])
  160. {
  161.     QApplication a(argc, argv);
  162.     MainWindow w;
  163.     w.show();

  164.     return a.exec();
  165. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|感谢博安盈科技资助本网站|OpenCV中文网站 ( 粤ICP备13079875号-2   

GMT+8, 2017-9-22 10:39 , Processed in 0.101286 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表