集成百度人脸识别

项目需求:

基于人脸识别的实名认证方案

人证核验方案可以轻松改变以上问题,并在线完成认证流程:

  • 确保为真人:通过离在线双重活体检测,确保操作者为真人,可有效抵御彩打照片、视频、3D建模等攻击。用户无需提交任何资料,或去网点柜台办理业务,高效方便。
  • 确保为本人:基于「真人」的基础,将真人人脸图片与公安权威数据源身份信息库的人脸小图对比,确保操作者身份的真实性。避免身份证或人脸图像伪造等欺诈风险,权威可靠。

方案流程:

简单来说分为三个步骤:

1、身份证OCR识别

用户上传身份证照片,返回姓名+身份证号等字段信息,支持对翻拍、伪造等身份证件进行识别。

2、人脸采集及防攻击

离线采集SDK进行有动作活体验证,并输出高质量人脸图片。

3、调取公安接口验证

① 设置质量检测及活体检测参数进行二次过滤

② 调取权威数据源进行比对

此方案适用于原生APP(iOS、安卓)的实名认证、远程身份核验、人脸登录等场 景,通过两种活体能力的综合应用,可有效防止各类活体攻击,提升业务的安全性

 

 

首先,身份证OCR识别集成:

集成逻辑

2.1 IOS集成

1、打开或者新建一个项目。

2、右键点击项目,会出现一个添加菜单,在菜单中选择『Add Files to“此处是你的项目名字”…… 』,如下图所示:

img

3、在添加文件弹出框里面选择申请到的license和SDK添加进来。如下图:

注意:license为百度官方提供,刚才在后台下载的文件(文件名称:idl-license.face-ios)

SDK包含下面几个文件:

  • AipOcrSDK文件夹 
  • AipOcrSdk.framework
  • AipBase.framework
  • IdcardQuality.framework
  • aip.license

 

要注意AipOcrSDK文件夹也要拖进项目中去,不要会提示引用报错,

其次,确认下 Bundle Identifier 是否是申请license时填报的那一个.

注意:license 和 Bundle Identifier 为一一对应关系,填错了会导致SDK不可用.

4.在 FaceParameterConfig.h 文件中填写下面几项。

#ifndef FaceParameterConfig_h
#define FaceParameterConfig_h

// 如果在后台选择自动配置授权信息,下面的三个LICENSE相关的参数已经配置好了
// 只需配置FACE_API_KEY和FACE_SECRET_KEY两个参数即可

// 人脸license文件名
#define FACE_LICENSE_NAME    @"idl-license"

// 人脸license后缀
#define FACE_LICENSE_SUFFIX  @"face-ios"

// (您申请的应用名称(appname)+「-face-ios」后缀,如申请的应用名称(appname)为test123,则此处填写test123-face-ios)
// 在后台 -> 产品服务 -> 人脸识别 -> 客户端SDK管理查看,如果没有的话就新建一个
#define FACE_LICENSE_ID        @"HKT-face-ios"

#endif /* FaceParameterConfig_h */

5、选择链接C++标准库。

img

6、如果没有使用pod管理第三方库的话,请在Build Setting Linking Other Linker Flags 上面加入 –ObjC选项。如果用了pod请忽略,因为pod会自动添加上。

7.在TARGETS->General->Fr阿玛我人口数,Libraries,and Embedded Content 

8.把下载下来的License文件(文件名:aip.license),添加到项目里面,无需更改文件名称,然后在AppDelegate添加以下代码引用进去

   //    #error 【必须!】请在 ai.baidu.com中新建App, 绑定BundleId后,在此填写授权信息
        //    #error 【必须!】上传至AppStore前,请使用lipo移除AipBase.framework、AipOcrSdk.framework的模拟器架构,参考FAQ:ai.baidu.com/docs#/OCR-iOS-SDK/top
        //     授权方法1:在此处填写App的Api Key/Secret Key
        //    [[AipOcrService shardService] authWithAK:@"Api Key" andSK:@"Secret Key"];
        
        
        // 授权方法2(更安全): 下载授权文件,添加至资源
        NSString *licenseFile = [[NSBundle mainBundle] pathForResource:@"aip" ofType:@"license"];
        NSData *licenseFileData = [NSData dataWithContentsOfFile:licenseFile];
        if(!licenseFileData) {
            [UIAlertController showAlertCntrollerWithViewController:self alertControllerStyle:UIAlertControllerStyleAlert title:Localized(@"授权失败") message:Localized(@"授权失败")  CallBackBlock:^(NSInteger btnIndex) {
                if (btnIndex == 1) {
                }
            } cancelButtonTitle:Localized(@"确定") destructiveButtonTitle:nil otherButtonTitles:nil];
        }
        
        [[AipOcrService shardService] authWithLicenseFileData:licenseFileData];

