栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

微信小程序实现人脸识别

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微信小程序实现人脸识别

本文为大家分享了微信小程序人脸识别的具体代码,供大家参考,具体内容如下

首先,我们要有开发者工具,今天所说的是后端和前端联合起来实现的。

在PHP的控制器中写一个upload方法,代码如下:

public function upload($id=''){ 
 if(empty($id)){ 
  return false; 
 } 
 
 $no = M("student")->where("id={$id}")->getField('no'); 
 $dir = "./Upload/studentface/"; 
 if(!file_exists($dir)){ 
  mkdir($dir, 0777, true); 
 } 
 $upload = new ThinkUpload();// 实例化上传类 
 $upload->maxSize = 3145728 ;// 设置附件上传大小 
 $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 $this->ajaxReturn(array('error'=>true,'msg'=>$upload->getError())); 
  return json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE); 
 }else{// 上传成功 获取上传文件信息 
  // return $this->ajaxReturn(array('error'=>false,'msg'=>$info['savepath'].$info['savename'],'id'=>$id)); 
  $file = $dir . $info['savepath'] . $info['savename']; 
  $image = base64_encode(file_get_contents($file)); 
  $this->facevalid($no,$image); 
 
  $m = M('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 "采集照片成功"; 
 } 
} 
public function facevalid($no,$image,$file){ 
 
 $options = array(); 
 
 $options["max_face_num"] = 2; 
 // $options["face_type"] = "LIVE"; 
 
 // $image=file_get_contents($file); 
 // $image=base64_encode($image); 
 // echo $image; 
 $imageType="base64"; 
  
 // 带参数调用人脸检测 
 $client=$this->init_face(); 
 $ret=$client->detect($image,$imageType,$options); 
 // $arr=$ret; 
 // print_r($ret); 
 // exit; 
 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){//可靠性为1 
   $group=$this->face_group(); 
   
   // echo $group; 
   // exit; 
   $faces=$client->faceGetlist($no,$group); 
   if($faces['error_code']>0){ 
   $client->addUser($image,'base64',$group,$no); 
   }else{ 
   $client->updateUser($image,'base64',$group,$no); 
   } 
   // echo '人脸检测完成,并已入库'; 
   // return true; 
   // $arr = array('error'=>false,'msg'=>'上传成功'); 
   
  }else{ 
  die('图片质量'); 
   // die('图片质量仅为:'.$face_probability.',上传失败'); 
  } 
  }else{ 
  die('人脸数量大于1'); 
  // die('人脸数量大于1,失败'); 
  } 
 }else{ 
  die('没有人脸'); 
  // die('没有人脸,失败'); 
 } 
 } 

在前端我们需要在开发者工具里写js和wxml.

js代码如下:

const app = getApp() 
Page({ 
 data: { 
 sex: '女', 
 empty:true 
 }, 
 cancel: function () { 
 wx.redirectTo({ 
 url: '../face/face', 
 }) 
 }, 
 
 switch1Change: function (e) { 
 if (e.detail.value) { 
 this.setData({ sex: '男' }) 
 } else { 
 this.setData({ sex: '女' }) 
 } 
 }, 
 formSubmit: function (e) { 
 // console.log(e); 
 wx.request({ 
 url: 'http:/ 
 data: { 
 files: [], 
 options:null, 
 id:null, 
 }, 
 formSubmit:function(e){ 
 upload(this,this.data.id); 
 }, 
  
 onLoad: function (options) { 
 console.log(options); 
 this.setData({options:options}) 
 this.setData({ id: options.id }) 
 }, 
 
  
 onReady: function () { 
 
 }, 
 
  
 onShow: function () { 
 
 }, 
 
  
 onHide: function () { 
 
 }, 
 
  
 onUnload: function () { 
 
 }, 
 
  
 onPullDownRefresh: function () { 
 
 }, 
 
  
 onReachBottom: function () { 
 
 }, 
 
  
 onShareAppMessage: function () { 
 
 } 
})

 wxml代码如下:

录入学生信息 
 

上传图片wxml代码如下:

 
图像采集 
{{options.name}} {{options.no}} 
 
 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/80882.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号