小程序人脸识别php,微信小程序-人脸识别(2)实现人脸识别功能

本文介绍了如何创建一个刷脸登录页面,通过拍照上传照片到服务器,使用PHP进行文件上传,并结合百度云服务进行人脸识别比对。当相似度高于95%时确认为本人。涉及到的技术包括微信小程序开发、图片上传、人脸识别API调用等。
摘要由CSDN通过智能技术生成

接下来就是在写一个页面,是刷脸页面,通过这个页面你将自己的脸拍照,传入到自己的服务器上,去与存入百度云上面的照片进行对比。下面是代码。

前台代码:wxml

刷脸登录

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

}

})

}

})

},

后台代码,这个方法是将你刷脸是拍的照片上传到服务器上与百度云对比,并返回数据,返回的数据中,其中一个是两张照片的相似度,相似度在百分之九十五以上,则是本人,

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;

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值