前言
你有沒(méi)有過(guò)這樣的顧慮,刷臉解鎖真的安全嗎?如果有人用我的照片或者視頻冒充我,那么手機(jī)可不可以發(fā)現(xiàn)鏡頭前不是我本人呢?當(dāng)然可以啦。華為HMS ML Kit活體檢測(cè)技術(shù)可以準(zhǔn)確地分辨真實(shí)人臉和“假臉”。不管是人臉?lè)恼掌?、人臉視頻重放,還是人臉面具,活體檢測(cè)技術(shù)都可以馬上揭穿這些“假臉”,讓“假臉”無(wú)所遁形!

應(yīng)用場(chǎng)景
活體檢測(cè)技術(shù)通常用在人臉比對(duì)技術(shù)前,先確認(rèn)鏡頭前是真實(shí)的人而不是有人拿照片或面具作假,然后再比對(duì)當(dāng)前人臉和已錄入的人臉是否是同一個(gè)人?;铙w檢測(cè)技術(shù)在生活中有廣泛的應(yīng)用場(chǎng)景。比如在手機(jī)解鎖時(shí),活體檢測(cè)技術(shù)可以防止有人假冒自己解鎖手機(jī),造成個(gè)人信息泄露。

或者是在辦理金融業(yè)務(wù)時(shí),活體檢測(cè)技術(shù)可以用于實(shí)名認(rèn)證過(guò)程中,先判斷當(dāng)前是真實(shí)人臉,再比對(duì)當(dāng)前人臉和身份證上照片信息,確認(rèn)辦理業(yè)務(wù)的是身份證上的本人,有效防止他人冒充自己造成財(cái)產(chǎn)損失。

并且,HMS ML Kit活體檢測(cè)技術(shù)支持靜默式活體檢測(cè),不需要用戶配合做動(dòng)作就可以判斷是不是真實(shí)人臉,怎么樣,是不是很方便呢。下面小編給大家介紹如何快速集成活體檢測(cè)技術(shù)。
開(kāi)發(fā)實(shí)戰(zhàn)
[if !supportLists]1.???? [endif]開(kāi)發(fā)準(zhǔn)備
詳細(xì)的準(zhǔn)備步驟可以參考華為開(kāi)發(fā)者聯(lián)盟:
https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/ml-process-4
這里列舉關(guān)鍵的開(kāi)發(fā)步驟。
[if !supportLists]1.1???[endif]項(xiàng)目級(jí)gradle里配置Maven倉(cāng)地址
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 應(yīng)用級(jí)gradle里配置SDK依賴
[if !supportLists]1.????
? [endif]dependencies{
[if !supportLists]2.????
? [endif]????//引入活體檢測(cè)集合包。
[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 添加如下語(yǔ)句到AndroidManifest.xml文件中,自動(dòng)更新機(jī)器學(xué)習(xí)模型到設(shè)備
[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]申請(qǐng)相機(jī)權(quán)限
相機(jī)權(quán)限申請(qǐng)的具體操作步驟可參考https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/add-permissions-0000001050040051
[if !supportLists]2.???? [endif]代碼開(kāi)發(fā)
2.1創(chuàng)建活體檢測(cè)結(jié)果回調(diào),用于獲取檢測(cè)結(jié)果。
[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]//檢測(cè)成功的處理邏輯,檢測(cè)結(jié)果可能是活體或者非活體。
[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]//檢測(cè)未完成,如相機(jī)異常CAMERA_ERROR,添加失敗的處理邏輯。
[if !supportLists]10.? [endif]}
[if !supportLists]11.? [endif]};
2.2 創(chuàng)建活體檢測(cè)實(shí)例,啟動(dòng)檢測(cè)。
[if !supportLists]1.????
? [endif]MLLivenessCapture capture = MLLivenessCapture.getInstance();
[if !supportLists]2.????
? [endif]capture.startDetect(activity, callback);
Demo效果
下面這個(gè)demo展示了鏡頭前分別是真實(shí)人臉和人臉面具時(shí)活體檢測(cè)技術(shù)的檢測(cè)結(jié)果,效果是不是很棒?

Github源碼
更詳細(xì)的開(kāi)發(fā)指南參考華為開(kāi)發(fā)者聯(lián)盟官網(wǎng)