這段時間抽空做了下人臉識別功能废酷,人臉識別有很多第三方的SDK,例如:Face++抹缕,騰訊澈蟆、訊飛、opencv等卓研,但其實iOS 原生已經(jīng)支持人臉識別趴俘,網(wǎng)上也有很多人臉識別的demo,但基本檢測到人臉后就沒后續(xù)操作了奏赘,因此這里分享下識別到人臉后獲取識別到的人臉圖片寥闪。
識別的原理是:客戶端檢測到人臉,然后將識別到的人臉照片請求后臺接口磨淌,讓后臺做人臉校驗疲憋,成功后返回相關信息!
下面給出基于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];
// 根據(jù)輸入設備創(chuàng)建輸入對象
self.input = [[AVCaptureDeviceInput alloc] initWithDevice:deviceF error:nil];
// 設置代理監(jiān)聽輸出對象輸出的數(shù)據(jù)
self.videoOutput = [[AVCaptureVideoDataOutput alloc] init];
// 對實時視頻幀進行相關的渲染操作,指定代理
[_videoOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
self.session = [[AVCaptureSession alloc] init];
// 設置輸出質(zhì)量(高像素輸出)
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.創(chuàng)建預覽圖層
_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轉(zhuǎn)為NSImage
- (void )imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
//CIImage -> CGImageRef -> UIImage
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); //拿到緩沖區(qū)幀數(shù)據(jù)
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer]; //創(chuàng)建CIImage對象
CIContext *temporaryContext = [CIContext contextWithOptions:nil]; //創(chuàng)建上下文
//識別臉部
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);
//這個布爾值用于判斷檢測到人臉后,獲取到人臉照片,不用再進行持續(xù)檢測
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];
//顯示人臉圖片谐檀,這里可以將圖片轉(zhuǎn)為NSdata類型后,請求后臺接口
[self uploadFaceImg:resultImg];
//置為NO
weakSelf.isFirst = NO;
});
}
}
}
}
}
六裁奇、 顯示捕捉到的人臉圖片
//顯示圖片桐猬,這里可以請求后臺接口
-(void)uploadFaceImg:(UIImage *)image{
_faceImgView.image = image;
WS(weakSelf);
//這里設置為2秒后可以進行繼續(xù)檢測
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
weakSelf.isFirst = YES;
});
}
七、實現(xiàn) AVCaptureVideoDataOutput
獲取實時圖像的代理
#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
//AVCaptureVideoDataOutput獲取實時圖像刽肠,這個代理方法的回調(diào)頻率很快溃肪,幾乎與手機屏幕的刷新頻率一樣快
- (void)captureOutput:(AVCaptureOutput*)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection{
[self imageFromSampleBuffer:sampleBuffer];
}
結語:
以上就是人臉識別功能代碼, 如有問題請下方留言指正!
如有幫助請??支持一下 ??
Demo地址: https://github.com/zhwIdea/FaceDetect
下一篇文章:基于GPUImage框架進行人臉識別