【本文出自APICloud官方論壇,感謝鮑永道的分享。】
首先介紹下百度人臉識別模組(baiduFaceRec):
baiduFaceRec模組封裝了百度AI人臉識別功能,使用此模組可實現百度人臉檢測(包括age,beauty,expression,faceshape,gender,glasses,landmark,race,quality,facetype資訊)、人臉對比功能(比對兩張圖片中人臉的相似度,並返回相似度分值)。暫僅支援 android 平臺。
不囉嗦,直接上程式碼:
<!DOCTYPE html>
<html xmlns=”http://www.w3.org/1999/html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport"
content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
<title>frame2</title>
<link rel="stylesheet" href="../css/api.css">
<link rel="stylesheet" href="../css/aui.css">
<style>
html, body {
background: #ffffff;
}
.my-card {
border: solid 1px #dddddd;
margin: 10px;
}
.aui-btn-block {
margin-bottom: 10px;
}
</style>
</head>
<body>
<section class=”aui-content-padded my-card”>
<div class="aui-card-list">
<div class="aui-card-list-header">
百度人臉識別(V3版本)自定義模組
</div>
<div class="aui-card-list-content-padded">
人臉識別
</div>
<div class="aui-card-list-footer">
2018-06-03
</div>
</div>
</section>
<div class=”aui-content-padded”>
<p>
<div class="aui-btn aui-btn-info aui-btn-block">獲取access_token</div>
</p>
<p>
<div class="aui-btn aui-btn-info aui-btn-block">人臉檢測</div>
</p>
<p>
<div class="aui-btn aui-btn-info aui-btn-block">人臉對比</div>
</p>
</div>
</body>
</html>
<script src=”../script/api.js”></script>
<script>
var baiduFaceRec = null;
var UIAlbumBrowser = null;
apiready = function () {
baiduFaceRec = api.require(`baiduFaceRec`);
UIAlbumBrowser = api.require(`UIAlbumBrowser`);
};
//獲取access_token
function getAuth() {
var params = {
ak: `your ak`,
sk: `your sk`
};
baiduFaceRec.getAuth(params, function (ret, err) {
if (ret) {
console.log(JSON.stringify(ret));
alert(`access_token=` + ret.access_token);
} else {
console.log(err.msg);
alert(`錯誤資訊:` + err.msg);
}
})
}
//人臉檢測
function detect() {
//先獲取access_token
var params = {
ak: `your ak`,
sk: `your sk`
};
baiduFaceRec.getAuth(params, function (ret, err) {
if (ret) {
console.log(JSON.stringify(ret));
var access_token = ret.access_token;
//選擇照片或拍照
api.actionSheet({
title: `選擇照片`,
cancelTitle: `取消`,
buttons: [`拍照`, `手機相簿`]
}, function (ret, err) {
if (ret) {
console.log(ret.buttonIndex);
if (ret.buttonIndex != 3) {
var sourceType = ret.buttonIndex;
//獲取圖片
api.getPicture({
sourceType: (sourceType == 1) ? `camera` : `album`,
encodingType: `jpg`,
mediaValue: `pic`,
destinationType: `url`,
allowEdit: true,
saveToPhotoAlbum: false
}, function (ret, err) {
if (ret) {
console.log(ret.data);
var filePath = ret.data;
var params = {
filePath: filePath,
access_token: access_token
};
//人臉檢測
baiduFaceRec.detect(params, function (ret, err) {
if (ret) {
console.log(JSON.stringify(ret));
alert(`人臉檢測資料` + JSON.stringify(ret.result.face_list));
} else {
console.log(err.msg);
}
})
} else {
console.log(JSON.stringify(err));
alert(JSON.stringify(err));
}
})
} else {
return false;
}
}
});
} else {
console.log(err.msg);
alert(`錯誤:` + ret.msg);
}
});
}
//人臉對比
function match() {
//先獲取access_token
var params = {
ak: `your ak`,
sk: `your sk`
};
baiduFaceRec.getAuth(params, function (ret, err) {
if (ret) {
console.log(JSON.stringify(ret));
var access_token = ret.access_token;
//得到對比圖片
UIAlbumBrowser.open({
max: 2,
styles: {
bg: `#fff`,
mark: {
icon: ``,
position: `bottom_left`,
size: 20
},
nav: {
bg: `rgba(0,0,0,0.6)`,
titleColor: `#fff`,
titleSize: 18,
cancelColor: `#fff`,
cancelSize: 16,
finishColor: `#fff`,
finishSize: 16
}
},
rotation: true
}, function (ret) {
if (ret) {
var filePath1 = ret.list[0].path;
var filePath2 = ret.list[1].path;
var params = {
filePath1: filePath1,
filePath2: filePath2,
access_token: access_token
};
//人臉對比
baiduFaceRec.match(params, function (ret, err) {
if (ret) {
console.log(JSON.stringify(ret));
alert(`人臉檢測資料` + JSON.stringify(ret));
} else {
console.log(err.msg);
}
})
}
});
} else {
console.log(err.msg);
alert(`錯誤:` + ret.msg);
}
});
}
</script>
使用模組前需要先到百度AI開發者中心建立應用,獲取ak和sk,然後進行身份驗證,獲取返回的access_token,建議每次進行人臉識別介面時先獲取access_token(30期限),然後每次請求識別介面也傳入access_token,這樣保證每次都請求ok。
另外的兩個人臉識別介面,一個是人臉識別,一個是人臉對比。
人臉識別主要是識別人的臉部相關引數,對應的引數很多,我就不一一說明了,文件有詳細說明。另外就是人臉對比,對比兩張臉的相似度值,可以根據相似度值來判斷兩張人臉是否是同一個人,在專案上應用於人臉對比驗證,應該會使用的比較多。