9.通过API调用ViewController进行身份证扫描

#import <objc/runtime.h>
#import <AipOcrSdk/AipOcrSdk.h>


  
#pragma mark ===============OCR============================
    //1.身份证识别OCR
    - (void)setAipOcrService{
       
        [XSTool showProgressHUDTOView:self.view withText:@"身份证识别中,请稍后..."];
        
        [[AipOcrService shardService] authWithLicenseFileData:licenseFileData];
        //身份证正面识别
        @weakify(self);
        [[AipOcrService shardService] detectIdCardFrontFromImage:self.fontImage withOptions:@{} successHandler:^(id result) {
            @strongify(self);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [XSTool hideProgressHUDWithView:self.view];
                NSLog(@"识别结果%@", result);
                NSMutableString *message = [NSMutableString string];
                
                if(result[@"words_result"]){
                    self.cardnum = [NSString stringWithFormat:@"%@",result[@"log_id"]];
                    
                    if([result[@"words_result"] isKindOfClass:[NSDictionary class]]){
                        [result[@"words_result"] enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                            if([obj isKindOfClass:[NSDictionary class]] && [obj objectForKey:@"words"]){
                                [message appendFormat:@"%@: %@\n", key, obj[@"words"]];
                                if([((NSString *)key) isEqualToString:@"姓名"]){
                                    self.truename = obj[@"words"];
                                }
                                
                                if([((NSString *)key) isEqualToString:@"公民身份号码"]){
                                    self.cardnum = obj[@"words"];
                                }
                            }else{
                                [message appendFormat:@"%@: %@\n", key, obj];
                            }
                            
                        }];
                    }else if([result[@"words_result"] isKindOfClass:[NSArray class]]){
                        for(NSDictionary *obj in result[@"words_result"]){
                            if([obj isKindOfClass:[NSDictionary class]] && [obj objectForKey:@"words"]){
                                [message appendFormat:@"%@\n", obj[@"words"]];
                            }else{
                                [message appendFormat:@"%@\n", obj];
                            }
                            
                        }
                    }
                    
                }else{
                    [message appendFormat:@"%@", result];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    if (isEmptyString(self.truename) ||isEmptyString(self.cardnum)) {
                        [UIAlertController showAlertCntrollerWithViewController:self alertControllerStyle:UIAlertControllerStyleAlert title:Localized(@"提示") message:Localized(@"身份证识别识别,请重新上传!")  CallBackBlock:^(NSInteger btnIndex) {
                            
                        } cancelButtonTitle:Localized(@"确认") destructiveButtonTitle:nil otherButtonTitles:nil];
                        
                    }else{
                        //                  if ([self validateIDCardNumber:self.cardnum]) {
                        [self faceLiveness];
                        //                  }
                        
                    }
                });
                
                
            });
        } failHandler:^(NSError *err) {
            dispatch_async(dispatch_get_main_queue(), ^{
                Alert(@"身份证识别失败,请重试");
            });
        }];
        
        
    }
    

人脸离线采集SDK

1.客户端SDK需要 提交申请,流程分为以下几步:

  1. 完成企业认证(营业执照信息认证)
  2. 填写申请理由(填写联系方式、使用场景等)
  3. 等待审批(通常为1-5天内)
  4. 获取SDK使用权限

申请通过后,您即可下载SDK及官方提供的各场景的示例工程源码,如下图所示:

SDK的使用,涉及有效期,有效期过后,SDK本地功能将会不可用,SDK会自动向百度服务器请求拉取最新的授权license,设置好iOS包名,下载的SDK会自动配好FACE_LICENSE_ID,

引入项目中即可使用,

2.导入Demo中的以下文件,注意如果需要百度自带的识别界面,需要把对应的UI文件也拖进项目中

  • IDLFaceSDK.framework
  • com.baidu.idl.face.faceSDK.bundle
  • com.baidu.idl.face.model.bundle
  • com.baidu.idl.face.live.action.image.bundle

在AppDelegate中设置

  NSString* licensePath = [NSString stringWithFormat:@"%@.%@", FACE_LICENSE_NAME, FACE_LICENSE_SUFFIX ];
    [[FaceSDKManager sharedInstance] setLicenseID:FACE_LICENSE_ID andLocalLicenceFile:licensePath andRemoteAuthorize:false];
    NSLog(@"canWork = %d",[[FaceSDKManager sharedInstance] canWork]);

viewController初始化


