/* * GKRPlugin * * Author: Andrew Salamon * * Copyright (c) 2008 Machine Perception Laboratory * University of California San Diego. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include "GKRPlugin.h" #include #include "PluginController.h" #include "PluginBase.h" #include "SVMPlugin.h" #include "AfterFacePlugin.h" #include "FacePlugin.h" #include "FeaturePlugin.h" #include "CERT_Config.h" #include "CERT_Arguments.h" #include "mp_CERT.h" namespace CERT { GKRPlugin::GKRPlugin( ConfigItem *_config ) : AfterFacePlugin(_config), training(false), temporal(false), GKR(false), logistic(false), auid("edu.ucsd.mplab.plugins.CERT4_4_Weights") { training = Arguments::GKRTraining(); modelPath = Arguments::GKRModelPath(); std::vector< std::string > item; item = config->getValueForKey( "gkr" ); if( 1 == item.size() ) { if( item[0] == "true" ) GKR = true; } if( 0 == modelPath.length() ) { if( isEnabled() && GKR ) { std::cerr << "GKR processing was enabled in the CKF Plugin, but no Model Path was given on the command line. GKR processing will be disabled." << std::endl; GKR = false; } } item = config->getValueForKey( "temporal" ); if( 1 == item.size() ) { if( item[0] == "true" ) temporal = true; } item = config->getValueForKey( "logistic" ); if( 1 == item.size() ) { if( item[0] == "true" ) logistic = true; } if( isEnabled() ) { models.setFlags( training, GKR, logistic, temporal ); if( !training && GKR ) models.loadModels( modelPath ); if( temporal && !setCKFValuesFromConfig() ) { std::cerr << "The CKF Plugin was unable to parse initial values for the CKF (temporal) model. Default values will be used." << std::endl; } } item = config->getValueForKey( "auid" ); if( 1 == item.size() ) { auid = item[0]; } } GKRPlugin::~GKRPlugin() { if( isEnabled() ) outputResults(); } PluginBase * GKRPlugin::copy() { return new GKRPlugin(*this); } std::vector GKRPlugin::getLabels() const { if( 0 == labels.size() ) { const_cast(this)->loadLabels(); } return labels; } void GKRPlugin::loadLabels() { if( 0 == labels.size() ) { PluginController *pc = PluginController::getPluginController(); SVMPlugin *auPlugin = dynamic_cast( pc->pluginWithID( auid ) ); if( auPlugin ) { static const std::string CKF("CKF: "); const std::vector extLabels( auPlugin->getLabels() ); for( unsigned int i=0; i < extLabels.size(); ++i ) { nans += "\tNaN"; labels.push_back( CKF + extLabels[i] ); } models.setLabels( extLabels ); } } } std::string GKRPlugin::invalidResultsString() const { return nans; } void GKRPlugin::processImageUsingCert( RImage &image, MP_CERT &cert ) { PluginController *pc = &cert.pluginController(); results.clear(); // These plugin id's should be in the config file PluginBase *posePlugin = pc->pluginWithID( "edu.ucsd.mplab.plugins.PoseDetector" ); SVMPlugin *auPlugin = dynamic_cast( pc->pluginWithID( auid ) ); if( auPlugin && (0 == labels.size()) ) { static const std::string CKF("CKF: "); const std::vector extLabels( auPlugin->getLabels() ); for( unsigned int i=0; i < extLabels.size(); ++i ) { nans += "\tNaN"; labels.push_back( CKF + extLabels[i] ); } models.setLabels( extLabels ); } if( posePlugin && auPlugin ) { PluginResultsType poseRes = posePlugin->getResults(); PluginResultsType auRes = auPlugin->getResults(); results = models.addFrame( poseRes, auRes, cert.didFindFace() ); } } void GKRPlugin::processTask( Task &task ) { results.clear(); // if( task.didFindFace ) { PluginResultsType poseRes = task.resultsForPlugin( "edu.ucsd.mplab.plugins.PoseDetector" ); PluginResultsType auRes = task.resultsForPlugin( auid); results = models.addFrame( poseRes, auRes, task.didFindFace ); } task.addResultsForPlugin( id(), results ); } bool GKRPlugin::areResultsValid() { return (results.size() > 0); } double GKRPlugin::getResultsDouble() { return results[0]; } void GKRPlugin::outputResults() { if( !training || !GKR ) return; models.train(); models.saveModels( modelPath ); mout() << "GKR:\t" << (training?"Trained model":"Calibrated data") << std::endl; } std::ostream & GKRPlugin::mout() { return CERT::Arguments::outputStream(); } bool GKRPlugin::floatFromString( const std::string &str, float &fl ) { std::stringstream strstr( str ); float _fl; if( !(strstr >> _fl) ) return false; fl = _fl; return true; } /* The values needed to initialize the temporal models (CKF) look like this in the config file: (AU 1) Inner Brow Raise 1.0 1.0 25.0 -2.0 */ bool GKRPlugin::setCKFValuesFromConfig() { static const int stride(7); std::vector< std::string > values = config->getValueForKey( "ckf_init" ); if( (values.size() % stride) != 0 ) return false; GKR::Models::CKFmap valmap; for( unsigned int i = 0; i < values.size(); i += stride ) { std::string AUID( values[i] ); std::vector valvec; for( int v = 1; v < stride; ++v ) { float tmp; if( !floatFromString( values[i+v], tmp ) ) return false; valvec.push_back( tmp ); } valmap[ AUID ] = valvec; } bool res = models.setCKFValues( valmap ); models.saveCKFValues( "/tmp/CKFmap.xml" ); return res; } } // end namespace CERT