微信小程式-人臉識別(2)實現人臉識別功能
阿新 • • 發佈:2019-02-13
上一篇的部落格已經將圖片存入到百度雲與自己的伺服器阿里雲上面了,接下來就是在寫一個頁面,是刷臉頁面,通過這個頁面你將自己的臉拍照,傳入到自己的伺服器上,去與存入百度雲上面的照片進行對比。下面是程式碼。
前臺程式碼:
js程式碼:<camera device-position="front" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera> <view class="weui-cell weui-cell_switch"> <view class="weui-cell__ft"> <switch checked bindchange="switch1Change" /> </view> </view> <button type="primary" bindtap="takePhoto">刷臉登入</button>
data: { // switch1Change:true path:null, status:'front' }, switch1Change: function (e) { if(e.detail.value){ this.setData({status:'back'}) }else{ this.setData({status:'front'}) } }, takePhoto() { const ctx = wx.createCameraContext() ctx.takePhoto({ quality: 'high', success: (res) => { this.setData({ src: res.tempImagePath }) wx.uploadFile({ url: 'http://www.anweimin.top/miniprgram-php/server/index.php/home/index/login', filePath: this.data.src, name: 'file', success: (res) => { var data = res.data; console.log(data); } }) } }) },
後臺程式碼,這個方法是將你刷臉是拍的照片上傳到伺服器上與百度雲對比,並返回資料,返回的資料中,其中一個是兩張照片的相似度,相似度在百分之九十五以上,則是本人,
public function login(){ //上傳檔案路徑 $dir ="./Upload/temp/"; if(!file_exists($dir)){ mkdir($dir,0777,true); } $upload = new \Think\Upload();// 例項化上傳類 $upload->maxSize = 2048000 ;// 設定附件上傳大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設定附件上傳型別 $upload->rootPath = $dir; // 設定附件上傳根目錄 $upload->savePath = ''; // 設定附件上傳(子)目錄 //阻止建資料夾 $upload->autoSub = false; // 上傳檔案 $info =$upload->uploadOne($_FILES['file']); if(!$info){ //上傳錯誤提示資訊 echo json_encode(array('error'=>true,'msg'=>$uplaod->getError()),JSON_UNESCAPED_UNICODE); }else{//上傳成功 // $this->success('上傳成功'); $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','pingjiao',$options); echo json_encode($ret,JSON_UNESCAPED_UNICODE); if($ret['error_code']==0){ $user=$ret['result']['user_list']['0']; $no=$user['user_id']; $score=$user['score']; if(!empty($no)){ $data=M('student')->field('no,name,sex')->where("no={$no}")->find(); if($data){ $data['score']=$score; } } } } }