微信小程序人脸识别接口用法介绍(人脸识别的最终解决方案)

一、接口介绍

微信小程序人脸识别接口是一种基于微信原生组件的API,可以在小程序中调用该接口实现人脸识别功能。该接口可以根据用户的输入,识别图片中的人脸特征,将相似程度与模板库中的人脸特征相比较,实现对人脸的识别和比对。

调用该接口需要先获取小程序的调用接口令牌,然后将回调地址添加到小程序中,再在开发环境中进行接口配置、调试和测试。

二、接口使用步骤

以下是使用微信小程序人脸识别接口的几个关键步骤:

1、在小程序管理后台的接口管理中开通微信小程序人脸识别接口,并获取接口权限;

2、在小程序中调用wx.chooseImage等API上传图片,并获取上传后的本地图片文件路径;

3、将本地图片文件路径转换成base64格式,并传给接口调用方法;

4、接口返回比对结果,根据结果给用户返回相应的信息。

三、代码示例

以下是微信小程序人脸识别接口的代码示例:

wx.chooseImage({
  count: 1,
  sizeType: ['original', 'compressed'],
  sourceType: ['album'],
  success(res) {
    const tempFilePaths = res.tempFilePaths
    wx.getFileSystemManager().readFile({
      filePath: tempFilePaths[0],
      encoding: "base64",
      success(response) {
        let base64 = "data:image/jpeg;base64," + response.data
        wx.request({
          url: 'https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=xxxxx&type=image',
          data: {
            "media": base64
          },
          method: "POST",
          success(res) {
            let face_token = res.data['face_tokens'][0]
            let api_key = 'xxxxx'
            let api_secret = 'xxxxxx'
            let faceid_url = 'https://api-cn.faceplusplus.com/facepp/v3/search'
            wx.request({
              url: faceid_url,
              data: {
                "api_key": api_key,
                "api_secret": api_secret,
                "face_token": face_token,
                "outer_id": "xxxxxx"
              },
              method: "POST",
              success(res) {
                let results = res.data['results'][0]
                if (results !== undefined) {
                  wx.showToast({
                    title: '人脸识别成功',
                    icon: 'success',
                    duration: 2000
                  })
                } else {
                  wx.showToast({
                    title: '人脸识别失败',
                    icon: 'fail',
                    duration: 2000
                  })
                }
              }
            })
          }
        })
      }
    })
  }
})

四、接口使用限制

使用微信小程序人脸识别接口还需要注意以下几点限制:

1、接口调用次数限制:每个小程序每天最多调用10000次;

2、接口使用时间限制:每天07:00~23:00可以使用该接口;

3、接口响应时间限制:40ms以内的接口响应时间是最佳的。

五、总结

本文对微信小程序人脸识别接口进行了详细的介绍,对该接口的使用步骤、代码示例和使用限制进行了解析,希望能够对开发者们有所帮助。

Published by

风君子

独自遨游何稽首 揭天掀地慰生平