2 as78963214555 as78963214555 于 2017.09.13 21:07 提问

QT5.9+OpenCV3.10_contrib,GitHub上人脸识别找不到face相关类和hpp

调配了很多天的环境,网上说OpenCV3以后face.hpp在contrib扩展包里面,我都配置好之后还是有如下毛病:(之前用的OpenCV2.410也是如下毛病)
#如果在头文件加上

#include<opencv2/contrib/contrib.hpp>
 #include<opencv2/face.hpp>

就会报错

 error: opencv2/face.hpp: No such file or directory

如果把这个注释掉(依然保留#include)

就会报错
图片说明

即 face名词空间还是没找到,和face有关的类还是没找到
百度了很深的程度也没有解决,希望有前辈指点一下,很想完成一个人脸识别的功能

下面把代码都贴出来(UI就不放了)

1、pro文件:

 #-------------------------------------------------
#
# Project created by QtCreator 2015-11-11T08:11:51
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = face_recognition
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui


INCLUDEPATH+=C:\OpenCV_contrib\include\opencv\
             C:\OpenCV_contrib\include\opencv2\
             C:\OpenCV_contrib\include

LIBS += -LC:/OpenCV_contrib/lib -lopencv_core2410.dll \
            -lopencv_highgui2410.dll -lopencv_imgproc2410.dll -lopencv_features2d2410.dll \
            -lopencv_calib3d2410.dll \
           -lopencv_objdetect2410.dll  \
           -lopencv_contrib2410.dll

MainWindow.h :

 #ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include<QMainWindow>
#include<QCloseEvent>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/objdetect/objdetect.hpp>
#include<opencv2/contrib/contrib.hpp>
//#include<opencv2/face.hpp>
#include<iostream>

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

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_loadButton_clicked();
    void on_testButton_clicked();
    void on_regButton_clicked();
    void closeEvent(QCloseEvent *e);

private:
    Ui::MainWindow *ui;
    Ptr<LBPHFaceRecognizer> model;
    QString fileName,saveXml,saveName,name[10];
};

#endif // MAINWINDOW_H

cpp文件:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<QFileDialog>
#include<QPixmap>
#include<QFile>
#include<QTextStream>

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

MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

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

    model = createLBPHFaceRecognizer();

    if(QFile::exists(saveXml)&&QFile::exists(saveName))
    {
        model->load(saveXml.toStdString());
        QFile file(saveName);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return;
        QTextStream in(&file);
        QString lineText;
        while(!in.atEnd())
        {
            lineText = in.readLine();
            QString i = lineText.split(":").first();
            name[i.toInt()] = lineText.split(":").last();
        }
    }
//    for(int i=1;i<11;i++)
//        for(int j=1;j<10;j++)
//        {
//            QString file = "att_faces/s%1/%2.pgm";
//            images.push_back(imread(file.arg(i).arg(j).toStdString(), CV_LOAD_IMAGE_GRAYSCALE));
//            labels.push_back(i);
//        }

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

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

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *e)
{
    model->save(saveXml.toStdString());
    QFile file(saveName);
    if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
        return;
    QTextStream out(&file);
    for(int i=0;i<10;i++)
    {
        if(name[i].isEmpty())
            continue;
        out<<i<<":"<<name[i]<<"\n";
    }
    e->accept();
}

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

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

void MainWindow::on_regButton_clicked()
{
    ui->nameLabel->clear();
    if(fileName.isEmpty())
        return;
    Mat image = imread(fileName.toStdString(), CV_LOAD_IMAGE_GRAYSCALE);
    model->setThreshold(ui->doubleSpinBox->value());
    ui->textBrowser->append(tr("准备识别Threshold:%1 ...").arg(ui->doubleSpinBox->value()));
    int result = model->predict(image);
    ui->textBrowser->append(tr("识别完成"));
    if(result < 0)
        ui->nameLabel->setText(tr("无法识别此人"));
    else
        ui->nameLabel->setText(tr("%1").arg(name[result]));
}


main.cpp:
#include "mainwindow.h"
#include

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();

}

1个回答

ILOREO
ILOREO   2017.10.17 09:53

请问你解决了吗?我也遇到了类似的问题

Csdn user default icon
上传中...
上传图片
插入图片