资讯专栏INFORMATION COLUMN

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

yeyan1996 / 826人阅读

摘要:文档中心接入流程按照文档获取,进行用户身份验证和授权的凭证的生成自行获取网络错误,请重试选择人脸识别人脸检测,人脸识别接口分为和两个版本,确认在百度云后台获得的是还是版本接口权限。

文档中心: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?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. 结果如下:

哼~一点都不准

原文地址:https://github.com/liujianxi/...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/100303.html

相关文章

  • 利用百度ai进行人脸识别,以及传参时遇到的问题

    摘要:安装推荐安装好,执行新建是人脸识别的客户端,为使用人脸识别的开发人员提供了一系列的交互方法。参考如下代码新建一个你的你的你的你的调用人脸检测这里仅以人脸检测为例,其他都大同小异。百度文档中提到,图片信息总数据。 安装SDK 推荐安装好pip,执行pip install baidu-aip 新建AipFace AipFace是人脸识别的Python SDK客户端,为使用人脸识别的开发人员...

    bbbbbb 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<