当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP实现微信小程序人脸识别刷脸登录功能

发布:smiling 来源: PHP粉丝网  添加日期:2021-09-17 19:58:36 浏览: 评论:0 

本文通过实例代码给大家讲解了基于PHP实现微信小程序人脸识别刷脸登录功能,感兴趣的朋友跟随脚本之家小编一起学习吧。

首先我们先确认我们的百度云人脸库里已经上传了我们的个人信息照片

然后我们在后台写刷脸登陆的接口login我们要把拍照获取的照片存储到服务器

  1. public function login(){  
  2.    // 上传文件路径  
  3.    $dir = "./Uploads/temp/";  
  4.    if(!file_exists($dir)){  
  5.     mkdir($dir,0777,true);  
  6.    }  
  7.    $upload = new \Think\Upload();  
  8.    $upload->maxSize = 2048000 ;// 设置附件上传大小  
  9.    $upload->exts = array('jpg''gif''png''jpeg');// 设置附件上传类型  
  10.    $upload->savepath = '';  
  11.    $upload->autoSub = false;  
  12.    $upload->rootPath = $dir// 设置附件上传根目录  
  13.    // 上传单个文件  
  14.    $info = $upload->uploadOne($_FILES['file']);  
  15.    if(!$info) {// 上传错误提示错误信息  
  16.      echo json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE);  
  17.    }else{// 上传成功 获取上传文件信息  
  18.     $file = $dir . $info['savepath'].$info['savename'];  
  19.     $image = base64_encode(file_get_contents($file));  
  20.     $client = $this->init_face();  
  21.     $options['liveness_control'] = 'NORMAL';  
  22.     $options['max_user_num'] = '1';  
  23.     $ret = $client->search($image,'BASE64','student',$options);  
  24.     // echo json_encode($ret,JSON_UNESCAPED_UNICODE);  
  25.     // exit;  
  26.     if($ret['error_code']==0){  
  27.      $user = $ret['result']['user_list'][0];  
  28.      $no = $user['user_id'];  
  29.      $score = $user['score'];  
  30.      if($score>=95){  
  31.       $data = M('student')->where("no = '{$no}'")->find();  
  32.       $data['score'] = $score;  
  33.       // $data['name'] = json_decode($data['name'],true);  
  34.       // $data['sex'] = json_decode($data['sex'],true);  
  35.       echo '识别成功' . json_encode($data,JSON_UNESCAPED_UNICODE);  
  36.      }else{  
  37.       echo '识别失败' . $data['score'];  
  38.      }  
  39.     }  
  40.    }  
  41.   } 

然后进行前台设计

  1. <camera device-position="{{device?'back':'front'}}" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>  
  2.     <view class="weui-cells__title" >开关</view>  
  3.     <view class="weui-cells weui-cells_after-title">  
  4.       <view class="weui-cell weui-cell_switch">  
  5.         <view class="weui-cell__bd">切换摄像头</view>  
  6.         <view class="weui-cell__ft" >  
  7.           <switch bindtap="devicePosition" />  
  8.         </view>  
  9.       </view>  
  10.     </view>  
  11. <button type="primary" bindtap="takePhoto">刷脸登录</button> 

我们还可以控制相机的前后镜头

  1. devicePosition() {  
  2. this.setData({  
  3.  device: !this.data.device,  
  4. })  
  5. console.log("当前相机摄像头为:", this.data.device ? "后置" : "前置");  
  6. camera() {  
  7.  let { ctx, type, startRecord } = this.data; },  
  8. data: {  
  9.  src: null,  
  10. }, 

在js里面调用接口

  1. takePhoto() {  
  2.    const ctx = wx.createCameraContext()  
  3.    ctx.takePhoto({  
  4.     quality: 'high',  
  5.     success: (res) => {  
  6.      this.setData({  
  7.       src: res.tempImagePath  
  8.      })  
  9.      console.log(res)  
  10.      wx.uploadFile({  
  11.       url: ''//仅为示例,非真实的接口地址  
  12.       filePath: this.data.src,  
  13.       name: 'file',  
  14.       formData: {  
  15.       },  
  16.       success: function (res) {  
  17.        // var data = res.data  
  18.        // var json = JSON.parse(data)  
  19.        console.log(res)  
  20.        wx.showModal({  
  21.         title: "提示",  
  22.         content: res.data,  
  23.         showCancel: false,  
  24.         confirmText: "确定" 
  25.        })  
  26.       }  
  27.      })  
  28.     }  
  29.    })  
  30.   }, 

刷脸登录就成功了。

Tags: PHP微信小程序 PHP人脸识别

分享到: