A:

Recently, after the identification of id card and bank card, the project began to realize face recognition and living body recognition, among which face recognition includes face storage, face search, face 1 :N comparison, face N:N comparison, and living body recognition is used in the security login function.

As we all know, alipay uses the service of face++ to realize face recognition. In the actual project, iflytek’s face recognition SDK is used for secondary encapsulation to realize living body recognition. The recognition of open mouth and shake head is realized. As far as I know, iflytek’s service is based on face++, the recognition rate is still very high, and both iOS and Android should have packaged SDK.

In practice, there are a lot of app height in order to guarantee the safety of the users, in addition to the regular account password to log in, have realized the fingerprint login, gestures login, third-party login (QQ, WeChat, pay treasure), brush face login, next I will share with you how to realize face recognition in vivo detection, This is the most basic implementation of face login.

In addition, these blog posts are from my technical summary in daily development. If time permits, I will share iOS and Android versions respectively for technical points, and try to attach demo for everyone’s reference. If you need other technical points, you can leave a message after the article, and I will do my best to help you.

Two: realize the analysis of ideas

  1. Click the identify button to invoke the camera

  2. Class CameraRules, check camera permissions

  3. Initialize the page, create a camera page, create open mouth data and shake head data

  4. Turn on recognition, face frame recognition

  5. Face part recognition, face recognition to determine whether to detect a face

  6. Once a face is detected, determine its location

  7. Judge the position. Judge if you open your mouth

  8. Open mouth to judge, verify whether shaking head

  9. Shake head judge finished, 3 seconds countdown photo

  10. After taking the photo, choose to re-take or upload the image

  11. Select reshoot and repeat steps 5-9. Select Upload to call back the image data

  12. Data on the clean

Three: realize source code analysis

According to the analysis of the implementation ideas, step by step to achieve the coding:

1. Click the recognition button to call the camera

if([CameraRules isCapturePermissionGranted]){
        [self setDeviceAuthorized:YES];
    }
    else{
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString* info=@"No camera access.";
            [self showAlert:info];
            [self setDeviceAuthorized:NO];
        });
    }
Copy the code

2. CameraRules, which checks camera permissions

/ / inspection camera permissions + (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{
            returnYES; }}else{
        returnYES; }}Copy the code

3. Initialize the page, create a camera page, create open mouth data and shake head data

// Create camera page, create open mouth data and shake head data [self faceUI]; [self faceCamera]; [self faceNumber];Copy the code

4. Enable recognition, face frame recognition

    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;
    }
Copy the code

5. Face recognition, face recognition to determine whether to detect a face

    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];
                returnnil; } p=pScale(p, widthScaleBy, heightScaleBy); [arrStrPoints addObject:NSStringFromCGPoint(p)]; }}Copy the code

6. After detecting the face, judge the position and remind the action

    if (right - left < 230 || bottom - top < 250) {
        self.textLabel.text = @"It's too far";
        [self delateNumber];
        isCrossBorder = YES;
        return YES;
    }else if (right - left > 320 || bottom - top > 320) {
        self.textLabel.text = @"Too close.";
        [self delateNumber];
        isCrossBorder = YES;
        return YES;
    }else{
        if(isJudgeMouth ! = YES) { self.textLabel.text = @"Open your mouth again, please.";
            [self tomAnimationWithName:@"openMouth" count:2];
            
            if (left < 100 || top < 100 || right > 460 || bottom > 400) {
                isCrossBorder = YES;
                isJudgeMouth = NO;
                self.textLabel.text = @"Adjust your position first.";
                [self delateNumber];
                returnYES; }}else if(isJudgeMouth == YES && isShakeHead ! = YES) { self.textLabel.text = @"Please repeat the shaking of your head.";
            [self tomAnimationWithName:@"shakeHead" count:4];
            number = 0;
        }else{
            takePhotoNumber += 1;
            if (takePhotoNumber == 2) {
                [self timeBegin];
            }
        }
        isCrossBorder = NO;
    }
Copy the code

7. Determine the position and whether to open your mouth

    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; }}}Copy the code

8. Open your mouth and verify whether you shake your head

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]; }}Copy the code

9. Shake your head and take a photo with a 3-second countdown

if(timeCount >= 1)
    {
        self.textLabel.text = [NSString  stringWithFormat:@"Take photos after %ld s",(long)timeCount];
    }
    else
    {
        [theTimer invalidate];
        theTimer=nil;
        
        [self didClickTakePhoto];
    }
Copy the code

10. After taking the picture, choose to re-take or upload the picture

-(void)didClickPhotoAgain
{
    [self delateNumber];
    
    [self.previewLayer.session startRunning];
    self.textLabel.text = @"Please adjust your position.";
    
    [backView removeFromSuperview];
    
    isJudgeMouth = NO;
    isShakeHead = NO;
    
}
Copy the code

11. Select reshoot and repeat steps 5-9. Select Upload to call back the image data

- (void) didClickUpPhoto {/ / upload photos success [self. FaceDelegate sendFaceImage: imageView. Image]; [self.navigationController popViewControllerAnimated:YES]; }Copy the code

12. The clean data

-(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"];
}
Copy the code

Iv. Download and configuration of IFlytek SDK

1. Download the SDK

Since the iflytek face recognition SDK is used in the project, we need to create an application on the Open platform of Iflytek and download the SDK.

2. Add a system library

Add iflymsc. framework in the lib directory of the development kit to the project. Also add other libraries that you rely on in Demo to your project. Add the iOS libraries required by the SDK as shown in the following example:

3. Set the Bitcode

Just search for Bitcode in the Targets – Build Settings, find the corresponding option and set it to NO, as shown below:

4. Configure user privacy rights

Add the following Settings to info.plist:

Five: actual use of the project

1. Download the demo

Download the demo and import the FBYFaceData folder from the demo into the project.

2. Introduce FBYFaceRecognitionViewController in the project

#import "FBYFaceRecognitionViewController.h"
Copy the code

3. Add code to the click event of the item identification button

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

4. Picture callback function

-(void)sendFaceImage:(UIImage *)faceImage
{
    NSLog(@"Picture uploaded successfully");
}

- (void)sendFaceImageError {
    NSLog(@"Picture uploading failed");
}
Copy the code

This article demo source:

Source code Demo access method

Pay attention to [net development] wechat public number, reply [face recognition] can be received. Net the world method, convenient for you and ME to develop, all documents will continue to update, welcome to pay attention to grow together!

Hope to help you, if you have questions can add QQ technical exchange group: 668562416

If there is anything wrong or inadequate place, but also look for readers to make suggestions or suggestions

If you need to reprint, please contact me, after authorization can be reprinted, thank you

This post has been synchronized to my blog: FBY Zhan Fei


Welcome to pay attention to my public number: net development