title: Detect a face while my cam is open
body: <p>i need to build an app with just a cam view, and it should detect my cam is looking at a face, can anyone point me in the right direction?
  I have built something that detects a face on an image, but i need to work with a cam, here is what i have done so far:</p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
      NSString *path = [[NSBundle mainBundle] pathForResource:@"picture" ofType:@"JPG"];
      NSURL *url = [NSURL fileURLWithPath:path];
  
      CIContext *context = [CIContext contextWithOptions:nil];
  
      CIImage *image = [CIImage imageWithContentsOfURL:url];
  
      NSDictionary *options = @{CIDetectorAccuracy: CIDetectorAccuracyHigh};
  
      CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:options];
  
      NSArray *features = [detector featuresInImage:image];
  
  }
  </code></pre>
  
  <p>i have done the following:</p>
  
  <pre><code>-(void)viewWillAppear:(BOOL)animated{
      _session = [[AVCaptureSession alloc] init];
      [_session setSessionPreset:AVCaptureSessionPresetPhoto];
  
      AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  
      NSError *error;
      AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&amp;error];
  
      if([_session canAddInput:deviceInput]){
          [_session addInput:deviceInput];
      }
  
      AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_session];
      [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
      CALayer *rootLayer = [[self view] layer];
      [rootLayer setMasksToBounds:YES];
  
      CGRect frame = self.frameCapture.frame;
      [previewLayer setFrame:frame];
  
      [rootLayer insertSublayer:previewLayer atIndex:0];
      [_session startRunning];
  
  }
  
  -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
      for(AVMetadataObject *metadataObject in metadataObjects) {
          if([metadataObject.type isEqualToString:AVMetadataObjectTypeFace]) {
  
             _faceDetectedLabel.text = @"face detected";
          }
      }
  }
  </code></pre>
  
  <p>but still it is not detecting any faces, am i doing anything wrong?</p>