微信小程序实现人脸识别
程序员文章站
2023-11-21 18:46:40
本文为大家分享了微信小程序人脸识别的具体代码,供大家参考,具体内容如下
首先,我们要有开发者工具,今天所说的是后端和前端联合起来实现的。
在php的控制器中写一个upl...
本文为大家分享了微信小程序人脸识别的具体代码,供大家参考,具体内容如下
首先,我们要有开发者工具,今天所说的是后端和前端联合起来实现的。
在php的控制器中写一个upload方法,代码如下:
public function upload($id=''){ if(empty($id)){ return false; } $no = m("student")->where("id={$id}")->getfield('no'); $dir = "./upload/studentface/"; if(!file_exists($dir)){ mkdir($dir, 0777, true); } $upload = new \think\upload();// 实例化上传类 $upload->maxsize = 3145728 ;// 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 $upload->rootpath = $dir; // 设置附件上传根目录 $upload->savepath = ''; // 设置附件上传(子)目录 $upload->savename = $no; $upload->replace = true; $upload->autosub = false; // 上传文件 $info = $upload->uploadone($_files['file']); if(!$info) {// 上传错误提示错误信息 // return $this->ajaxreturn(array('error'=>true,'msg'=>$upload->geterror())); return json_encode(array('error'=>true,'msg'=>$upload->geterror()),json_unescaped_unicode); }else{// 上传成功 获取上传文件信息 // return $this->ajaxreturn(array('error'=>false,'msg'=>$info['savepath'].$info['savename'],'id'=>$id)); $file = $dir . $info['savepath'] . $info['savename']; $image = base64_encode(file_get_contents($file)); $this->facevalid($no,$image); $m = m('head'); $data = $m->where("no='{$no}'")->find(); if($data){ //有数据,则更新 $m->where("no='{$no}'")->save(array('base64'=>$image, 'path'=>$file)); }else{ $m->add(array('no'=>$no,'base64'=>$image,'path'=>$file)); } return "采集照片成功"; } }
public function facevalid($no,$image,$file){ $options = array(); $options["max_face_num"] = 2; // $options["face_type"] = "live"; // $image=file_get_contents($file); // $image=base64_encode($image); // echo $image; $imagetype="base64"; // 带参数调用人脸检测 $client=$this->init_face(); $ret=$client->detect($image,$imagetype,$options); // $arr=$ret; // print_r($ret); // exit; if($ret['error_code']==0){//有人脸 $result=$ret['result']; $face_num=$result['face_num']; if(1==$face_num){//人脸数量为1 $face_probability=$result['face_list'][0]['face_probability']; if(1==$face_probability){//可靠性为1 $group=$this->face_group(); // echo $group; // exit; $faces=$client->facegetlist($no,$group); if($faces['error_code']>0){ $client->adduser($image,'base64',$group,$no); }else{ $client->updateuser($image,'base64',$group,$no); } // echo '人脸检测完成,并已入库'; // return true; // $arr = array('error'=>false,'msg'=>'上传成功'); }else{ die('图片质量'); // die('图片质量仅为:'.$face_probability.',上传失败'); } }else{ die('人脸数量大于1'); // die('人脸数量大于1,失败'); } }else{ die('没有人脸'); // die('没有人脸,失败'); } }
在前端我们需要在开发者工具里写js和wxml.
js代码如下:
const app = getapp() page({ data: { sex: '女', empty:true }, cancel: function () { wx.redirectto({ url: '../face/face', }) }, switch1change: function (e) { if (e.detail.value) { this.setdata({ sex: '男' }) } else { this.setdata({ sex: '女' }) } }, formsubmit: function (e) { // console.log(e); wx.request({ url: 'http://*****.top/ppp/server/index.php/home/index/index', data: e.detail.value, method: 'post', header: { 'content-type': 'application/x-www-form-urlencoded' }, success: (res) => { console.log(res.data); if (res.data.error) { wx.showtoast({ title: res.data.msg, icon: 'none', duration: 2000 }) } else { wx.showtoast({ title: res.data.msg, icon: 'success', duration: 2000 }) settimeout(function () { wx.navigateto({ url: '../headimg/headimg?id=' + res.data.id, }) }, 2000) } } }) } })
上传图片js代码如下:
const app = getapp() function upload(that, id) { if (that.data.files.length == 0) { return; } wx.uploadfile({ url: 'http://****.top/ppp/server/index.php/home/index/upload', //仅为示例,非真实的接口地址 filepath: that.data.files[0], name: 'file', formdata: { 'id': id }, success: function (res) { var data = res.data // var json = json.parse(data) console.log(data) wx.showtoast({ title: data, icon:'success', duration:2000 }) settimeout(function () { wx.navigateto({ url: '../index/index', }) }, 2000) } }) } page({ chooseimage: function (e) { var that = this; wx.chooseimage({ count: 1, sizetype: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourcetype: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { console.log(res) // 返回选定照片的本地文件路径列表,tempfilepath可以作为img标签的src属性显示图片 that.setdata({ files: res.tempfilepaths, }); } }) }, //预览照片 previewimage: function () { var current = e.target.dataset.src wx.previewimage({ current: current, urls: this.data.imagelist }) }, cancel:function(){ wx.redirectto({ url: '../index/index', }) }, /** * 页面的初始数据 */ data: { files: [], options:null, id:null, }, formsubmit:function(e){ upload(this,this.data.id); }, /** * 生命周期函数--监听页面加载 */ onload: function (options) { console.log(options); this.setdata({options:options}) this.setdata({ id: options.id }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onready: function () { }, /** * 生命周期函数--监听页面显示 */ onshow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onhide: function () { }, /** * 生命周期函数--监听页面卸载 */ onunload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onpulldownrefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onreachbottom: function () { }, /** * 用户点击右上角分享 */ onshareappmessage: function () { } })
wxml代码如下:
<view class="weui-cells__title text">录入学生信息</view> <form bindsubmit="formsubmit"> <view class="weui-cells weui-cells_after-title"> <view class="weui-cell weui-cell_input"> <view class="weui-cell__hd"> <view class="weui-label">学号</view> </view> <view class="weui-cell__bd"> <input class="weui-input" placeholder="请输入学号" value='1635050739' name="no" /> </view> </view> <view class="weui-cell weui-cell_input weui-cell_vcode"> <view class="weui-cell__hd"> <view class="weui-label">姓名</view> </view> <view class="weui-cell__bd"> <input class="weui-input" placeholder="请输入姓名" value='小苏' name="name" /> </view> </view> <view class="weui-cell weui-cell_input"> <view class="weui-label">性别</view> <input class="weui-input" name='sex' value='{{sex}}'/> <view class='weui-cell_ft'> <switch checked bindchange='switch1change'></switch> </view> </view> <view class="weui-cell weui-cell_input weui-cell_vcode"> <view class="weui-cell__hd"> <view class="weui-label">年龄</view> </view> <view class="weui-cell__bd"> <input class="weui-input" placeholder="请输入年龄" value='20' name="age" /> </view> </view> </view> <view class="weui-btn-area"> <button class="weui-btn" type="primary" bindtap="showtoptips" formtype="submit">注册</button> <button class="weui-btn" type="default" bindtap='cancel'>返回上级</button> </view> </form>
上传图片wxml代码如下:
<view class="page" xmlns:wx="http://www.w3.org/1999/xhtml"> <view class="weui-cells__title text">图像采集</view> <view class="weui-cells__title text">{{options.name}} {{options.no}}</view> <form bindsubmit="formsubmit"> <view class="page__bd"> <view class="weui-cells"> <view class="weui-cell"> <view class="weui-cell__bd"> <view class="weui-uploader"> <view class="weui-uploader__hd"> <view class="weui-uploader__title">图片上传</view> <view class="weui-uploader__info">{{files.length}}/1</view> </view> <view class="weui-uploader__bd"> <view class="weui-uploader__files" id="uploaderfiles"> <block wx:for="{{files}}" wx:key="*this"> <view class="weui-uploader__file" bindtap="previewimage" id="{{item}}"> <image class="weui-uploader__img" src="{{item}}" mode="aspectfill"/> </view> </block> </view> <view class="weui-uploader__input-box"> <view class="weui-uploader__input" bindtap="chooseimage"></view> </view> </view> </view> </view> </view> </view> </view> <view class="weui-btn-area"> <button class="weui-btn" type="primary" form-type="submit">确认</button> <button class="weui-btn" type="default" bindtap='cancel'>取消</button> </view> </form> </view>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。