小程序开发教程栏目介绍不一样的实现人脸识别方法
相关免费学习:小程序开发教程
一 准备好百度云的开发者账号
登录进入控制台人工智能------图像识别创建应用
获取接口需要的参数
查看官网API文档
二 页面布局
文件ai.wxml:
<view class="c1"> <view class="c1-1"> </view> <button type="primary" size="mini" bindtap="chooseImage">选择图片</button> <view class="c1-2"> <image src="{{img}}" mode="widthFix"></image> <text>颜值:{{face.beauty}}</text> <text>年龄:{{face.age}}</text> <text>性别:{{face.gender.type}}</text> <text>情绪:{{face.emotion.type}}</text> </view> </view>
编写样式文件ai.wxss
.c1{ padding: 50rpx; } .c1-1{ height: 800rpx; margin-bottom: 20rpx; display: flex; justify-content: center; font-size: 30rpx; box-shadow: 0px 0px 10px gray; } .c1-2{ }
页面布局如下:
ai.js
//获取app.js对象 var app = getApp(); Page({ data: { face: {},//检测结果 img: '', //选择的图片 showResult: false //检测是由有结果 }, onLoad: function (options) { //console.log('获取全局变量数据:' + app.globalData.access_token); }, //选择图片事件 chooseImage(){ var that = this; wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success (res) { const tempPath = res.tempFilePaths[0];//获取选择的图片的地址 //准备好了access_token和图片后,就可以开始请求百度的人脸检测接口了该接口需要的参数是access_token,图片的base64值 //图片的base64值的处理 var base64 = wx.getFileSystemManager().readFileSync(tempPath,'base64'); //提示 wx.showLoading({ title: '人脸检测中...', mask: true }); //开始请求百度的人脸检测接口 wx.request({ url: '检测结果正确 //将选择的图片回显到页面 //that.setData({img: tempPath}); that.setData(); //植物识别要传入键值对 //取出检测的结果进行页面显示 var face = res.data.result.face_list[0]; console.log(face); that.setData({face: face,showResult: true}); //隐藏加载窗口 wx.hideLoading(); }else{ wx.showToast({ title: '检测失败'+res.data.error_msg, duration: 5000 }); } } }) } }) }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })
app.js
//app.js App({ onLaunch: function () { var access_token = wx.getStorageSync('access_token'); var expire_in = wx.getStorageSync('expire_in'); // var access_token = parse; var access_token_date = parseInt(wx.getStorageSync('access_token_date')); var now = new Date().getTime(); if(!access_token){ this.requestToken(); } else if(now > access_token_date + expire_in){ this.requestToken(); }else{ } // 展示本地存储能力 var logs = wx.getStorageSync('logs') [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) // 登录 wx.login({ success: res => { // 发送e 到后台换取 openId, sessionKey, unionId } }) // 获取用户信息rInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 wx.getUserInfo({ success: res => { // 可以将 res 发送给后台解码出 unionId this.globalData.userInfo = res.userInfo // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 if (this.userInfoReadyCallback) { this.userInfoReadyCallback(res) } } }) } } }) }, globalData: { userInfo: null }, requestToken() { var that = this; wx.request({ url: '那里填写自己的百度key值 client_id: 'aaa', client_secret: 'aaa' }, //header: {'content-type': 'application/json'}, header: {'content-type': 'application/x-www-form-urlencoded'}, success (res) { if(res.statusCode == 200){ wx.setStorageSync("access_token", res.data.access_token); wx.setStorageSync("expires_in", res.data.expires_in); //wx.setStorageSync("access_token_date", res.data.access_token_date); wx.setStorageSync("access_token_date", new Date().getTime()); that.globalData.access_token = res.data.access_token; } } }) } })
识别结果如下:
消息,Binance Wallet已完成预测市场集成升级,所有相关服务自2026年7月2日06:50起恢复正常运营。...
2 单靠谴责的言辞无法阻止对基辅的攻击消息,欧盟外交与安全政策高级代表卡拉斯:单靠谴责的言辞无法阻止对基辅的攻击。只有对...
3 Dunamu:upside academy向主要企业输送安全人消息,据韩国经济日报,upbit运营商Dunamu表示,其Web3与网络安全人才培养项目upside academy已向三...
4 法国2026年报告77起与加密相关的绑架和勒消息,据法政部长劳伦特纽内兹透露,2026年法国已记录77起与加密行业相关的绑架、非法拘留...
5 俄对乌第聂伯罗彼得罗夫斯克和波尔塔瓦消息,俄罗斯国防部:俄罗斯对乌克兰第聂伯罗彼得罗夫斯克和波尔塔瓦的机场实施了打击。...
6 Paribu推出DeFi、Polymarket和股票交易等功能消息,土耳其数字资产平台Paribu宣布在其主应用中推出DeFi功能,增加了去中心化交易、通过...
7 特朗普旗下美国比特币因反向拆股跌至历消息,特朗普家族支持的美国比特币在周三因公司宣布进行1比15的反向拆股而跌至历史低点,...
8 4名巨鲸折价抄底MU,以1044美元均价完成建消息,伴随MU再度跌至1000美元上方,HyperLiquid平台已有4名巨鲸选择折价抄底,合计开立754万美...
9 Ark Invest买入1800万美元Circle股票Cathie Wood旗下的Ark Invest于周三买入价值1800万美元的Circle Internet Group股票,当天CRCL股价下跌至...
10 James Wynn发行代币仅WYNN存活,市值32万美元消息,据Lookonchain监测,James Wynn发行的3种代币中仅WYNN存活,该代币市值仅32万美元。James Wyn...
成都来彰科技 蜀ICP备2025134723号-1
资讯来源互联网,如有版权问题请联系管理员删除。