examples/Qt/facerecgapp/mainwindow.cpp

00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the examples of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:BSD$
00010 ** You may use this file under the terms of the BSD license as follows:
00011 **
00012 ** "Redistribution and use in source and binary forms, with or without
00013 ** modification, are permitted provided that the following conditions are
00014 ** met:
00015 **   * Redistributions of source code must retain the above copyright
00016 **     notice, this list of conditions and the following disclaimer.
00017 **   * Redistributions in binary form must reproduce the above copyright
00018 **     notice, this list of conditions and the following disclaimer in
00019 **     the documentation and/or other materials provided with the
00020 **     distribution.
00021 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
00022 **     the names of its contributors may be used to endorse or promote
00023 **     products derived from this software without specific prior written
00024 **     permission.
00025 **
00026 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00030 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00037 ** $QT_END_LICENSE$
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     // Initialize the face recogniser, sets up the camera and timer.
00061     faceRecgApp = new FaceRecgApp(this);
00062     setupCamera();
00063     timer = new QTimer(this);
00064     timer->setInterval(3000);
00065     // Defines the connection for recogniser and the timer.
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();//starting the camera
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     //Selecting the secondary camera by passing argument value as 1.
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(); // reset the reecogniser.
00246         msgBox.setText("Face is recognised as same as the one trained");
00247         setEnabled(true); // enable the mainwindow.
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 

Generated by  doxygen 1.6.2