iOS 基于AVFoundation框架搭建人臉識別,并獲取識別到的人臉圖片

這段時間抽空做了下人臉識別功能,人臉識別有很多第三方的SDK,例如:Face++,騰訊、訊飛、opencv等,但其實iOS 原生已經支持人臉識別,網上也有很多人臉識別的demo,但基本檢測到人臉后就沒后續操作了,因此這里分享下識別到人臉后獲取識別到的人臉圖片。

識別的原理是:客戶端檢測到人臉,然后將識別到的人臉照片請求后臺接口,讓后臺做人臉校驗,成功后返回相關信息!

下面給出基于AVFoundation框架搭建人臉檢測功能代碼:
一、導入 <AVFoundation/AVFoundation.h>框架,并設置相關代理和屬性
#import <AVFoundation/AVFoundation.h>
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
#define WS(weakSelf) __weak __typeof(&*self) weakSelf = self

@interface FaceViewController ()<AVCaptureVideoDataOutputSampleBufferDelegate>
@property (nonatomic,strong) AVCaptureSession *session;
@property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewLayer;
@property (nonatomic,strong) AVCaptureDeviceInput*input;
@property (nonatomic,strong) AVCaptureVideoDataOutput *videoOutput;
@property(nonatomic,strong) UIImageView *faceImgView;
@property(nonatomic,assign)BOOL isFirst;

@end
二、界面初始化

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"人臉識別";
    
    _isFirst = YES;
    [self deviceInit];
    [self initUI];
}


-(void)initUI{
    _faceImgView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth - 120 , 64, 120, 120)];
    _faceImgView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:_faceImgView];
    
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(52, 100, kWidth - 108, 18)];
    titleLab.text = @"請對準臉部拍攝  提高認證成功率";
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = [UIColor redColor];
    titleLab.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:titleLab];
}

三、相機設備初始化

-(void)deviceInit{
    // 獲取輸入設備(攝像頭)
    NSArray *devices = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack].devices;
    AVCaptureDevice *deviceF = devices[0];
    
    // 根據輸入設備創建輸入對象
    self.input = [[AVCaptureDeviceInput alloc] initWithDevice:deviceF error:nil];
    
    // 設置代理監聽輸出對象輸出的數據
    self.videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    
    // 對實時視頻幀進行相關的渲染操作,指定代理
    [_videoOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
    
    self.session = [[AVCaptureSession alloc] init];
    
    // 設置輸出質量(高像素輸出)
    if ([self.session canSetSessionPreset:AVCaptureSessionPreset640x480]) {
        [self.session setSessionPreset:AVCaptureSessionPreset640x480];
    }
    // 添加輸入和輸出到會話
    [self.session beginConfiguration];
    
    if ([self.session canAddInput:_input]) {
        [self.session addInput:_input];
    }
    
    if ([self.session canAddOutput:_videoOutput]) {
        [self.session addOutput:_videoOutput];
    }
    
    [self.session commitConfiguration];
    
    AVCaptureSession *session = (AVCaptureSession *)self.session;
    
    //8.創建預覽圖層
    _previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    _previewLayer.frame = self.view.bounds;
    [self.view.layer insertSublayer:_previewLayer atIndex:0];
    

    //10. 開始掃描
    [self.session startRunning];

}
四、將CMSampleBufferRef轉為NSImage
- (void )imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
    //CIImage -> CGImageRef -> UIImage
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);  //拿到緩沖區幀數據
    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];            //創建CIImage對象
    CIContext *temporaryContext = [CIContext contextWithOptions:nil];           //創建上下文
    
    //識別臉部
    CIDetector *detector=[CIDetector detectorOfType:CIDetectorTypeFace context:temporaryContext options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}]; //CIDetectorAccuracyLow:識別精度低,但識別速度快、性能高
                                                              //CIDetectorAccuracyHigh:識別精度高、但識別速度比較慢
    NSArray *faceArray = [detector featuresInImage:ciImage
                                           options:nil];
    
    //得到人臉圖片的尺寸
    if (faceArray.count) {
        NSLog(@"faceArray == %@",faceArray);
        WS(weakSelf);
        for (CIFaceFeature * faceFeature in faceArray) {
           if (faceFeature.hasLeftEyePosition && faceFeature.hasRightEyePosition  && faceFeature.hasMouthPosition) {
                NSLog(@"_isFirst == %d",_isFirst);
               //這個布爾值用于判斷檢測到人臉后,獲取到人臉照片,不用再進行持續檢測
                if (_isFirst) {
                    //因為剛開始掃描到的人臉是模糊照片,所以延遲幾秒獲取
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        CGImageRef cgImageRef = [temporaryContext createCGImage:ciImage fromRect:faceFeature.bounds];
                        
                        //resultImg即為獲得的人臉圖片
                        UIImage   *resultImg = [[UIImage alloc] initWithCGImage:cgImageRef scale:0.1 orientation:UIImageOrientationLeftMirrored];
                        
                        //顯示人臉圖片,這里可以將圖片轉為NSdata類型后,請求后臺接口
                        [self uploadFaceImg:resultImg];
                        //置為NO
                        weakSelf.isFirst = NO;
                    });
                   
                }
           }
        }
    }
}

六、 顯示捕捉到的人臉圖片
//顯示圖片,這里可以請求后臺接口
-(void)uploadFaceImg:(UIImage *)image{
    _faceImgView.image = image;

     WS(weakSelf);
    //這里設置為2秒后可以進行繼續檢測
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        weakSelf.isFirst = YES;
    });
}
七、實現 AVCaptureVideoDataOutput獲取實時圖像的代理
#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
//AVCaptureVideoDataOutput獲取實時圖像,這個代理方法的回調頻率很快,幾乎與手機屏幕的刷新頻率一樣快
- (void)captureOutput:(AVCaptureOutput*)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection{
    [self imageFromSampleBuffer:sampleBuffer];
}

結語:

以上就是人臉識別功能代碼, 如有問題請下方留言指正!
如有幫助請??支持一下 ??
Demo地址: https://github.com/zhwIdea/FaceDetect

下一篇文章:基于GPUImage框架進行人臉識別

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容