您好,登錄后才能下訂單哦!
首先我們先確認我們的百度云人臉庫里已經上傳了我們的個人信息照片;
然后我們在后臺寫刷臉登陸的接口login我們要把拍照獲取的照片存儲到服務器;
public function login(){ // 上傳文件路徑 $dir = "./Uploads/temp/"; if(!file_exists($dir)){ mkdir($dir,0777,true); } $upload = new \Think\Upload(); $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實現微信小程序人臉識別刷臉登錄功能的詳細內容,更多請關注億速云其它相關文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。