微信小程序-人臉識(shí)別(2)實(shí)現(xiàn)人臉識(shí)別功能

接下來(lái)就是在寫(xiě)一個(gè)頁(yè)面,是刷臉頁(yè)面,通過(guò)這個(gè)頁(yè)面你將自己的臉拍照,傳入到自己的服務(wù)器上,去與存入百度云上面的照片進(jìn)行對(duì)比。下面是代碼。

前臺(tái)代碼:wxml

<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>

JS:

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);

? ? ? ? ? }

? ? ? ? })

? ? ? }

? ? })

? },

后臺(tái)代碼,這個(gè)方法是將你刷臉是拍的照片上傳到服務(wù)器上與百度云對(duì)比,并返回?cái)?shù)據(jù),返回的數(shù)據(jù)中,其中一個(gè)是兩張照片的相似度,相似度在百分之九十五以上,則是本人,

public function login(){

? ? ? ? //上傳文件路徑

? ? ? $dir ="./Upload/temp/";

? ? ? if(!file_exists($dir)){

? ? ? ? ? mkdir($dir,0777,true);

? ? ? }

? ? ? ? $upload = new \Think\Upload();// 實(shí)例化上傳類

? ? ? ? $upload->maxSize = 2048000 ;// 設(shè)置附件上傳大小

? ? ? ? $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設(shè)置附件上傳類型

? ? ? ? $upload->rootPath = $dir; // 設(shè)置附件上傳根目錄

? ? ? ? $upload->savePath = ''; // 設(shè)置附件上傳(子)目錄

? ? ? ? //阻止建文件夾

? ? ? ? $upload->autoSub = false;

? ? ? ? // 上傳文件

? ? ? ? $info =$upload->uploadOne($_FILES['file']);

? ? ? ? if(!$info){

? ? ? ? ? //上傳錯(cuò)誤提示信息

? ? ? ? ? ? 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;

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容