/* * 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 // For system independent path support #include #include #include // For basename and extension namespace bf = boost::filesystem; #include "mp_rotateFace.h" #include "visualobject.h" #include "mp_SVMWeights.h" static const std::string CERT1Weights( "CERT3_3_Weights" ); static const std::string EmotionWeightsID( "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( CERT1Weights ); 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( EmotionWeightsID ); // 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 ); rotator.setEyeLenRate( 0.48 ); gabor = new GABOR_CLASS( patch, patch.width, patch.height ); setAUCategories( false ); clear(); } void MP_CERT::loadFeatureDetector( const std::string &dir ) { #ifdef USE_FEATURES bf::path dirPath( dir ); bf::path fdtxml; bf::path gpriorxml; featureDetector.setUseSequence( false ); fdtxml = dirPath / "left_eye_CERT3.fdtxml"; gpriorxml = dirPath / "left_eye_CERT3.gpriorxml"; featureDetector.loadFeatureFromFiles( fdtxml.native_file_string().c_str(), gpriorxml.native_file_string().c_str() ); fdtxml = dirPath / "right_eye_CERT3.fdtxml"; gpriorxml = dirPath / "right_eye_CERT3.gpriorxml"; featureDetector.loadFeatureFromFiles( fdtxml.native_file_string().c_str(), gpriorxml.native_file_string().c_str() ); fdtxml = dirPath / "nose_CERT3.fdtxml"; gpriorxml = dirPath / "nose_CERT3.gpriorxml"; featureDetector.loadFeatureFromFiles( fdtxml.native_file_string().c_str(), gpriorxml.native_file_string().c_str() ); fdtxml = dirPath / "mouth_CERT3_1.fdtxml"; gpriorxml = dirPath / "mouth_CERT3_1.gpriorxml"; featureDetector.loadFeatureFromFiles( fdtxml.native_file_string().c_str(), gpriorxml.native_file_string().c_str() ); featureDetector.setCERT3_3_FaceBox(); #endif } 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 } if( (lx > 0) && (rx > 0) && ( (lx-rx) > 0) ) { leftEye.x= lx; leftEye.y = ly; rightEye.x = rx; rightEye.y = ry; bool madePatch = false; #ifdef USE_FEATURES madePatch = getPatchWithFeatures( rimage ); #else madePatch = rotator.rotateImageIntoPatch( &rimage, (int)leftEye.x, (int)rightEye.x, (int)leftEye.y, (int)rightEye.y, patch ); #endif if( madePatch ) { 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(EmotionWeightsID); 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 gabor->resultsForCategory( EmotionWeightsID ); } #ifdef USE_FEATURES bool MP_CERT::getPatchWithFeatures( RImage &rimage ) { ObjectWithFeatures *largestObject = featureDetector.getLargestFace(); if( !largestObject ) return false; for( std::vector::iterator featureIter = largestObject->finalBestFeature.begin(), lastFeature = largestObject->finalBestFeature.end(); featureIter != lastFeature; ++featureIter ) { if( "left_eye" == featureIter->type ) { leftEye.x = featureIter->x; leftEye.y = featureIter->y; } else if( "left_eye" == featureIter->type ) { rightEye.x = featureIter->x; rightEye.y = featureIter->y; } } OWFList allFaces; bool didRotate = false; // FeatureAlignment::alignment_method method = FeatureAlignment::eyes_only; // For best results with the CERT 3.3 weights, we need to use this method: FeatureAlignment::alignment_method method = FeatureAlignment::unconstrained_least_squares; allFaces.push_back( *largestObject ); try { didRotate = rotator.rotateImageUsingFeatures( &featureDetector, &rimage, allFaces, patch, method ); } catch( const char *msg ) { std::cerr << msg << std::endl; } catch(...) { std::cerr << "Unable to align face patch." << std::endl; } return didRotate; } #endif