How to detect faces within a video session with iOS 5
Within my App I'm using a videosession where I can take pictures with. However I'd like to have face detection within this videosession. I looked at Apple's example "SquareCam" which is exactly what I'm looking for. However implementing their code in my project is driving me bonkers.

    #import "CaptureSessionManager.h"
    #import <ImageIO/ImageIO.h>
    
    @implementation CaptureSessionManager
    
    @synthesize captureSession;
    @synthesize previewLayer;
    @synthesize stillImageOutput;
    @synthesize stillImage;
    
    #pragma mark Capture Session Configuration
    
    - (id)init {
    	if ((self = [super init])) {
    		[self setCaptureSession:[[AVCaptureSession alloc] init]];
    	}
    	return self;
    }
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        NSLog(@"memorywarning");
        
        // Release any cached data, images, etc that aren't in use.
    }
    - (void)addVideoPreviewLayer {
    	[self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] autorelease]];
    	[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
      
    }
    
    - (void)addVideoInput {
    	AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];	
        if ([videoDevice isFocusModeSupported:AVCaptureFocusModeLocked]) {
            
            NSError *error = nil;
            
            if ([videoDevice lockForConfiguration:&error]) {
                NSLog(@"focus");
                videoDevice.focusMode = AVCaptureFocusModeLocked;
                videoDevice.focusMode = AVCaptureFocusModeContinuousAutoFocus;
                //videoDevice.focusMode = AVCaptureSessionPresetPhoto
    
                videoDevice.whiteBalanceMode = AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance;
                [captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
    
                [videoDevice unlockForConfiguration];
                
            }
            
            else {
                
            }
        }
                
                // Respond to the failure as appropriate.
    	if (videoDevice) {
    		NSError *error;
    		AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
    		if (!error) {
    			if ([[self captureSession] canAddInput:videoIn])
    				[[self captureSession] addInput:videoIn];
    			else
    				NSLog(@"Couldn't add video input");		
    		}
    		else
    			NSLog(@"Couldn't create video input");
    	}
    	else
    		NSLog(@"Couldn't create video capture device");
    }
    
    - (void)addStillImageOutput 
    {
      [self setStillImageOutput:[[[AVCaptureStillImageOutput alloc] init] autorelease]];
      NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil];
      [[self stillImageOutput] setOutputSettings:outputSettings];
      
      AVCaptureConnection *videoConnection = nil;
      for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
          if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
            videoConnection = connection;
            break;
          }
        }
        if (videoConnection) { 
          break; 
        }
      }
      
      [[self captureSession] addOutput:[self stillImageOutput]];
        [outputSettings release];
    }
    
    - (void)captureStillImage
    {  
    	AVCaptureConnection *videoConnection = nil;
    	for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
    		for (AVCaptureInputPort *port in [connection inputPorts]) {
    			if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
    				videoConnection = connection;
    				break;
    			}
    		}
    		if (videoConnection) { 
          break; 
        }
    	}
      
    	NSLog(@"about to request a capture from: %@", [self stillImageOutput]);
    	[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection 
                                                           completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 
                                                             CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
                                                             if (exifAttachments) {
                                                               NSLog(@"attachements: %@", exifAttachments);
                                                             } else { 
                                                               NSLog(@"no attachments");
                                                             }
                                                             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
                                                               
                                                             UIImage *image = [[UIImage alloc] initWithData:imageData];
                                                               
                                                             [self setStillImage:image];
                                                             [image release];
                                                             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];
                                                           }];
    }
    
    
    - (void)dealloc {
    
    	[[self captureSession] stopRunning];
    
    	[previewLayer release], previewLayer = nil;
    	[captureSession release], captureSession = nil;
        [stillImageOutput release], stillImageOutput = nil;
        [stillImage release], stillImage = nil;
    
    	[super dealloc];
    }
    
    @end


Besides my videosession I did succeeded with recognizing faces within a UIImage which I imported inside my project. I did this with the example of @Abhinav Jha (http://stackoverflow.com/questions/7871166/how-to-properly-instantiate-cidetector-class-object-in-ios-5-face-detection-api).

    CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"IMG_0056.JPG"]];
    if (ciImage == nil)
    
    [imageView setImage:[UIImage imageNamed:@"Photo.JPG"]];
    
    NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                             @"CIDetectorAccuracy", @"CIDetectorAccuracyHigh",nil];
    CIDetector *ciDetector = [CIDetector detectorOfType:CIDetectorTypeFace 
                                                context:nil
                                                options:options];
    NSArray *features = [ciDetector featuresInImage:ciImage];
    NSLog(@"no of face detected: %d", [features count]);


Hope someone can point me in the right direction with combining the two examples!
How to detect faces within a video session with iOS 5
===

