人臉識別 -- 活體檢測(張嘴搖頭識別)

一:簡介

最近項目在做了身份證銀行卡識別之后,開始實現(xiàn)人臉識別和活體識別,其中人臉識別包括人臉入庫、人臉查找、人臉1:N對比、人臉N:N對比,另外活體識別運用在安全登錄功能。

大家都熟知的支付寶使用face++ 的服務來實現(xiàn)人臉識別,在實際項目中使用了訊飛的人臉識別SDK進行二次封裝來實現(xiàn)活體識別。主要實現(xiàn)了張嘴和搖頭兩個活體動作的識別。據(jù)我所知,訊飛的服務是基于face++,識別率還是很高,并且iOS和Android都對應有封裝好的SDK。

在實際運用中,有很多app為了高度保證用戶使用的安全問題,除了常規(guī)的賬號密碼登錄之外,相繼實現(xiàn)了指紋登錄,手勢登錄,第三方登陸(QQ、微信、支付寶)、刷臉登錄,接下里我就和大家分享一下如何實現(xiàn)人臉識別的活體檢測,這是實現(xiàn)刷臉登錄最基礎的實現(xiàn)。

另外,這些博文都是來源于我日常開發(fā)中的技術總結,在時間允許的情況下,我會針對技術點分別分享iOS、Android兩個版本,盡量附上demo以供大家參考,如果有其他技術點需要,可在文章后留言,我會盡全力幫助大家。

二:實現(xiàn)思路分析

  1. 點擊識別按鈕,調用相機

  2. CameraRules類,檢測相機權限

  3. 初始化頁面,創(chuàng)建攝像頁面,創(chuàng)建張嘴數(shù)據(jù)和搖頭數(shù)據(jù)

  4. 開啟識別,臉部框識別

  5. 臉部部位識別,臉部識別判斷是否檢測到人臉

  6. 檢測到人臉之后,判斷位置

  7. 位置判斷合適,判斷是否張嘴

  8. 張嘴判斷完畢,驗證是否搖頭

  9. 搖頭判斷完畢,3秒倒計時拍照

  10. 拍照完畢,選擇重拍或者上傳圖片

  11. 選擇重拍重復5-9步驟,選擇上傳將圖片數(shù)據(jù)回調

  12. 數(shù)據(jù)clean

三:實現(xiàn)源碼分析

根據(jù)實現(xiàn)思路分析,一步步進行編碼實現(xiàn):

1. 點擊識別按鈕,調用相機

if([CameraRules isCapturePermissionGranted]){
        [self setDeviceAuthorized:YES];
    }
    else{
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString* info=@"沒有相機權限";
            [self showAlert:info];
            [self setDeviceAuthorized:NO];
        });
    }

2. CameraRules類,檢測相機權限

//檢測相機權限
+(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;
    }
}

3. 初始化頁面,創(chuàng)建攝像頁面,創(chuàng)建張嘴數(shù)據(jù)和搖頭數(shù)據(jù)

    //創(chuàng)建攝像頁面,創(chuàng)建張嘴數(shù)據(jù)和搖頭數(shù)據(jù)
    [self faceUI];
    [self faceCamera];
    [self faceNumber];

4. 開啟識別,臉部框識別

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

5. 臉部部位識別,臉部識別判斷是否檢測到人臉

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

6. 檢測到人臉之后,判斷位置動作提醒

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

7. 位置判斷合適,判斷是否張嘴

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

8. 張嘴判斷完畢,驗證是否搖頭

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

9. 搖頭判斷完畢,3秒倒計時拍照

if(timeCount >= 1)
    {
        self.textLabel.text = [NSString  stringWithFormat:@"%ld s后拍照",(long)timeCount];
    }
    else
    {
        [theTimer invalidate];
        theTimer=nil;
        
        [self didClickTakePhoto];
    }

10. 拍照完畢,選擇重拍或者上傳圖片

-(void)didClickPhotoAgain
{
    [self delateNumber];
    
    [self.previewLayer.session startRunning];
    self.textLabel.text = @"請調整位置";
    
    [backView removeFromSuperview];
    
    isJudgeMouth = NO;
    isShakeHead = NO;
    
}

11. 選擇重拍重復5-9步驟,選擇上傳將圖片數(shù)據(jù)回調

-(void)didClickUpPhoto
{
    //上傳照片成功
    [self.faceDelegate sendFaceImage:imageView.image];
    [self.navigationController popViewControllerAnimated:YES];
}

12. 數(shù)據(jù)clean

-(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下載及配置

1. SDK下載

因為項目中使用到訊飛人臉識別SDK,需要去訊飛開放平臺創(chuàng)建應用,下載SDK。

11.png

2. 添加系統(tǒng)庫

將開發(fā)工具包中l(wèi)ib目錄下的iflyMSC.framework添加到工程中。同時請將Demo中依賴的其他庫也添加到工程中。 按下圖示例添加 SDK 所需要的 iOS系統(tǒng)庫:

88.png

3. 設置Bitcode

在Targets - Build Settings 中搜索Bitcode 即可,找到相應選項,設置為NO,如下圖:

333.jpg

4. 用戶隱私權限配置

在Info.plist 中增加下圖設置:

444.png

五:項目實際使用

1. 下載demo

下載demo,將demo中FBYFaceData文件夾引入項目中。

2. 在項目中引入FBYFaceRecognitionViewController

#import "FBYFaceRecognitionViewController.h"

3. 在項目識別按鈕的點擊事件中添加代碼

-(void)pushToFaceStreamDetectorVC
{
    FBYFaceRecognitionViewController *faceVC = [[FBYFaceRecognitionViewController alloc]init];
    faceVC.faceDelegate = self;
    [self.navigationController pushViewController:faceVC animated:YES];
}

4. 圖片回調函數(shù)

-(void)sendFaceImage:(UIImage *)faceImage
{
    NSLog(@"圖片上傳成功");
}

- (void)sendFaceImageError {
    NSLog(@"圖片上傳失敗");
}

本篇文章demo源碼:

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

相關閱讀更多精彩內容

友情鏈接更多精彩內容