/* * mp_CERT * * Author: Andrew Salamon * Date: Wed May 24, 2006 * * Copyright (c) 2006 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 "mp_CERT.h" #include "mp_rotateFace.h" #include "visualobject.h" #include "mp_SVMWeights.h" static const std::string CERT1Weights( "SVMWeights" ); static const std::string EmotionWeights( "EmotionWeights" ); /** MP_CERT constructor. * Sets the directory for finding SVM weights files (defaults to "SVMWeights" in the current working directory). * Sets some parameters in both the eyefinder and face rotator. */ MP_CERT::MP_CERT( const std::string &svmWeightsDir ) : patch( 96, 96 ), rotator( 96 ), useFeatures(false), gabor(NULL), foundFace(false) { auLabels.push_back( 1 ); auLabels.push_back( 2 ); auLabels.push_back( 4 ); auLabels.push_back( 5 ); auLabels.push_back( 10 ); auLabels.push_back( 12 ); auLabels.push_back( 14 ); auLabels.push_back( 20 ); mp_SVMWeights *weights = mp_SVMWeights::getSVMWeights(); weights->setDirectory( svmWeightsDir ); weights->loadAllWeights(); // Preload all SVM weight data if( weights->getLoadError() ) std::cerr << "Error loading SVM Weights file(s)." << std::endl; mp_SVMWeights *emotionWeights = mp_SVMWeights::getSVMWeights( "EmotionWeights" ); emotionWeights->setDirectory( svmWeightsDir ); emotionWeights->loadAllWeights(); // Preload all emotion detector SVM weight data if( emotionWeights->getLoadError() ) std::cerr << "Error loading SVM Weights files for the emotion detectors." << std::endl; eyefinder.setUseSequence( false ); eyefinder.setCascadeThresholds( 1.0, 1.0 ); #ifdef USE_FEATURES featureDetector.setUseSequence( false ); featureDetector.loadFeatureFromFiles( "./featuredetector/frozenData/left_eye_02_20_07.fdtxml", "./featuredetector/frozenData/left_eye_02_20_07.gpriorxml" ); featureDetector.loadFeatureFromFiles( "./featuredetector/frozenData/right_eye_02_20_07.fdtxml", "./featuredetector/frozenData/right_eye_02_20_07.gpriorxml" ); #endif rotator.setEyeLenRate( 0.48 ); gabor = new GABOR_CLASS( patch, patch.width, patch.height ); setAUCategories( true ); clear(); } MP_CERT::~MP_CERT() { if( gabor ) delete gabor; } void MP_CERT::clear() { mptSquare empty = { 0.0, 0.0, 0.0, 0.0 }; face = empty; leftEye = empty; rightEye = empty; foundFace = false; gabor->clearYhat(); } std::vector MP_CERT::eyes() { // left.x left.y right.x right.y std::vector eyeVec( 4, 0.0 ); eyeVec[0] = leftEye.x; eyeVec[1] = leftEye.y; eyeVec[2] = rightEye.x; eyeVec[3] = rightEye.y; return eyeVec; } /** Main entry point. * -# Finds one face. Will use the largest found face if there are more than one. * -# Aligns the eyes (rotates chosen face). * -# Calculates and returns AU values. * /param[in] rimage */ std::vector MP_CERT::calcAUs( RImage &rimage ) { EyeFaceList faces; clear(); #ifdef USE_FEATURES if( useFeatures ) featureDetector.findEyes( rimage, faces ); else #endif eyefinder.findEyesSpeed( rimage, faces ); float lx=0; float ly=0; float rx=0; float ry=0; // eye positions EyeFaceList::iterator faceIter = faces.begin(); EyeFaceList::iterator last_face = faces.end(); double largestArea = 0.0; #ifdef DEBUG std::cout << "Found " << faces.size() << " faces" << std::endl; #endif for( ; faceIter != last_face; ++faceIter ) { double faceArea = faceIter->xSize * faceIter->ySize; if( (faceIter->x > 0.0) && (faceIter->y > 0.0) && (faceArea > largestArea) ) { face.x = faceIter->x; face.y = faceIter->y; face.width = faceIter->xSize; face.height = faceIter->ySize; lx = faceIter->both_eyes.xLeft; ly = faceIter->both_eyes.yLeft; rx = faceIter->both_eyes.xRight; ry = faceIter->both_eyes.yRight; largestArea = faceArea; foundFace = true; } #ifdef DEBUG std::cout << "Face(" << faceIter - faces.begin() << "): {" << face.x << "," << face.y << "} " << face.width << "x" << face.height << std::endl; std::cout << " Eyes: left {" << lx << "," << ly << "} " << "right {" << rx << "," << ry << "}" << std::endl; #endif } // Criteria are eye x-positions are nonzero and at least 50 pix apart. // That criteria is really only for data sets, but we might want it to be a preference. if( (lx > 0) && (rx > 0) && ( (lx-rx) > 0) ) { leftEye.x= lx; leftEye.y = ly; rightEye.x = rx; rightEye.y = ry; if( rotator.rotateImageIntoPatch( &rimage, (int)leftEye.x, (int)rightEye.x, (int)leftEye.y, (int)rightEye.y, patch ) ) { return calcAUsFromFace( patch ); } } return std::vector(); } std::map MP_CERT::getAUMap() { if( gabor ) { std::map auMap; std::vector res = gabor->resultsForCategory( CERT1Weights ); assert( res.size() == auLabels.size() ); for( unsigned int ind = 0; ind < res.size(); ++ind ) auMap[ auLabels[ind] ] = res[ind]; return auMap; } return std::map(); } std::map< std::string, mp_AUResults > MP_CERT::getFrequencies() { return gabor->getFrequencies(); } bool MP_CERT::didFindFace() { return foundFace; } /** Secondary entry point. * Assumes that a face has already been found and the image has been cropped and rotated. * Calculates and returns AU values. * /param[in] rimage with a cropped and aligned face. */ std::vector MP_CERT::calcAUsFromFace( RImage &face ) { if( !gabor ) gabor = new GABOR_CLASS( face, face.width, face.height ); else gabor->setPixels( face ); gabor->CERT_Gabor(); return gabor->resultsForCategory( CERT1Weights ); } void MP_CERT::setAUCategories( bool withEmotions ) { std::vector< std::string > categories; categories.push_back(CERT1Weights); if( withEmotions ) categories.push_back(EmotionWeights); gabor->setCategories( categories ); } void MP_CERT::setDoEmotions( bool _doEmotions ) { if( !gabor ) std::cerr << "Error: Gabor not constructed properly (2)." << std::endl; setAUCategories( _doEmotions ); } std::vector MP_CERT::getEmotions() { if( !gabor ) return std::vector( 5, 0.0 ); return ((mp_threadedGabor *)gabor)->resultsForCategory( EmotionWeights ); }