#pragma mark - UI Action
- (void) initSDK {
    
    if (![[FaceSDKManager sharedInstance] canWork]){
        NSLog(@"授权失败,请检测ID 和 授权文件是否可用");
        return;
    }
    
    // 初始化SDK配置参数,可使用默认配置
    // 设置最小检测人脸阈值
    [[FaceSDKManager sharedInstance] setMinFaceSize:200];
    // 设置截取人脸图片高
    [[FaceSDKManager sharedInstance] setCropFaceSizeWidth:400];
    // 设置截取人脸图片宽
    [[FaceSDKManager sharedInstance] setCropFaceSizeHeight:640];
    // 设置人脸遮挡阀值
    [[FaceSDKManager sharedInstance] setOccluThreshold:0.5];
    // 设置亮度阀值
    [[FaceSDKManager sharedInstance] setIllumThreshold:40];
    // 设置图像模糊阀值
    [[FaceSDKManager sharedInstance] setBlurThreshold:0.3];
    // 设置头部姿态角度
    [[FaceSDKManager sharedInstance] setEulurAngleThrPitch:10 yaw:10 roll:10];
    // 设置人脸检测精度阀值
    [[FaceSDKManager sharedInstance] setNotFaceThreshold:0.6];
    // 设置抠图的缩放倍数
    [[FaceSDKManager sharedInstance] setCropEnlargeRatio:3.0];
    // 设置照片采集张数
    [[FaceSDKManager sharedInstance] setMaxCropImageNum:6];
    // 设置超时时间
    [[FaceSDKManager sharedInstance] setConditionTimeout:15];
    // 设置开启口罩检测,非动作活体检测可以采集戴口罩图片
    [[FaceSDKManager sharedInstance] setIsCheckMouthMask:true];
    // 设置开启口罩检测情况下,非动作活体检测口罩过滤阈值,默认0.8 不需要修改
    [[FaceSDKManager sharedInstance] setMouthMaskThreshold:0.8f];
    // 设置原始图缩放比例
    [[FaceSDKManager sharedInstance] setImageWithScale:0.8f];
    // 设置图片加密类型,type=0 基于base64 加密;type=1 基于百度安全算法加密
    [[FaceSDKManager sharedInstance] setImageEncrypteType:0];
    // 初始化SDK功能函数
    [[FaceSDKManager sharedInstance] initCollect];
    
    
}

- (void)initLivenesswithList {
    // 默认活体检测打开,顺序执行
    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLiveEye)];
    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLiveMouth)];
    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLiveYawRight)];
    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLiveYawLeft)];
    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLivePitchUp)];
    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLivePitchDown)];
    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLiveYaw)];
    BDFaceLivingConfigModel.sharedInstance.isByOrder = YES;
    BDFaceLivingConfigModel.sharedInstance.numOfLiveness = 7;
}

- (void) destorySDK{
    // 销毁SDK功能函数
    [[FaceSDKManager sharedInstance] uninitCollect];
}

点击跳转到人脸活体验证界面,获取到检测图片后,上传给后台,调用公安验证API

 //2.人脸采集-活体检测
    - (void)faceLiveness {
        
        BDFaceLivenessViewController* lvc = [[BDFaceLivenessViewController alloc] init];
        BDFaceLivingConfigModel* model = [BDFaceLivingConfigModel sharedInstance];
        [lvc livenesswithList:model.liveActionArray order:model.isByOrder numberOfLiveness:model.numOfLiveness];
        __weak typeof(self) wSelf = self;
        lvc.faceImageBlock = ^(UIImage *originalImage) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (originalImage) {
                    [self.imgArray addObject:originalImage];
                    [wSelf upLoadImageWithImgArr];
                    
                }else{
                    [UIAlertController showAlertCntrollerWithViewController:self alertControllerStyle:UIAlertControllerStyleAlert title:Localized(@"提示") message:Localized(@"人脸采集失败,请重试")  CallBackBlock:^(NSInteger btnIndex) {
                        
                    } cancelButtonTitle:Localized(@"确认") destructiveButtonTitle:nil otherButtonTitles:nil];
                }
            });
        };
        
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:lvc];
        navi.navigationBarHidden = true;
        navi.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:navi animated:YES completion:nil];
    }

/*
SDK提供了人脸活体验证后返回扣图图片,供项目使用

/**
 *  人脸活体验证,成功之后返回扣图图片,原始图片
 * @param image 镜头拿到的图片
 * @param previewRect 预览的Rect
 * @param detectRect 检测的Rect
 * return completion 回调信息
 */
-(void) livenessNormalWithImage:(UIImage *)image previewRect:(CGRect)previewRect detectRect:(CGRect)detectRect completionHandler:(LivenessNormalCompletion)completion;

*/

 

OK,至此百度人脸识别搞定,代码中需要的文件放在这里需要的自提:

BaiDu_FACE

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值