examples/Qt/facerecgapp/facerecg.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 "facerecg.h"
00042 #include <QMessageBox>
00043 
00050 FaceRecgApp::FaceRecgApp(QObject* parent) : QObject(parent)
00051     { 
00052     //mTrainOrRecongise = true;
00053     m_Thumbnailer = new DummyFaceThumbnailer(this);
00054     m_FaceDB = new XQFaceDatabase(*m_Thumbnailer, QString(), this);
00055     m_Recognizer = new XQFaceRecognizer(*m_FaceDB, this);
00056     connect(m_Recognizer, SIGNAL(facesRecognized(const QString&, const QImage&, QList<XQFaceRegion>)),
00057             this, SLOT(recongnised(const QString&, const QImage&, QList<XQFaceRegion>)));
00058     }
00063 FaceRecgApp::~FaceRecgApp()
00064     {
00065     if(m_FaceDB)
00066         m_FaceDB->clearFaceReferences("FRAPPL1");
00067     }
00068 
00069 
00075 void FaceRecgApp::train(const QString sourceId)
00076 {
00077     mTrainOrRecongise = true;
00078     m_Recognizer->processFaces(sourceId);
00079     while(m_Recognizer->isProcessing())
00080     { // this is waiting as the current image proecessing is synchronous call 
00081     }
00082 }
00083 
00089 void FaceRecgApp::recongnised(const QString& sourceId, const QImage& image, QList<XQFaceRegion> faceRegions)
00090 {
00091     if(mTrainOrRecongise)
00092         { // In training part, to set face id if recognised.
00093         if(!faceRegions.isEmpty())
00094             {// Found face regions.
00095             foreach (XQFaceRegion region ,faceRegions) {
00096                 // define a face id and set it in recogniser db
00097                 region.setFaceId("FRAPPL1");
00098                 }
00099             }
00100         else
00101             {// Could not find any face regions.
00102             QMessageBox msgBox;
00103             msgBox.setInformativeText(tr("Proper Face could not be detected, try again"));
00104             qWarning("training: incorrect image!!!"); 
00105             msgBox.exec();
00106             }
00107         }
00108     else
00109         { // In recognise part, to match the regions face id with trained one.
00110         if(!faceRegions.isEmpty())
00111             {
00112             foreach (XQFaceRegion region ,faceRegions)
00113                 {
00114                 QStringList myString = region.faceIds();
00115                 if(!myString.isEmpty())
00116                     {
00117                     if((myString[0].compare("FRAPPL1") == 0))
00118                         {                    
00119                         if((region.isConfirmed() == true))
00120                             {// Face is recognised.                  
00121                             emit resultCompared(true);
00122                             break;
00123                             }
00124                         else
00125                             {// Could not recognise the face.
00126                             emit resultCompared(false);
00127                             }
00128                         }
00129                     else
00130                         {// Could not recognise the face and as it is not same as trained face id.
00131                         emit resultCompared(false);
00132                         }
00133                     }
00134                 else
00135                     {
00136                     // Could not recognise any valid face ids or
00137                     // the recogniser is not trained.                    
00138                     emit resultCompared(false);
00139                     }
00140                 } // End of for loop.
00141             }
00142         }
00143 }
00144 
00148 void FaceRecgApp::reset()
00149 {   // Clear all face references for the face id.
00150     if(m_FaceDB)
00151         m_FaceDB->clearFaceReferences("FRAPPL1");
00152 }
00153 
00157 void FaceRecgApp::test(const QString imageInput)
00158 {
00159     mTrainOrRecongise = false;
00160     m_Recognizer->processFaces(imageInput);
00161     while(m_Recognizer->isProcessing())
00162     { // This is waiting as the current proecessing is synchronous call.
00163     }
00164 }

Generated by  doxygen 1.6.2