00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <QDateTime>
00042 #include "mainwindow.h"
00043
00044 MainWindow::MainWindow()
00045 {
00046 infoLabel = new QLabel(tr("<i>Choose a menu option</i>"));
00047 infoLabel->setAlignment(Qt::AlignCenter);
00048
00049 imageLabel = new QLabel();
00050
00051 cameraViewfinder = new QCameraViewfinder(this);
00052 layout = new QVBoxLayout;
00053 layout->addWidget(infoLabel);
00054 QWidget *widget = new QWidget;
00055 setCentralWidget(widget);
00056 widget->setLayout(layout);
00057 createActions();
00058 createMenus();
00059 setWindowTitle(tr("Face Recogniser"));
00060
00061 faceRecgApp = new FaceRecgApp(this);
00062 setupCamera();
00063 timer = new QTimer(this);
00064 timer->setInterval(3000);
00065
00066 connect(timer, SIGNAL(timeout()), this, SLOT(timerTimeout()));
00067 connect(faceRecgApp,SIGNAL(resultCompared(bool)),this, SLOT(comparedResult(bool)));
00068 }
00073 void MainWindow::trainwImg()
00074 {
00075 camera->stop();
00076 layout->removeWidget(cameraViewfinder);
00077 layout->addWidget(infoLabel);
00078 layout->addWidget(imageLabel);
00079 imageLabel->clear();
00080 infoLabel->setText(tr("Invoked <b>Training</b>"));
00081 openImage();
00082 if(!fileName.isEmpty())
00083 faceRecgApp->train(fileName);
00084 }
00085
00090 void MainWindow::recogniseUser()
00091 {
00092 imageLabel->clear();
00093 imageLabel->adjustSize();
00094 layout->removeWidget(imageLabel);
00095 layout->removeWidget(infoLabel);
00096 infoLabel->setText(tr("Invoked <b>Recognise User </b>"));
00097 layout->addWidget(cameraViewfinder);
00098 layout->addWidget(infoLabel);
00099 camera->start();
00100 timer->start();
00101 setEnabled(false);
00102 }
00103
00104
00109 void MainWindow::timerTimeout()
00110 {
00111 QDateTime now = QDateTime::currentDateTime();
00112 QString name = now.toString("hhmmss");
00113 capImgName = "e:/capImgL2/";
00114 capImgName.append(name);
00115 capImgName.append(".jpg");
00116 camera->searchAndLock();
00117 imageCapture->capture(capImgName);
00118 }
00119
00123 void MainWindow::createActions()
00124 {
00125 trainAct = new QAction(tr("&Train"), this);
00126 trainAct->setStatusTip(tr("Traing with a image"));
00127 connect(trainAct, SIGNAL(triggered()), this, SLOT(trainwImg()));
00128
00129 recogniseAct= new QAction(tr("&Recognise"), this);
00130 recogniseAct->setStatusTip(tr("Recognise the User"));
00131 connect(recogniseAct, SIGNAL(triggered()), this, SLOT(recogniseUser()));
00132
00133 resetAct= new QAction(tr("Re&set"), this);
00134 resetAct->setStatusTip(tr("Reset the recogniser"));
00135 connect(resetAct, SIGNAL(triggered()), this, SLOT(reset()));
00136
00137 exitAct = new QAction(tr("E&xit"), this);
00138 exitAct->setShortcuts(QKeySequence::Quit);
00139 exitAct->setStatusTip(tr("Exit the application"));
00140 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
00141 }
00142
00146 void MainWindow::createMenus()
00147 {
00148 fileMenu = menuBar()->addMenu(tr("Face Rec App"));
00149 fileMenu->addAction(trainAct);
00150 fileMenu->addAction(recogniseAct);
00151 fileMenu->addAction(resetAct);
00152 fileMenu->addAction(exitAct);
00153 }
00154
00158 void MainWindow::openImage()
00159 {
00160 QString selectedFilter;
00161 QString filePath = QFileDialog::getOpenFileName(this,
00162 tr("Select the image"),
00163 QDir::homePath(),
00164 tr("Images (*.png *.bmp *.jpg)"),
00165 &selectedFilter,
00166 QFileDialog::DontUseNativeDialog );
00167 if (!filePath.isEmpty())
00168 {
00169 QImageReader reader(filePath);
00170 reader.setScaledSize(QSize(imageLabel->width(),imageLabel->height()));
00171 QImage image = reader.read();
00172 if (image.isNull()) {
00173 QMessageBox::information(this, tr("Image Viewer"),
00174 tr("Cannot load %1.").arg(filePath));
00175 qWarning("bad argument, Try again");
00176 return;
00177 }
00178 fileName = filePath;
00179 imageLabel->setPixmap(QPixmap::fromImage(image,Qt::AutoColor));
00180 infoLabel->setText("Training the recogniser...");
00181 }
00182 else
00183 {
00184 fileName = "";
00185 }
00186 }
00193 void MainWindow::setupCamera()
00194 {
00195
00196
00197 cameraDevice = QCamera::availableDevices()[1];
00198 camera = new QCamera(cameraDevice,this);
00199
00200 camera->setViewfinder(cameraViewfinder);
00201 imageCapture = new QCameraImageCapture(camera);
00202 camera->setCaptureMode(QCamera::CaptureStillImage);
00203
00204 connect(imageCapture,SIGNAL(imageSaved(int,QString)), this, SLOT(imageSaved(int,QString)));
00205 if (camera->state() == QCamera::ActiveState) {
00206 camera->stop();
00207 }
00208 }
00213 void MainWindow::imageSaved(int id, const QString imgName)
00214 {
00215 infoLabel->setText("Face recognition in progress...");
00216 faceRecgApp->test(imgName);
00217 }
00218
00223 void MainWindow::reset()
00224 {
00225 infoLabel->setText("Face recogniser is reset.");
00226 camera->stop();
00227 layout->removeWidget(cameraViewfinder);
00228 layout->addWidget(infoLabel);
00229 imageLabel->clear();
00230 layout->removeWidget(imageLabel);
00231 faceRecgApp->reset();
00232 }
00233
00238 void MainWindow::comparedResult(bool status)
00239 {
00240 QMessageBox msgBox;
00241 if(status)
00242 {
00243 camera->stop();
00244 timer->stop();
00245 faceRecgApp->reset();
00246 msgBox.setText("Face is recognised as same as the one trained");
00247 setEnabled(true);
00248 msgBox.setModal(true);
00249 msgBox.exec();
00250 close();
00251 }
00252 else
00253 {
00254 timer->stop();
00255 msgBox.setText("Face is different from the trained face. Try Again!!!");
00256 msgBox.setModal(true);
00257 msgBox.exec();
00258 timer->start();
00259 }
00260 }
00261
00262
00263