首页 > 编程知识 正文

微信小程序人脸识别,图片入库

时间:2023-05-06 16:38:55 阅读:272046 作者:3129

人脸识别最好就是用sdk,因为用sdk十分的便捷,同时易于代码书写,首先把sdk解压放到controller同一目录中,引入AipFace.php,

const HOST='xxx'; //你的网站 //初始化人脸 private function init_face(){ $APP_ID=''; $API_KEY=''; $SECRET_KEY=''; $dir=APP_PATH.'/facesdk/'; //这是我下载的sdk解压后起的名字 require_once $dir.'AipFace.php'; return new Aipface($APP_ID,$API_KEY,$SECRET_KEY); }

在上传图片时就检测是否是人脸,并保存到数据库中

public function upload($id=''){ if(empty($id)){ return false; } $no=M("rl_students")->where("id={$id}")->getField('no'); $dir="./Uploads/studentface/";//上传文件路径 $upload = new ThinkUpload();// 实例化上传类 $upload->maxSize = 2048000 ;// 设置附件上传大小2m $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 $upload->rootPath = $dir; // 设置附件上传根目录 //$upload->savePath = ''; // 设置附件上传(子)目录 $upload->saveName=$no; $upload->replace=true; $upload->autoSub=false; // 上传文件 $info = $upload->uploadOne($_FILES['file']); if(!$info) {// 上传错误提示错误信息 return 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)); $this->facevalid($no,$image); $m=M('rl_head'); $data=$m->where("no='{$no}'")->find(); if($data){ $m->where("no={$no}")->save(array('base64'=>$image,'path'=>$file)); }else{ $m->add(array('no'=>$no,'base64'=>$image,'path'=>$file)); } // return $this->ajaxReturn(array('error'=>false,'msg'=>$info['savepath'].$info['savename'],'id'=>$id)); echo "采集图片成功"; } }//获取小组 private function face_group(){ //组名 $groupname='test'; $client=$this->init_face(); $ret=$client->getGroupList(); if($ret['error_code']==0){ $grouplist=$ret['result']['group_id_list']; if(in_array($groupname, $grouplist)){ return $groupname; }else{ $ret=$client->groupAdd($groupname); if($ret['error_code']==0){ return $groupname; }else{ return false; } } }else{ return false; } } //删除组 private function del_facegroup(){ $client=$this->init_face(); $ret=$client->groupDelete('test'); print_r($ret); } //人脸检测并入库 public function facevalid($no,$image){ // $file='./Uploads/2.jpg'; // if(!file_exists($file)){ // die('文件不存在'); // } // $image=base64_encode(file_get_contents($file)); //如果有可选参数 $options=array(); //$options['face_field']="age"; $options["max_face_num"]=2; // $options["face_type"]="LIVE"; $client=$this->init_face(); $ret=$client->detect($image,'BASE64',$options); // print_r($ret); if($ret['error_code']==0){ //有人脸 $result=$ret['result']; $face_num=$result['face_num']; if(1==$face_num){//人脸数量为1 $face_probability=$result['face_list'][0]['face_probability']; if(1==$face_probability){ // $guid=myguid(); $group=$this->face_group(); // echo $group; // exit; $client->addUser($image,'BASE64',$group,$no); echo "人脸检测完成,并已入库"; }else{ die('可靠性:'.$face_probability); } }else{ die('人脸数量大于1'); } }else{ die('没有人脸'); exit; } }

在我前台,代码如下,这个放到page前面

function upload(that,id) { if(that.data.imageList.length==0){ return; } wx.uploadFile({ url: 'xx', //仅为示例,非真实的接口地址 filePath: that.data.imageList[0], name: 'file', formData: { 'id': id }, success: (res)=> { var data = res.data; console.log(data); wx.showToast({ title: data, }) wx.reLaunch({ url: '../header/header', }) }})}

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