代码之家  ›  专栏  ›  技术社区  ›  user3411226

打开摄像机时检测人脸

  •  0
  • user3411226  · 技术社区  · 9 年前

    我已经建立了一些可以在图像上检测人脸的东西,但我需要使用摄像头,以下是我迄今为止所做的:

    - (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 回复  |  直到 9 年前
        1
  •  1
  •   Stas Volskiy    9 年前

    在获得一些数据之前,应该添加元数据输出。

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

    这应该行得通。如果有,请告诉我