iOS之人臉識別

最近做一個項目有人臉識別和自動拍照兩個功能,先記錄一下相關代碼。
人臉識別這塊用的第三方是face++,但客戶端沒有應用,只負責拍照傳照片,數(shù)據(jù)由后臺來分析,那么就此來講實現(xiàn)這些功能其實和掃碼類似。

首先獲取設備,創(chuàng)建輸入輸出流,以及捕捉靜態(tài)圖片流,捕捉影像層,初始化AVCaptureSession對象。

//獲取攝像設備
    device = [self cameraWithPosition:AVCaptureDevicePositionFront];
    //創(chuàng)建輸入流
    input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    if (!input) return;
    //創(chuàng)建輸出流
    output = [[AVCaptureMetadataOutput alloc]init];
    //設置代理 在主線程里刷新
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //初始化鏈接對象
    _session = [[AVCaptureSession alloc]init];
    //高質量采集率
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    [_session addInput:input];
    [_session addOutput:output];
    [_session addOutput:self.stillImageOutput];
    if ([_session canAddOutput:self.videoDataOutput]) {
        [_session addOutput:self.videoDataOutput];
    }
    //設置像素格式
    [_videoDataOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
    //設置掃臉類型
    output.metadataObjectTypes=@[AVMetadataObjectTypeFace];
    layer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
    layer.frame=self.view.layer.bounds;
    [self.view.layer insertSublayer:layer atIndex:0];
    //開始捕獲
    [_session startRunning];

設置AVCaptureMetadataOutputObjectsDelegate,AVCaptureVideoDataOutputSampleBufferDelegate代理,并且實現(xiàn)兩個方法。

//AVCaptureVideoDataOutput獲取實時圖像,這個代理方法的回調頻率很快,幾乎與手機屏幕的刷新頻率一樣快
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{

    [connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
    constantImage = [self imageFromSampleBuffer:sampleBuffer];
    [self addFaceFrameWithImage:constantImage];
}
//CMSampleBufferRef轉NSImage
-(UIImage *)imageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer{
    // 為媒體數(shù)據(jù)設置一個CMSampleBuffer的Core Video圖像緩存對象
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    // 鎖定pixel buffer的基地址
    CVPixelBufferLockBaseAddress(imageBuffer, 0);
    // 得到pixel buffer的基地址
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
    // 得到pixel buffer的行字節(jié)數(shù)
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    // 得到pixel buffer的寬和高
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    // 創(chuàng)建一個依賴于設備的RGB顏色空間
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    // 用抽樣緩存的數(shù)據(jù)創(chuàng)建一個位圖格式的圖形上下文(graphics context)對象
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    // 根據(jù)這個位圖context中的像素數(shù)據(jù)創(chuàng)建一個Quartz image對象
    CGImageRef quartzImage = CGBitmapContextCreateImage(context);
    // 解鎖pixel buffer
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);
    // 釋放context和顏色空間
    CGContextRelease(context); CGColorSpaceRelease(colorSpace);
    // 用Quartz image創(chuàng)建一個UIImage對象image
    UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1 orientation:UIImageOrientationUp];
    // 釋放Quartz image對象
    CGImageRelease(quartzImage);
    return (image);
}
//人臉定位,添加框架
- (void)addFaceFrameWithImage:(UIImage *)images{
    CIContext * context = [CIContext contextWithOptions:nil ];
    CIImage * image = [CIImage imageWithCGImage:images.CGImage];
    NSDictionary * param = [NSDictionary dictionaryWithObject:CIDetectorAccuracyLow forKey:CIDetectorAccuracy];
    CIDetector * faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:param];
    NSArray * detectResult = [faceDetector featuresInImage:image];
    for(int j=0;m_highlitView[j]!=nil;j++){
        m_highlitView[j].hidden = YES;
    }
    int i=0;
    for(CIFaceFeature* faceObject in detectResult){
        CGRect modifiedFaceBounds = faceObject.bounds;
        modifiedFaceBounds.origin.y = images.size.height-faceObject.bounds.size.height -faceObject.bounds.origin.y;
        [self addSubViewWithFrame:modifiedFaceBounds index:i];
        i++;
    }
}
///自畫圖像
-(void)addSubViewWithFrame:(CGRect)frame  index:(int)_index{
    if(m_highlitView[_index]==nil) {
        m_highlitView[_index]= [[UIView alloc] initWithFrame:frame];
        m_highlitView[_index].layer.borderWidth = 2;
        m_highlitView[_index].layer.borderColor = [[UIColor redColor] CGColor];
        [self.view addSubview:m_highlitView[_index]];
        m_transform[_index] = m_highlitView[_index].transform;
    }
    frame.origin.x = frame.origin.x/2.5;
    frame.origin.y = frame.origin.y/2.5;
    frame.size.width = frame.size.width/1.8;
    frame.size.height = frame.size.height/1.8;
    m_highlitView[_index].frame = frame;
    
    ///根據(jù)頭像大小縮放自畫View
    float scale = frame.size.width/220;
    CGAffineTransform transform = CGAffineTransformScale(m_transform[_index], scale,scale);
    m_highlitView[_index].transform = transform;
    m_highlitView[_index].hidden = NO;
}

通過如下方法識別人臉

captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection 

通過如下方法獲取圖片,最終上傳服務器比較

captureStillImageAsynchronouslyFromConnection:(AVCaptureConnection *)connection completionHandler:(void (^)(CMSampleBufferRef imageDataSampleBuffer, NSError *error))handler

目前未解決的問題是,添加識別人臉的紅框閃動頻率很快,其他正常!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容