微信小程序调用百度人脸识别api读取云存储中的fileID进行人脸识别获取face_token
时间: 2024-06-09 19:11:31 浏览: 17
首先,你需要在百度AI开放平台注册账号并创建一个人脸识别应用,获取API Key和Secret Key。
然后,在微信小程序中使用wx.cloud.downloadFile()方法下载云存储中的文件,并获取到文件的临时路径。
接着,将文件的临时路径作为参数,调用百度人脸识别API中的人脸检测接口,获取到人脸位置信息。
最后,将人脸位置信息作为参数,调用百度人脸识别API中的人脸注册接口,获取到face_token。具体实现过程如下:
1. 下载云存储中的文件:
```javascript
wx.cloud.downloadFile({
fileID: 'your fileID',
success: res => {
const filePath = res.tempFilePath
// 调用人脸识别API
detectFace(filePath)
},
fail: err => {
console.error(err)
}
})
```
2. 调用人脸检测接口,获取人脸位置信息:
```javascript
function detectFace(filePath) {
wx.showLoading({
title: '正在检测中...',
mask: true
})
wx.request({
url: 'https://aip.baidubce.com/rest/2.0/face/v3/detect',
data: {
image: wx.getFileSystemManager().readFileSync(filePath, 'base64'),
image_type: 'BASE64',
face_field: 'location'
},
header: {
'Content-Type': 'application/json'
},
method: 'POST',
success: res => {
wx.hideLoading()
if (res.data.error_code) {
wx.showToast({
title: res.data.error_msg,
icon: 'none'
})
return
}
const faceToken = res.data.result.face_list[0].face_token
// 将faceToken保存到数据库或其他地方
},
fail: err => {
wx.hideLoading()
console.error(err)
}
})
}
```
3. 调用人脸注册接口,获取face_token:
```javascript
function registerFace(faceLocation) {
wx.showLoading({
title: '正在注册中...',
mask: true
})
wx.request({
url: 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add',
data: {
image: wx.getFileSystemManager().readFileSync(filePath, 'base64'),
image_type: 'BASE64',
group_id: 'your group_id',
user_id: 'your user_id',
face_token: faceToken,
liveness_control: 'HIGH',
action_type: 'REPLACE'
},
header: {
'Content-Type': 'application/json'
},
method: 'POST',
success: res => {
wx.hideLoading()
if (res.data.error_code) {
wx.showToast({
title: res.data.error_msg,
icon: 'none'
})
return
}
const faceToken = res.data.result.face_token
// 将faceToken保存到数据库或其他地方
},
fail: err => {
wx.hideLoading()
console.error(err)
}
})
}
```
注意:在调用人脸识别API时,需要将API Key和Secret Key加入到请求头中,并且需要将图片转换成base64格式。同时,需要注意API中各个参数的含义和使用方法,具体可以参考百度AI开放平台的文档。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)