Within my App I'm using a videosession where I can take pictures with. However I'd like to have face detection within this videosession. I looked at Apple's example "SquareCam" which is exactly what I'm looking for. However implementing their code in my project is driving me bonkers.

    #import "CaptureSessionManager.h"
    #import <ImageIO/ImageIO.h>
    
    @implementation CaptureSessionManager
    
    @synthesize captureSession;
    @synthesize previewLayer;
    @synthesize stillImageOutput;
    @synthesize stillImage;
    
    #pragma mark Capture Session Configuration
    
    - (id)init {
    	if ((self = [super init])) {
    		[self setCaptureSession:[[AVCaptureSession alloc] init]];
    	}
    	return self;
    }
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        NSLog(@"memorywarning");
        
        // Release any cached data, images, etc that aren't in use.
    }
    - (void)addVideoPreviewLayer {
    	[self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] autorelease]];
    	[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
      
    }
    
    - (void)addVideoInput {
    	AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];	
        if ([videoDevice isFocusModeSupported:AVCaptureFocusModeLocked]) {
            
            NSError *error = nil;
            
            if ([videoDevice lockForConfiguration:&error]) {
                NSLog(@"focus");
                videoDevice.focusMode = AVCaptureFocusModeLocked;
                videoDevice.focusMode = AVCaptureFocusModeContinuousAutoFocus;
                //videoDevice.focusMode = AVCaptureSessionPresetPhoto
    
                videoDevice.whiteBalanceMode = AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance;
                [captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
    
                [videoDevice unlockForConfiguration];
                
            }
            
            else {
                
            }
        }
                
                // Respond to the failure as appropriate.
    	if (videoDevice) {
    		NSError *error;
    		AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
    		if (!error) {
    			if ([[self captureSession] canAddInput:videoIn])
    				[[self captureSession] addInput:videoIn];
    			else
    				NSLog(@"Couldn't add video input");		
    		}
    		else
    			NSLog(@"Couldn't create video input");
    	}
    	else
    		NSLog(@"Couldn't create video capture device");
    }
    
    - (void)addStillImageOutput 
    {
      [self setStillImageOutput:[[[AVCaptureStillImageOutput alloc] init] autorelease]];
      NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil];
      [[self stillImageOutput] setOutputSettings:outputSettings];
      
      AVCaptureConnection *videoConnection = nil;
      for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
          if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
            videoConnection = connection;
            break;
          }
        }
        if (videoConnection) { 
          break; 
        }
      }
      
      [[self captureSession] addOutput:[self stillImageOutput]];
        [outputSettings release];
    }
    
    - (void)captureStillImage
    {  
    	AVCaptureConnection *videoConnection = nil;
    	for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
    		for (AVCaptureInputPort *port in [connection inputPorts]) {
    			if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
    				videoConnection = connection;
    				break;
    			}
    		}
    		if (videoConnection) { 
          break; 
        }
    	}
      
    	NSLog(@"about to request a capture from: %@", [self stillImageOutput]);
    	[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection 
                                                           completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 
                                                             CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
                                                             if (exifAttachments) {
                                                               NSLog(@"attachements: %@", exifAttachments);
                                                             } else { 
                                                               NSLog(@"no attachments");
                                                             }
                                                             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
                                                               
                                                             UIImage *image = [[UIImage alloc] initWithData:imageData];
                                                               
                                                             [self setStillImage:image];
                                                             [image release];
                                                             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];
                                                           }];
    }
    
    
    - (void)dealloc {
    
    	[[self captureSession] stopRunning];
    
    	[previewLayer release], previewLayer = nil;
    	[captureSession release], captureSession = nil;
        [stillImageOutput release], stillImageOutput = nil;
        [stillImage release], stillImage = nil;
    
    	[super dealloc];
    }
    
    @end


Besides my videosession I did succeeded with recognizing faces within a UIImage which I imported inside my project. I did this with the example of @Abhinav Jha (http://stackoverflow.com/questions/7871166/how-to-properly-instantiate-cidetector-class-object-in-ios-5-face-detection-api).

    CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"IMG_0056.JPG"]];
    if (ciImage == nil)
    
    [imageView setImage:[UIImage imageNamed:@"Photo.JPG"]];
    
    NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                             @"CIDetectorAccuracy", @"CIDetectorAccuracyHigh",nil];
    CIDetector *ciDetector = [CIDetector detectorOfType:CIDetectorTypeFace 
                                                context:nil
                                                options:options];
    NSArray *features = [ciDetector featuresInImage:ciImage];
    NSLog(@"no of face detected: %d", [features count]);


Hope someone can point me in the right direction with combining the two examples!