网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/10/26 浏览:3 次 )

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

文档中心:https://ai.baidu.com/docs#/Begin/a2bbf4b2

接入流程

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"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"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. 结果如下:

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

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

上一篇:vue-router启用history模式下的开发及非根目录部署方法
下一篇:
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap