文件中心:https://ai.baidu.com/docs#/Begin/a2bbf4b2
複製程式碼
接入流程
- 按照文件獲取AppID、API Key、Secret Key,進行Access Token(使用者身份驗證和授權的憑證)的生成
const getBaiduToken = function () {
return new Promise((resolve, reject) => {
//自行獲取APIKey、SecretKey
const apiKey = APIKey;
const secKey = SecretKey;
const tokenUrl = `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=${apiKey}&client_secret=${secKey}`;
wx.request({
url: tokenUrl,
method: 'POST',
dataType: "json",
header: {
'content-type': 'application/json; charset=UTF-8'
},
success: function (res) {
resolve(res);
},
fail: function (res) {
wx.hideLoading();
wx.showToast({
title: '網路錯誤,請重試!',
icon: 'none',
duration: 2000
})
reject(res);
},
complete: function (res) {
resolve(res);
}
})
})
}
複製程式碼
- 選擇人臉識別-->人臉檢測,人臉識別介面分為V2和V3兩個版本,確認在百度雲後臺獲得的是V2還是v3版本介面許可權。
//封裝識別方法
const getImgIdentify = function(tokenUrl, data){
return new Promise((resolve, reject) => {
const detectUrl = `https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=${tokenUrl}`;
wx.request({
url: detectUrl,
data: data,
method: 'POST',
dataType: "json",
header: {
'content-type': 'Content-Type:application/json; charset=UTF-8'
},
success: function (res) {
resolve(res);
},
fail: function (res) {
wx.hideLoading();
wx.showToast({
title: '網路錯誤,請重試!',
icon: 'none',
duration: 2000
})
reject(res);
},
complete: function (res) {
resolve(res);
}
})
})
}
複製程式碼
- 呼叫識別方法
getBaiduToken().then((res) => {
let token = res.data.access_token;
let data = {
"image": self.data.img,
"image_type":"URL",
"face_field":"ge,beauty,expression,face_shape,gender,glasses,landmark,race,quality,eye_status,emotion,face_type"
}
util.getImgIdentify(token, data).then((res)=>{
//百度介面返回的結果
let score = parseInt(res.data.result.face_list[0].beauty);
self.setData({
score: score,
})
})
})
複製程式碼
- 結果如下:
哼~一點都不準