最近項目在作了身份證銀行卡識別以後,開始實現人臉識別和活體識別,其中人臉識別包括人臉入庫、人臉查找、人臉1:N對比、人臉N:N對比,另外活體識別運用在安全登陸功能。git
你們都熟知的支付寶使用face++ 的服務來實現人臉識別,在實際項目中使用了訊飛的人臉識別SDK進行二次封裝來實現活體識別。主要實現了張嘴和搖頭兩個活體動做的識別。據我所知,訊飛的服務是基於face++,識別率仍是很高,而且iOS和Android都對應有封裝好的SDK。github
在實際運用中,有不少app爲了高度保證用戶使用的安全問題,除了常規的帳號密碼登陸以外,相繼實現了指紋登陸,手勢登陸,第三方登錄(QQ、微信、支付寶)、刷臉登陸,接下里我就和你們分享一下如何實現人臉識別的活體檢測,這是實現刷臉登陸最基礎的實現。安全
另外,這些博文都是來源於我平常開發中的技術總結,在時間容許的狀況下,我會針對技術點分別分享iOS、Android兩個版本,儘可能附上demo以供你們參考,若是有其餘技術點須要,可在文章後留言,我會盡全力幫助你們。bash
點擊識別按鈕,調用相機微信
CameraRules類,檢測相機權限session
初始化頁面,建立攝像頁面,建立張嘴數據和搖頭數據app
開啓識別,臉部框識別async
臉部部位識別,臉部識別判斷是否檢測到人臉ide
檢測到人臉以後,判斷位置函數
位置判斷合適,判斷是否張嘴
張嘴判斷完畢,驗證是否搖頭
搖頭判斷完畢,3秒倒計時拍照
拍照完畢,選擇重拍或者上傳圖片
選擇重拍重複5-9步驟,選擇上傳將圖片數據回調
數據clean
根據實現思路分析,一步步進行編碼實現:
if([CameraRules isCapturePermissionGranted]){
[self setDeviceAuthorized:YES];
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
NSString* info=@"沒有相機權限";
[self showAlert:info];
[self setDeviceAuthorized:NO];
});
}
複製代碼
//檢測相機權限
+(BOOL)isCapturePermissionGranted{
if([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)]){
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus ==AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied){
return NO;
}
else if(authStatus==AVAuthorizationStatusNotDetermined){
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
__block BOOL isGranted=YES;
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
isGranted=granted;
dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
return isGranted;
}
else{
return YES;
}
}
else{
return YES;
}
}
複製代碼
//建立攝像頁面,建立張嘴數據和搖頭數據
[self faceUI];
[self faceCamera];
[self faceNumber];
複製代碼
float cx = (left+right)/2;
float cy = (top + bottom)/2;
float w = right - left;
float h = bottom - top;
float ncx = cy ;
float ncy = cx ;
CGRect rectFace = CGRectMake(ncx-w/2 ,ncy-w/2 , w, h);
if(!isFrontCamera){
rectFace=rSwap(rectFace);
rectFace=rRotate90(rectFace, faceImg.height, faceImg.width);
}
BOOL isNotLocation = [self identifyYourFaceLeft:left right:right top:top bottom:bottom];
if (isNotLocation==YES) {
return nil;
}
複製代碼
for(id key in keys){
id attr=[landmarkDic objectForKey:key];
if(attr && [attr isKindOfClass:[NSDictionary class]]){
if(!isFrontCamera){
p=pSwap(p);
p=pRotate90(p, faceImg.height, faceImg.width);
}
if (isCrossBorder == YES) {
[self delateNumber];
return nil;
}
p=pScale(p, widthScaleBy, heightScaleBy);
[arrStrPoints addObject:NSStringFromCGPoint(p)];
}
}
複製代碼
if (right - left < 230 || bottom - top < 250) {
self.textLabel.text = @"太遠了";
[self delateNumber];
isCrossBorder = YES;
return YES;
}else if (right - left > 320 || bottom - top > 320) {
self.textLabel.text = @"太近了";
[self delateNumber];
isCrossBorder = YES;
return YES;
}else{
if (isJudgeMouth != YES) {
self.textLabel.text = @"請重複張嘴動做";
[self tomAnimationWithName:@"openMouth" count:2];
if (left < 100 || top < 100 || right > 460 || bottom > 400) {
isCrossBorder = YES;
isJudgeMouth = NO;
self.textLabel.text = @"調整下位置先";
[self delateNumber];
return YES;
}
}else if (isJudgeMouth == YES && isShakeHead != YES) {
self.textLabel.text = @"請重複搖頭動做";
[self tomAnimationWithName:@"shakeHead" count:4];
number = 0;
}else{
takePhotoNumber += 1;
if (takePhotoNumber == 2) {
[self timeBegin];
}
}
isCrossBorder = NO;
}
複製代碼
if (rightX && leftX && upperY && lowerY && isJudgeMouth != YES) {
number ++;
if (number == 1 || number == 300 || number == 600 || number ==900) {
mouthWidthF = rightX - leftX < 0 ? abs(rightX - leftX) : rightX - leftX;
mouthHeightF = lowerY - upperY < 0 ? abs(lowerY - upperY) : lowerY - upperY;
NSLog(@"%d,%d",mouthWidthF,mouthHeightF);
}else if (number > 1200) {
[self delateNumber];
[self tomAnimationWithName:@"openMouth" count:2];
}
mouthWidth = rightX - leftX < 0 ? abs(rightX - leftX) : rightX - leftX;
mouthHeight = lowerY - upperY < 0 ? abs(lowerY - upperY) : lowerY - upperY;
NSLog(@"%d,%d",mouthWidth,mouthHeight);
NSLog(@"張嘴前:width=%d,height=%d",mouthWidthF - mouthWidth,mouthHeight - mouthHeightF);
if (mouthWidth && mouthWidthF) {
if (mouthHeight - mouthHeightF >= 20 && mouthWidthF - mouthWidth >= 15) {
isJudgeMouth = YES;
imgView.animationImages = nil;
}
}
}
複製代碼
if ([key isEqualToString:@"mouth_middle"] && isJudgeMouth == YES) {
if (bigNumber == 0 ) {
firstNumber = p.x;
bigNumber = p.x;
smallNumber = p.x;
}else if (p.x > bigNumber) {
bigNumber = p.x;
}else if (p.x < smallNumber) {
smallNumber = p.x;
}
if (bigNumber - smallNumber > 60) {
isShakeHead = YES;
[self delateNumber];
}
}
複製代碼
if(timeCount >= 1)
{
self.textLabel.text = [NSString stringWithFormat:@"%ld s後拍照",(long)timeCount];
}
else
{
[theTimer invalidate];
theTimer=nil;
[self didClickTakePhoto];
}
複製代碼
-(void)didClickPhotoAgain
{
[self delateNumber];
[self.previewLayer.session startRunning];
self.textLabel.text = @"請調整位置";
[backView removeFromSuperview];
isJudgeMouth = NO;
isShakeHead = NO;
}
複製代碼
-(void)didClickUpPhoto
{
//上傳照片成功
[self.faceDelegate sendFaceImage:imageView.image];
[self.navigationController popViewControllerAnimated:YES];
}
複製代碼
-(void)delateNumber
{
number = 0;
takePhotoNumber = 0;
mouthWidthF = 0;
mouthHeightF = 0;
mouthWidth = 0;
mouthHeight = 0;
smallNumber = 0;
bigNumber = 0;
firstNumber = 0;
imgView.animationImages = nil;
imgView.image = [UIImage imageNamed:@"shakeHead0"];
}
複製代碼
由於項目中使用到訊飛人臉識別SDK,須要去訊飛開放平臺建立應用,下載SDK。
將開發工具包中lib目錄下的iflyMSC.framework添加到工程中。同時請將Demo中依賴的其餘庫也添加到工程中。 按下圖示例添加 SDK 所須要的 iOS系統庫:
在Targets - Build Settings 中搜索Bitcode 便可,找到相應選項,設置爲NO,以下圖:
在Info.plist 中增長下圖設置:
下載demo,將demo中FBYFaceData文件夾引入項目中。
#import "FBYFaceRecognitionViewController.h"
複製代碼
-(void)pushToFaceStreamDetectorVC
{
FBYFaceRecognitionViewController *faceVC = [[FBYFaceRecognitionViewController alloc]init];
faceVC.faceDelegate = self;
[self.navigationController pushViewController:faceVC animated:YES];
}
複製代碼
-(void)sendFaceImage:(UIImage *)faceImage
{
NSLog(@"圖片上傳成功");
}
- (void)sendFaceImageError {
NSLog(@"圖片上傳失敗");
}
複製代碼
關注 【網羅開發】微信公衆號,回覆【人臉識別】即可領取。 網羅天下方法,方便你我開發,全部文檔會持續更新,歡迎關注一塊兒成長!
但願能夠幫助你們,若有問題可加QQ技術交流羣: 668562416
若是哪裏有什麼不對或者不足的地方,還望讀者多多提意見或建議
如需轉載請聯繫我,通過受權方可轉載,謝謝
本篇已同步到我的博客:FBY展菲
歡迎關注個人公衆號:網羅開發