首先我們先確認(rèn)我們的百度云人臉庫里已經(jīng)上傳了我們的個人信息照片
然后我們在后臺寫刷臉登陸的接口login我們要把拍照獲取的照片存儲到服務(wù)器
public function login(){
// 上傳文件路徑
$dir = "./Uploads/temp/";
if(!file_exists($dir)){
mkdir($dir,0777,true);
}
$upload = new \Think\Upload();
$upload->maxSize = 2048000 ;// 設(shè)置附件上傳大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設(shè)置附件上傳類型
$upload->savepath = '';
$upload->autoSub = false;
$upload->rootPath = $dir; // 設(shè)置附件上傳根目錄
// 上傳單個文件
$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'];
}
}
}
}
然后進(jìn)行前臺設(shè)計
camera device-position="{{device?'back':'front'}}" flash="off" binderror="error" style="width: 100%; height: 300px;">/camera>
view class="weui-cells__title" >開關(guān)/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>
我們還可以控制相機(jī)的前后鏡頭
devicePosition() {
this.setData({
device: !this.data.device,
})
console.log("當(dāng)前相機(jī)攝像頭為:", this.data.device ? "后置" : "前置");
camera() {
let { ctx, type, startRecord } = this.data; },
data: {
src: null,
},
在js里面調(diào)用接口
takePhoto() {
const ctx = wx.createCameraContext()
ctx.takePhoto({
quality: 'high',
success: (res) => {
this.setData({
src: res.tempImagePath
})
console.log(res)
wx.uploadFile({
url: '', //僅為示例,非真實(shí)的接口地址
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: "確定"
})
}
})
}
})
},
刷臉登錄就成功了
總結(jié)
以上所述是小編給大家介紹的PHP實(shí)現(xiàn)微信小程序人臉識別刷臉登錄,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
您可能感興趣的文章:- 微信小程序人臉識別功能代碼實(shí)例
- 微信小程序?qū)崿F(xiàn)人臉識別登陸的示例代碼
- 小程序?qū)崿F(xiàn)人臉識別功能(百度ai)
- 微信小程序?qū)崿F(xiàn)人臉檢測功能
- 微信小程序?qū)崿F(xiàn)人臉識別
- 詳解微信小程序官方人臉核身認(rèn)證