// // MPT_Emotions1.m // GenderController // // Created by Andrew Salamon on 9/22/08. // Copyright 2008 Machine Perception Laboratory, University of California San Diego. All rights reserved. // #import "MPT_Emotions1.h" #import @interface MPT_Emotions1(PrivateMethods) -(void)makeNewEmotions; @end @implementation MPT_Emotions1 - (id)initWithBundle:(NSBundle *)_bundle { if( self = [super initWithBundle:_bundle] ) { [self makeNewEmotions]; } return self; } - (void)dealloc { delete emotions; [super dealloc]; } - (id)copyWithZone:(NSZone *)zone { id cp = [super copyWithZone:zone]; return cp; } - (void)makeThreadsafe { [super makeThreadsafe]; [self makeNewEmotions]; } - (NSString *)localizedName { return NSLocalizedStringFromTableInBundle(@"MPT Emotion Detector", nil, [self bundle], @""); } - (NSString *)internalName { return @"emotion1"; } - (NSString *)step { return MPT_PluginStep_Results; } -(void)makeNewEmotions { emotions = new mpt::Emotions1; NSString *weightsPath = [[self bundle] pathForResource:@"emotions1" ofType:@"txt"]; // need to load the emotion weights if( [weightsPath length] > 0 ) emotions->readFromFile( [weightsPath UTF8String] ); else NSLog( @"Unable to read emotion weights file in Emotions1 plugin." ); } bool addAUToInput( NSString *auID, NSDictionary *aus, std::vector &inputs ) { NSNumber *auNum = [aus objectForKey:auID]; if( !auNum ) { return NO; } inputs.push_back( [auNum floatValue] ); return YES; } - (NSMutableDictionary *)processResults:(NSDictionary *)results usingController:(id)controller; { NSMutableDictionary *res = [NSMutableDictionary dictionary]; // edu.ucsd.mplab.plugins.CERT4_4_Weights: 1, 2, 4, 5, 9, 10, 12, 14, 15, 17, 20 // edu.ucsd.mplab.plugins.AdditonalAUs: 25, 17, 7, 6, 26, 15, 24, 23, 27, 18, 9, 28 NSDictionary *aus = [results objectForKey:@"edu.ucsd.mplab.plugins.CERT4_4_Weights"]; NSDictionary *addAUs = [results objectForKey:@"edu.ucsd.mplab.plugins.AdditonalAUs"]; std::vector inputs; // Find the inputs we need and add them to a vector // need the following aus: 1 2 4 5 6 7 9 10 12 14 15 17 18 20 23 24 25 26 28 if( !addAUToInput( @"(AU 1) Inner Brow Raise", aus, inputs ) ) return nil; if( !addAUToInput( @"(AU 2) Outer Brow Raise", aus, inputs ) ) return nil; if( !addAUToInput( @"(AU 4) Brow Lower", aus, inputs ) ) return nil; if( !addAUToInput( @"(AU 5) Eye Widen", aus, inputs ) ) return nil; if( !addAUToInput( @"AU 6", addAUs, inputs ) ) return nil; if( !addAUToInput( @"AU 7", addAUs, inputs ) ) return nil; if( !addAUToInput( @"(AU 9) Nose Wrinkle", aus, inputs ) ) return nil; if( !addAUToInput( @"(AU 10) Lip Raise", aus, inputs ) ) return nil; if( !addAUToInput( @"(AU 12) Lip Corner Pull", aus, inputs ) ) return nil; if( !addAUToInput( @"(AU 14) Dimpler", aus, inputs ) ) return nil; if( !addAUToInput( @"(AU 15) Lip Corner Depressor", aus, inputs ) ) return nil; if( !addAUToInput( @"(AU 17) Chin Raise", aus, inputs ) ) return nil; if( !addAUToInput( @"AU 18", addAUs, inputs ) ) return nil; if( !addAUToInput( @"(AU 20) Lip stretch", aus, inputs ) ) return nil; if( !addAUToInput( @"AU 23", addAUs, inputs ) ) return nil; if( !addAUToInput( @"AU 24", addAUs, inputs ) ) return nil; if( !addAUToInput( @"AU 25", addAUs, inputs ) ) return nil; if( !addAUToInput( @"AU 26", addAUs, inputs ) ) return nil; if( !addAUToInput( @"AU 28", addAUs, inputs ) ) return nil; // if( !addAUToInput( @"AU 17", addAUs, inputs ) ) return nil; // if( !addAUToInput( @"AU 15", addAUs, inputs ) ) return nil; // if( !addAUToInput( @"AU 27", addAUs, inputs ) ) return nil; // if( !addAUToInput( @"AU 9", addAUs, inputs ) ) return nil; //call if( inputs.size() != mpt::Emotions1::auCount ) { return res; } std::vector ans = emotions->calc( inputs ); if( ans.size() != mpt::Emotions1::emotionCount ) { return res; } // copy ans to res int index = 0; for( NSString *label in [self labels] ) { [res setObject:[NSNumber numberWithFloat:ans[index++]] forKey:label]; } return res; } @end