最近开发的时候遇到了一个需求 需要扫描照片里是否有人脸 历经千辛万苦找到了百度智能云,最终决定用百度智能云的人脸检测功能 首先去百度智能云注册账号(个人企业账号都可)
注册完以后领取资源 创建应用
接下来上代码
//通过申请到的应用API Key和Secret Key 获取access_token
Taro.request({
url: `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[API Key]&client_secret=[Secret Key]
`,
method: "POST",
success: (res) => {
Taro.request({
url: `https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=${res.data.access_token}`,
method: "POST",
header: {
'Content-Type': 'application/json'
},
//这里注意由于百度智能云请求的时候要base64编码 微信小程序有的东西不支持 我直接用的微信小程序自带的api转的
data: JSON.stringify({
"image": Taro.getFileSystemManager().readFileSync(file.url, "base64"),
"image_type": "BASE64"
}),
success: (r) => {
console.log("R", r.data.error_msg);
if (r.data.error_msg == "SUCCESS") {
Toast.show("人脸验证成功");
//上传
Taro.uploadFile({
url: AppConsts.uploadUrl + "/file/upload",
filePath: file.url,
name: "files",
formData: {
user: "test",
},
success(res) {
let datearr = JSON.parse(res.data);
console.log("dad", datearr.data.fileUrl);
setFileUrl(datearr.data.fileUrl)
},
});
} else {
Toast.show("验证失败,请重新选择照片")
}
}
})
}
})
到这里就结束啦~
欢迎大佬们多多指正~~

1048

被折叠的 条评论
为什么被折叠?



