教你用PHP實現微信小程式臉部辨識刷臉登入功能

2020-07-16 10:05:59
教你用PHP實現微信小程式臉部辨識刷臉登入功能

首先我們先確認我們的百度雲人臉庫裡已經上傳了我們的個人資訊照片;

然後我們在後台寫刷臉登陸的介面login我們要把拍照獲取的照片儲存到伺服器;

public function login(){ 
   // 上傳檔案路徑 
   $dir = "./Uploads/temp/"; 
   if(!file_exists($dir)){ 
    mkdir($dir,0777,true); 
   } 
   $upload = new ThinkUpload(); 
   $upload->maxSize = 2048000 ;// 設定附件上傳大小 
   $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設定附件上傳型別 
   $upload->savepath = ''; 
   $upload->autoSub = false; 
   $upload->rootPath = $dir; // 設定附件上傳根目錄 
   // 上傳單個檔案 
   $info = $upload->uploadOne($_FILES['file']); 
   if(!$info) {// 上傳錯誤提示錯誤資訊 
     echo json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE); 
   }else{// 上傳成功 獲取上傳檔案資訊 
    $file = $dir . $info['savepath'].$info['savename']; 
    $image = base64_encode(file_get_contents($file)); 
    $client = $this->init_face(); 
    $options['liveness_control'] = 'NORMAL'; 
    $options['max_user_num'] = '1'; 
    $ret = $client->search($image,'BASE64','student',$options); 
    // echo json_encode($ret,JSON_UNESCAPED_UNICODE); 
    // exit; 
    if($ret['error_code']==0){ 
     $user = $ret['result']['user_list'][0]; 
     $no = $user['user_id']; 
     $score = $user['score']; 
     if($score>=95){ 
      $data = M('student')->where("no = '{$no}'")->find(); 
      $data['score'] = $score; 
      // $data['name'] = json_decode($data['name'],true); 
      // $data['sex'] = json_decode($data['sex'],true); 
      echo '識別成功' . json_encode($data,JSON_UNESCAPED_UNICODE); 
     }else{ 
      echo '識別失敗' . $data['score']; 
     } 
    } 
   } 
  }

然後進行前台設計;

<camera device-position="{{device?'back':'front'}}" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera> 
    <view class="weui-cells__title" >開關</view> 
    <view class="weui-cells weui-cells_after-title"> 
      <view class="weui-cell weui-cell_switch"> 
        <view class="weui-cell__bd">切換攝像頭</view> 
        <view class="weui-cell__ft" > 
          <switch bindtap="devicePosition" /> 
        </view> 
      </view> 
    </view> 
<button type="primary" bindtap="takePhoto">刷臉登入</button>

我們還可以控制相機的前後鏡頭;

devicePosition() { 
this.setData({ 
 device: !this.data.device, 
}) 
console.log("當前相機攝像頭為:", this.data.device ? "後置" : "前置"); 
camera() { 
 let { ctx, type, startRecord } = this.data; }, 
data: { 
 src: null, 
},

在js裡面呼叫介面;

takePhoto() { 
   const ctx = wx.createCameraContext() 
   ctx.takePhoto({ 
    quality: 'high', 
    success: (res) => { 
     this.setData({ 
      src: res.tempImagePath 
     }) 
     console.log(res) 
     wx.uploadFile({ 
      url: '', //僅為範例,非真實的介面地址 
      filePath: this.data.src, 
      name: 'file', 
      formData: { 
      }, 
      success: function (res) { 
       // var data = res.data 
       // var json = JSON.parse(data) 
       console.log(res) 
       wx.showModal({ 
        title: "提示", 
        content: res.data, 
        showCancel: false, 
        confirmText: "確定"
       }) 
      } 
     }) 
    } 
   }) 
  },

刷臉登入就成功了。

以上就是教你用PHP實現微信小程式臉部辨識刷臉登入功能的詳細內容,更多請關注TW511.COM其它相關文章!