2017-08-15 8 views
0

カムビューだけでアプリを構築する必要があります。私のカムが顔を見ていることを検出する必要があります。私が行っている私のカムが開いている間に顔を検出する

- (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]; 

} 

以下: 私は画像の上に顔を検出し、何かを構築していますが、私はカムで作業する必要があり、ここに私はこれまでやっていることである

-(void)viewWillAppear:(BOOL)animated{ 
    _session = [[AVCaptureSession alloc] init]; 
    [_session setSessionPreset:AVCaptureSessionPresetPhoto]; 

    AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

    NSError *error; 
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&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"; 
     } 
    } 
} 

まだ顔を検出していない、何か間違っている?

+1

AVCaptureSessionを検索してください。 –

答えて

1

データを取得する前に、メタデータ出力を追加する必要があります。

AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init]; 
// create a serial queue to handle metadata output 
dispatch_queue_t metadataQueueOutput = dispatch_queue_create("com.YourAppName.metaDataQueue.OutputQueue", DISPATCH_QUEUE_SERIAL); 
[metadataOutput setMetadataObjectsDelegate:self queue:metadataQueueOutput]; 
if ([_session canAddOutput:metadataOutput]) { 
    [strongSelf.session addOutput:metadataOutput]; 
} 
// set object types that you are interested, then you should not check type in output callback 
metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeFace]; 

これはうまくいくはずです。もしそうなら私に教えてください

+0

それを試してみました...うまく動作します!ありがとう。 – user3411226

関連する問題