Android 超简单集成活体检测技术 快速识别“假脸”

前言

你有没有过这样的顾虑,刷脸解锁真的安全吗?如果有人用我的照片或者视频冒充我,那么手机可不可以发现镜头前不是我本人呢?当然可以啦。华为HMS ML Kit活体检测技术可以准确地分辨真实人脸和“假脸”。不管是人脸翻拍照片、人脸视频重放,还是人脸面具,活体检测技术都可以马上揭穿这些“假脸”,让“假脸”无所遁形!


应用场景

活体检测技术通常用在人脸比对技术前,先确认镜头前是真实的人而不是有人拿照片或面具作假,然后再比对当前人脸和已录入的人脸是否是同一个人。活体检测技术在生活中有广泛的应用场景。比如在手机解锁时,活体检测技术可以防止有人假冒自己解锁手机,造成个人信息泄露。


或者是在办理金融业务时,活体检测技术可以用于实名认证过程中,先判断当前是真实人脸,再比对当前人脸和身份证上照片信息,确认办理业务的是身份证上的本人,有效防止他人冒充自己造成财产损失。


并且,HMS ML Kit活体检测技术支持静默式活体检测,不需要用户配合做动作就可以判断是不是真实人脸,怎么样,是不是很方便呢。下面小编给大家介绍如何快速集成活体检测技术。

开发实战

[if !supportLists]1.     [endif]开发准备

详细的准备步骤可以参考华为开发者联盟:

https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/ml-process-4

这里列举关键的开发步骤。

[if !supportLists]1.1   [endif]项目级gradle里配置Maven仓地址

buildscript {


  repositories {


  ...


  maven {url 'https://developer.huawei.com/repo/'}


  }

}

 dependencies {

                ...


  classpath 'com.huawei.agconnect:agcp:1.3.1.300'


  }

allprojects {


  repositories {


  ...


  maven {url 'https://developer.huawei.com/repo/'}

    }

}

1.2 应用级gradle里配置SDK依赖

[if !supportLists]1.    

  [endif]dependencies{

[if !supportLists]2.    

  [endif]    //引入活体检测集合包。

[if !supportLists]3.    

  [endif]    implementation'com.huawei.hms:ml-computer-vision-livenessdetection:2.0.2.300'

[if !supportLists]4.    

  [endif]}

[if !supportLists]1.3 [endif]在文件头添加配置

[if !supportLists]1.    

  [endif]apply plugin: 'com.android.application'

[if !supportLists]2.    

  [endif]apply plugin: 'com.huawei.agconnect'

1.4 添加如下语句到AndroidManifest.xml文件中,自动更新机器学习模型到设备

[if !supportLists]1.    

  [endif]<meta-data

[if !supportLists]2.    

  [endif]android:name="com.huawei.hms.ml.DEPENDENCY"

[if !supportLists]3.    

  [endif]android:value= "livenessdetection"/>

[if !supportLists]1.5 [endif]申请相机权限

相机权限申请的具体操作步骤可参考https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/add-permissions-0000001050040051

[if !supportLists]2.     [endif]代码开发

2.1创建活体检测结果回调,用于获取检测结果。

[if !supportLists]1.    

  [endif]private MLLivenessCapture.Callback callback = new MLLivenessCapture.Callback() {

[if !supportLists]2.    

  [endif]@Override

[if !supportLists]3.    

  [endif]public void onSuccess(MLLivenessCaptureResult result) {

[if !supportLists]4.    

  [endif]//检测成功的处理逻辑,检测结果可能是活体或者非活体。

[if !supportLists]5.    

  [endif]}

[if !supportLists]6.    

  [endif] 

[if !supportLists]7.    

  [endif]@Override

[if !supportLists]8.    

  [endif]public void onFailure(int errorCode) {

[if !supportLists]9.    

  [endif]//检测未完成,如相机异常CAMERA_ERROR,添加失败的处理逻辑。

[if !supportLists]10.  [endif]}

[if !supportLists]11.  [endif]};

2.2 创建活体检测实例,启动检测。

[if !supportLists]1.    

  [endif]MLLivenessCapture capture = MLLivenessCapture.getInstance();

[if !supportLists]2.    

  [endif]capture.startDetect(activity, callback);

Demo效果

下面这个demo展示了镜头前分别是真实人脸和人脸面具时活体检测技术的检测结果,效果是不是很棒?


Github源码

https://github.com/HMS-Core/hms-ml-demo/blob/master/MLKit-Sample/module-body/src/main/java/com/mlkit/sample/activity/HumanLivenessDetectionActivity.java

更详细的开发指南参考华为开发者联盟官网

https://developer.huawei.com/consumer/cn/hms/huawei-mlkit

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容