首页 > 编程知识 正文

安装人脸识别系统门,微信人脸识别系统

时间:2023-05-06 06:51:52 阅读:272045 作者:3996

前面写了人脸注册的功能。现在再来实现人脸登录的功能就要简单得多了,还是先上PHP部分的代码:

PHP代码(搜索人脸库并返回对比结果)

<?php date_default_timezone_set("Asia/Shanghai"); //设置时区$code = $_FILES['file'];//获取小程序传来的图片if(is_uploaded_file($_FILES['file']['tmp_name'])) {//把文件转存到你希望的目录 $uploaded_file=$_FILES['file']['tmp_name'];//我们给每个用户动态的创建一个文件夹$user_path=$_SERVER['DOCUMENT_ROOT']."uploadphoto/hwphoto/userface/tmp";//判断该用户文件夹是否已经有这个文件夹 if(!file_exists($user_path)) { mkdir($user_path); } $file_true_name=$_FILES['file']['name']; $move_to_file=$user_path."/".date("Y-m-d")."-".uniqid().substr($file_true_name,strrpos($file_true_name,"."));//这里还是需要把照片保存到服务器端才能对比。只有设定个定时任务定时清除这个文件夹的照片了 move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file));}class post{private function init_face(){ $APP_ID = 'XXXXXX'; $API_KEY = 'XXXXXXXX'; $SECRET_KEY = 'XXXXXXXX'; require_once '/www/wwwroot/www.ailaiyun.com/AipFace.php'; return new AipFace($APP_ID,$API_KEY,$SECRET_KEY); }public function facevalid(){global $move_to_file; $file = "$move_to_file"; //照片服务器路径 if(!file_exists($file)){ die('文件不存在'); }//还是要先判断照片的真实性 $image = file_get_contents($file); $image = base64_encode($image); $options = array(); $options['liveness_control'] = 'LOW'; $options['max_user_num'] = '1'; $client = $this->init_face(); $ret = $client->detect($image,'BASE64',$options); if($ret['error_code']==0){ //有人脸 $result = $ret['result']; $face_num = $result['face_num']; $opp['user_info'] ='1'; if(1==$face_num){ //人脸数目为1 $face_probability = $result['face_list'][0]['face_probability']; if(1==$face_probability){ //可靠性为1//调动人脸对比库搜索对比百度人脸库里的照片 $ret_search = $client->search($image,'BASE64','stu',$options);//输出返回的结果。这里有个坑,在JS里面解决 echo json_encode($ret_search,JSON_UNESCAPED_UNICODE); }else{ die('可靠性为:' . $face_probability); } }else{ die('人脸数量大于1'); } }}}$post =new post();echo $post->facevalid(); ?>

js代码:

Page({ data: { nickName: "", //用户昵称 src: "", //上传到服务器的照片路径 msg: "", //服务器返回信息 url: "", //注册成功后自动回到登录界面 }, //拍照 face() { var that = this var user_id = that.data.nickName wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['camera'], success(res) { console.log(res) wx.showLoading({ title: '人脸对比中', mask: true }) //上传人脸进行注册 var tempFilePaths = res.tempFilePaths console.log(tempFilePaths[0]) wx.uploadFile({ url: 'https://www.ailaiyun.com/xxxxxx.php', filePath: tempFilePaths[0], name: 'file', success(res) { wx.hideLoading() wx.showToast({ title: '人脸登录成功', }) console.log(res.data) var data = res.data//这里解决服务器返回结果PHP输出的坑。把返回的结果转换成可提取的JSON对象形式。。 var obj = JSON.parse(data) console.log(obj)//提取需要的信息 var nickName = obj.result.user_list["0"].user_id var score = obj.result.user_list["0"].score if(score>80){ that.setData({ nickName })//把返回的用户名定义为全局函数。方便登录后识别用户 var app = getApp(); // 取得全局App app.globalData.name = that.data.nickName // 取得全局变量需要的值 wx.redirectTo({ url: '../stu_edit_info/stu_edit_info', }) } else{ wx.showToast({ title: '人脸认证失败,请重试!', icon: 'none' }) } } }) } }) },})

wxml代码:

<!--index.wxml--><view class="container"> <view class='img'> <image src='../images/index.jpg'></image> </view> <text class='title'>人脸登录系统</text> <view> <button class='btn-face' bindtap='face'>人脸登录</button> </view></view>

wxss代码:

/**index.wxss**/.img{ width: 100%;}.img image{ width: 100%;}.title{ margin-top: 20rpx; font-size: 40rpx; margin-bottom: 40rpx; font-weight: bolder;}.btn-face{ border: 10rpx solid rgb(58,160,255); margin-top: 20rpx; padding: 10rpx; border-radius: 50%; width:300rpx; height: 300rpx; line-height: 460rpx; text-align: center; background: url("https://www.ailaiyun.com/temp-img/face.png") no-repeat center; background-size: 50%; background-color: whitesmoke; color: rgb(58, 160, 255); font-weight: bold;}.btn-login{ background-color: rgb(19, 184, 19); margin-top: 20rpx; padding: 10rpx; border-radius: 20rpx; width: 300rpx; text-align: center;}.btn-res{ background-color: rgb(243, 167, 26); margin-top: 20rpx; padding: 10rpx; border-radius: 20rpx; width: 300rpx; text-align: center;}

原文地址: https://www.ailaiyun.com/%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e4%b9%8b%e7%99%be%e5%ba%a6%e4%ba%ba%e8%84%b8%e8%af%86%e5%88%ab%e7%b3%bb%e7%bb%9f-%e4%ba%ba%e8%84%b8%e7%99%bb%e5%bd%95%e5%89%8d%e5%90%8e%e7%ab%af%e4%bb%a3.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。