- 本文主要介紹基于百度云人臉識(shí)別接口實(shí)現(xiàn)微信小程序人臉?biāo)阉?/li>
- 開發(fā)前的準(zhǔn)備:
- 參考文檔:
- 在寫小程序端的代碼前墓律,還需要去百度ai平臺(tái)創(chuàng)建相關(guān)應(yīng)用實(shí)例,不清楚的讀者可以參考我之前的微信小程序?qū)崿F(xiàn)圖像搜索的文章幔亥,當(dāng)然耻讽,建議直接看官方文檔,官方文檔寫得很詳細(xì)帕棉。
- 代碼:
1针肥、Wxml:
<view class="container">
<!-- 相機(jī)組件 -->
<camera
class="camera"
device-position="front"
flash="off"
binderror="error"
></camera>
<view class="button_container">
<!-- 按鈕組件,點(diǎn)擊上傳人臉庫(kù) -->
<button class="button"
bindtap="takePhoto">上傳人臉庫(kù) </button>
<!-- 按鈕組件香伴,進(jìn)行人臉識(shí)別 -->
<button class="button"
bindtap="check">人臉庫(kù)識(shí)別 </button>
</view>
</view>
2慰枕、Wxss:
page {
background: white;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.button_container {
margin-top: 600rpx;
display: flex;
flex-direction: column;
}
.camera {
width: 300rpx;
height: 300rpx;
border-radius: 50%;
position: fixed;
top: 100rpx;
}
.button {
margin-top: 20rpx;
width: 100rpx;
height: 60rpx;
background: forestgreen;
color: white;
}
3、Javascript:
// camera.js
Page({
data: {
src: "",
token: "",
base64: "",
msg: "",
},
//上傳至人臉庫(kù)
upload(){
this.takePhoto();
this.faceUpload();
},
//進(jìn)行人臉識(shí)別
check(){
this.takePhoto();
this.faceRecognition();
},
//拍照
takePhoto() {
var that = this;
//創(chuàng)建拍照上下文對(duì)象
const ctx = wx.createCameraContext()
ctx.takePhoto({
quality: 'high',
//拍照成功
success: (res) => {
console.log(res)
wx.getFileSystemManager().readFile({
filePath: res.tempImagePath,
encoding: 'base64',
success: res => {
console.log(res)
this.setData({
base64: res.data
})
},
//拍照失敗
fail: err => {
console.log(err)
this.showToast("調(diào)用失敗,請(qǐng)稍后重試");
}
})
},
fail: err => {
this.showToast("調(diào)用失敗,請(qǐng)稍后重試");
}
})
},
//人臉上傳
faceUpload(group_id, user_id) {
//調(diào)用接口即纲,獲取token
wx.request({
url: 'https://aip.baidubce.com/oauth/2.0/token', //百度智能云相關(guān)的接口地址
data: {
grant_type: 'client_credentials',
client_id: 'xxxxxxxxxxxxxxxx',//用你創(chuàng)建的應(yīng)用的API Key
client_secret: 'xxxxxxxxxxxxxxxx'//用你創(chuàng)建的應(yīng)用的Secret Key
},
header: {
'Content-Type': 'application/json' // 默認(rèn)值
},
//獲取到之后具帮,請(qǐng)求接口,向人臉庫(kù)添加照片
success: res => {
console.log(res)
wx.request({
url: 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token=' + res.data.access_token,
method: 'POST',
data: {
image: this.data.base64,
image_type: 'BASE64',
group_id:'xxx',//用戶組的id
user_id:"xxxx"http://給用戶指定的id
},
header: {
'Content-Type': 'application/json'
},
success: res => {
// console.log(res)
if (res.data.error_msg == 'SUCCESS') {
wx.hideLoading();
this.showToast("上傳成功");
} else {
this.showToast("上傳失敗低斋,請(qǐng)稍后重試");
}
},
fail(err) {
this.showToast("上傳失敗蜂厅,請(qǐng)稍后重試");
}
})
},
fail(err) {
// console.log(err)
this.showToast("上傳失敗,請(qǐng)稍后重試");
}
})
},
//人臉識(shí)別
faceRecognition( group_id_list) {
var that = this;
wx.showLoading({
title: '識(shí)別中',
})
//獲取token
wx.request({
url: 'https://aip.baidubce.com/oauth/2.0/token', //真實(shí)的接口地址
data: {
grant_type: 'client_credentials',
client_id: 'xxxxxxxxxxxxxx',
client_secret: 'xxxxxxxxxxxxxxxx'
},
header: {
'Content-Type': 'application/json'
},
//有了token之后膊畴,進(jìn)行人臉識(shí)別
success: res => {
wx.request({
url: 'https://aip.baidubce.com/rest/2.0/face/v3/search?access_token=' + res.data.access_token,
method: 'POST',
data: {
image: that.data.base64,
image_type: 'BASE64',
group_id_list: 'xxxx'//你創(chuàng)建的應(yīng)用的用戶組id
},
header: {
'Content-Type': 'application/json'
},
success: res => {
wx.hideLoading();
console.log(res)
that.setData({
msg: parseInt(res.data.result.user_list[0].score)
})
if (that.data.msg > 80) {
let title = "識(shí)別通過,匹配率:" + this.data.msg + "%";
this.showToast(title)
} else {
let title = "識(shí)別未通過,匹配率:" + this.data.msg + "%";
this.showToast(title)
}
},
fail: err => {
wx.hideLoading();
this.showToast("調(diào)用失敗,請(qǐng)稍后重試")
}
});
}
})
},
//封裝的wx.showToast
showToast(title) {
wx.showToast({
title: title,
icon: 'none',
duration: 3000
})
}
})
請(qǐng)求中body中相關(guān)的參數(shù)這里就不一一說明了掘猿,詳情請(qǐng)閱讀官方文檔。
本文只介紹了簡(jiǎn)單的人臉?biāo)阉鲗?shí)現(xiàn)方法巴比,由于功力有限术奖,還望多多指教。