微信小程序> 小程序实现人脸识别功能

小程序实现人脸识别功能

浏览量:473 时间: 来源:weixin_33726318

接入流程

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'   },欢迎加入全栈开发交流划水交流圈:582735936面向划水1-3年前端人员帮助突破划水瓶颈,提升思维能力   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    })欢迎加入全栈开发交流划水交流圈:582735936面向划水1-3年前端人员帮助突破划水瓶颈,提升思维能力    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.

结果如下:
小程序

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