【微信小程序+Python实现人脸识别】

2022-3-18

方法一:使用极速API提供的人脸识别API 极速API 教程

免费次数只有五次。。


方法二:使用腾讯云人脸识别中的人脸对比 SDK 每月免费1W

使用SDK文档中的接口调试功能,然后那里可以生成代码,部署到python后端以及相应接口即可。注意这里要将tencent-sdk-python包升级到最新版本

  • 小程序端要提供两个图片的BASE64码
  • Base64码转换源码(末尾是wxml显示base64格式图片代码)
  • 转化源码来自B站刘江艳紫
function getPicFromcenavm(ctx){
  return new Promise(function(resolve,reject){
    ctx.takePhoto({
      quality:'low',
      success:(res)=>{
        //resolve(res.tempImagePath)
        getBase64(res.tempImagePath).then(function(res2){
          resolve(res2.data);
        });
      }
    })
  })

  function getBase64(tempImagePath){
    return new Promise(function (resolve,reject){
      wx.getFileSystemManager().readFile({
        filePath:tempImagePath,
        encoding:'base64',
        success:function(data){
          resolve(data);
        }
      })
    })
  }
}
module.exports = {
  getPicFromcenavm:getPicFromcenavm
}
  • 前端显示base64格式图片
<image src="data:image/png;base64,{{ImageA}}"/>
  • 前端调用后端接口实现人脸登录

注意这里的header在post请求下是必须要的,具体可以看微信的官方文档wx.request

wx.request({
      url: api.Face,
      data:{
        ImageA:this.data.ImageA,
        ImageB:this.data.ImageB
      },
      method: 'POST',
      dataType: 'json',
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(x){
        console.log(x)
      }
    })
  • 腾讯云人脸识别–Python实现

要先下载腾讯云的包 pip3 install tencentcloud-sdk-python

import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.iai.v20200303 import iai_client, models

from utils.response import FaceResponse

def check_face(ImageA, ImageB):
    response = FaceResponse()
    try:
        cred = credential.Credential("AKID1sUqvkJbeQv96mBJAk5Azf8j4A0ZsteB", "IK5ZmkU3C9rLZXFivsa3GDuLXTAbniNm")

        httpProfile = HttpProfile()
        httpProfile.endpoint = "iai.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = iai_client.IaiClient(cred, "ap-guangzhou", clientProfile)

        req = models.CompareFaceRequest()
        params = {
            "ImageA":ImageA,
            "ImageB":ImageB,
            "NeedRotateDetection": 1
        }
        req.from_json_string(json.dumps(params))

        resp = client.CompareFace(req)
        response.Score = resp.Score
        if(response.Score < 90):
            response.status = False
            response.code = "相似度小于90"
        else:
            response.status = True


    except TencentCloudSDKException as err:
        response.status = False
        response.code = err.code
    return response

这些都是之前开发时候的一些学习记录
本人菜鸟一枚,有错误的地方还望海涵~

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值