php 百度ai人脸识别,小程序实现人脸识别功能(百度ai)

本文详细介绍了如何在小程序中实现人脸识别功能,包括获取百度API的AppID、APIKey和SecretKey来生成AccessToken,选择并调用人脸检测V2或V3接口,以及如何封装和调用识别方法。通过示例代码展示了整个流程,帮助开发者快速理解和应用人脸识别技术。
摘要由CSDN通过智能技术生成

本文介绍了小程序实现人脸识别功能,分享给大家,具体如下:

接入流程

1. 按照文档获取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);

}

})

})

}

2. 选择人脸识别-->人脸检测,人脸识别接口分为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);

}

})

})

}

3. 调用识别方法

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,

})

})

})

4. 结果如下:

77c371ce884d10741e4e8fc456c99106.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值