// // PoseController.m // PoseController // // Created by Andrew Salamon on 2/4/08. // Copyright 2008 Machine Perception Laboratory, University of California San Diego.. All rights reserved. // #import "PoseController.h" @interface PoseController(PrivateMethods) - (BOOL)getFeature:(char *)type X:(float &)x Y:(float &)y fromFace:(ObjectWithFeatures &)face; - (void)resultsInDegreesChanged; @end @implementation PoseController - (id)initWithBundle:(NSBundle *)_bundle { if( self = [super initWithBundle:_bundle] ) { pose = new PoseDetector; prefs = [NSUserDefaults standardUserDefaults]; NSDictionary *defPrefs = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"resultsInDegrees"] forKey:[self pluginID]]; [prefs registerDefaults:defPrefs]; // [self setResultsInDegrees:YES]; NSDictionary *context = [NSDictionary dictionaryWithObject:self forKey:@"NSOwner"]; if( ![[self bundle] loadNibFile:@"PosePrefs.nib" externalNameTable:context withZone:[self zone]] ) NSLog( @"Unable to load pref pane for Pose Detector plugin." ); } return self; } - (void)dealloc { delete pose; [super dealloc]; } - (id)copyWithZone:(NSZone *)zone { id cp = [super copyWithZone:zone]; return cp; } - (void)makeThreadsafe { [super makeThreadsafe]; pose = new PoseDetector; } //- (BOOL)loadLabels //{ // labels = [[NSArray alloc] initWithObjects:[self localizedName], nil]; // return YES; //} - (NSString *)pluginID { NSString *ident = [super pluginID]; if( [ident length] > 0 ) return ident; return @"edu.ucsd.mplab.plugins.PoseDetector"; } - (NSString *)localizedName { return NSLocalizedStringFromTableInBundle(@"Pose Detector", nil, [NSBundle bundleForClass:[self class]], @""); } - (NSString *)internalName { return @"pose"; } - (NSString *)step { return MPT_PluginStep_AfterFaces; } - (NSView *)prefPane { return prefPane; } - (id)processFaces:(EyeFaceList &)faces fromEyefinder:(MPTSlidingWindow *)eyefinder withImage:(RImage &)pixels { return NULL; } - (BOOL)getFeature:(char *)type X:(float &)x Y:(float &)y fromFace:(ObjectWithFeatures &)face { VisualObject feature; if( !face.getFeatureOfType( type, feature ) ) return NO; x = feature.x; y = feature.y; return YES; } - (id)processFace:(ObjectWithFeatures &)face fromFeatureDetector:(FeatureDetector *)fd withImage:(RImage &)pixels { float yaw; float pitch; float roll; float leyeX; float leyeY; float reyeX; float reyeY; float noseX; float noseY; float mouthX; float mouthY; if( ![self getFeature:"left_eye" X:leyeX Y:leyeY fromFace:face] ) return NULL; if( ![self getFeature:"right_eye" X:reyeX Y:reyeY fromFace:face] ) return NULL; if( ![self getFeature:"nose" X:noseX Y:noseY fromFace:face] ) return NULL; if( ![self getFeature:"mouth" X:mouthX Y:mouthY fromFace:face] ) return NULL; pose->estimatePose( pixels, leyeX, leyeY, reyeX, reyeY, noseX, noseY, mouthX, mouthY, yaw, pitch, roll ); if( [self resultsInDegrees] ) { yaw = PoseDetector::rad2deg( yaw ); pitch = PoseDetector::rad2deg( pitch ); roll = PoseDetector::rad2deg( roll ); } NSDictionary *res = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:yaw], [labels objectAtIndex:0], [NSNumber numberWithFloat:pitch], [labels objectAtIndex:1], [NSNumber numberWithFloat:roll], [labels objectAtIndex:2], nil]; return res; } - (id)processFace:(FaceObject &)face withImage:(RImage &)pixels { return NULL; } - (void)processNoFace { } - (void)resultsInDegreesChanged { NSArray *allViews = [views allValues]; [allViews makeObjectsPerformSelector:@selector(resetDynamicRange)]; } - (BOOL)resultsInDegrees { NSDictionary *myPrefs = [prefs dictionaryForKey:[self pluginID]]; NSNumber *num = [myPrefs objectForKey:@"resultsInDegrees"]; if( num ) return [num boolValue]; return YES; } - (void)setResultsInDegrees:(BOOL)value { BOOL oldValue = [self resultsInDegrees]; if( oldValue != value ) { NSMutableDictionary *myPrefs = (NSMutableDictionary *)[prefs dictionaryForKey:[self pluginID]]; if( myPrefs ) myPrefs = [[myPrefs mutableCopy] autorelease]; else myPrefs = [NSMutableDictionary dictionary]; NSNumber *num = [NSNumber numberWithBool:value]; [myPrefs setObject:num forKey:@"resultsInDegrees"]; [prefs setObject:myPrefs forKey:[self pluginID]]; [self resultsInDegreesChanged]; } } @end