"haar"特征主要用于人脸检测
"hog"特征主要用于行人检测
"lbp"特征主要用于人脸识别
eye_Classifier.detectMultiScale(image_gray, eyeRect, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));//检测
face_cascade.detectMultiScale(image_gray, faceRect, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));//检测
#-------------------------------------------------
#
# 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
//mainwindow.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 <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}