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

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

发布:smiling 来源: PHP粉丝网  添加日期:2022-06-30 09:11:34 浏览: 评论:0 

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

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

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

然后进行前台设计;

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

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

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

在js里面调用接口;

  1. takePhoto() {  
  2.  
  3.    const ctx = wx.createCameraContext()  
  4.  
  5.    ctx.takePhoto({  
  6.  
  7.     quality: 'high',  
  8.  
  9.     success: (res) => {  
  10.  
  11.      this.setData({  
  12.  
  13.       src: res.tempImagePath  
  14.  
  15.      })  
  16.  
  17.      console.log(res)  
  18.  
  19.      wx.uploadFile({  
  20.  
  21.       url: ''//仅为示例,非真实的接口地址  
  22.  
  23.       filePath: this.data.src,  
  24.  
  25.       name: 'file',  
  26.  
  27.       formData: {  
  28.  
  29.       },  
  30.  
  31.       success: function (res) {  
  32.  
  33.        // var data = res.data  
  34.  
  35.        // var json = JSON.parse(data)  
  36.  
  37.        console.log(res)  
  38.  
  39.        wx.showModal({  
  40.  
  41.         title: "提示",  
  42.  
  43.         content: res.data,  
  44.  
  45.         showCancel: false,  
  46.  
  47.         confirmText: "确定" 
  48.  
  49.        })  
  50.  
  51.       }  
  52.  
  53.      })  
  54.  
  55.     }  
  56.  
  57.    })  
  58.  
  59.   }, 

刷脸登录就成功了。

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

分享到: