Home WeChat Applet Mini Program Development How to implement face recognition in WeChat applet

How to implement face recognition in WeChat applet

Jun 22, 2018 pm 04:21 PM
face recognition WeChat applet

This article mainly introduces in detail the WeChat applet to implement face recognition, add information and upload photos. It has certain reference value. Interested friends can refer to this article.

I have shared with you the specific code for face recognition in the WeChat applet for your reference. The specific content is as follows

First of all, we need developer tools. What we are talking about today is achieved by combining the backend and frontend.

Write an upload method in the PHP controller, the code is as follows:

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 \Think\Upload();// 实例化上传类 
 $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 "采集照片成功"; 
 } 
}
Copy after login

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('没有人脸,失败'); 
 } 
 }
Copy after login

In the front end we need to write js and wxml in the developer tools.

The js code is as follows:

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://*****.top/ppp/server/index.php/home/index/index', 
 data: e.detail.value, 
 method: 'POST', 
 header: { 
 'content-type': 'application/x-www-form-urlencoded' 
 }, 
 success: (res) => { 
 console.log(res.data); 
  
 if (res.data.error) { 
  wx.showToast({ 
  title: res.data.msg, 
  icon: 'none', 
  duration: 2000 
  }) 
 } else { 
  wx.showToast({ 
  title: res.data.msg, 
  icon: 'success', 
  duration: 2000 
  }) 
  
  setTimeout(function () { 
  wx.navigateTo({ 
  url: '../headimg/headimg?id=' + res.data.id, 
  }) 
  }, 2000) 
  
 } 
  
 } 
 
 }) 
 
 } 
  
})
Copy after login

The js code for uploading images is as follows :

const app = getApp() 
function upload(that, id) { 
 if (that.data.files.length == 0) { 
 return; 
 } 
 wx.uploadFile({ 
 url: 'http://****.top/ppp/server/index.php/home/index/upload', //仅为示例,非真实的接口地址 
 filePath: that.data.files[0], 
 name: 'file', 
 formData: { 
 'id': id 
 }, 
 success: function (res) { 
 var data = res.data 
 // var json = JSON.parse(data) 
 console.log(data) 
 wx.showToast({ 
 title: data, 
 icon:'success', 
 duration:2000 
 }) 
 setTimeout(function () { 
 wx.navigateTo({ 
  url: '../index/index', 
 }) 
 }, 2000) 
 } 
 }) 
} 
Page({ 
 chooseImage: function (e) { 
 var that = this; 
 wx.chooseImage({ 
 count: 1, 
 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 
 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 
 success: function (res) { 
 console.log(res) 
 // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 
 that.setData({ 
  files: res.tempFilePaths, 
 }); 
 } 
 }) 
 }, 
 //预览照片 
 previewImage: function () { 
 var current = e.target.dataset.src 
 wx.previewImage({ 
 current: current, 
 urls: this.data.imageList 
 }) 
 }, 
 
 cancel:function(){ 
 wx.redirectTo({ 
 url: '../index/index', 
 }) 
 }, 
 /** 
 * 页面的初始数据 
 */ 
 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 () { 
 
 } 
})
Copy after login

The wxml code is as follows:

<view class="weui-cells__title text">录入学生信息</view> 
<form bindsubmit="formSubmit"> 
 <view class="weui-cells weui-cells_after-title"> 
 <view class="weui-cell weui-cell_input"> 
 <view class="weui-cell__hd"> 
  <view class="weui-label">学号</view> 
 </view> 
 <view class="weui-cell__bd"> 
  <input class="weui-input" placeholder="请输入学号" value=&#39;1635050739&#39; name="no" /> 
 </view> 
 </view> 
 <view class="weui-cell weui-cell_input weui-cell_vcode"> 
 <view class="weui-cell__hd"> 
  <view class="weui-label">姓名</view> 
 </view> 
 <view class="weui-cell__bd"> 
  <input class="weui-input" placeholder="请输入姓名" value=&#39;小苏&#39; name="name" /> 
 </view> 
 </view> 
 <view class="weui-cell weui-cell_input"> 
 <view class="weui-label">性别</view> 
 <input class="weui-input" name=&#39;sex&#39; value=&#39;{{sex}}&#39;/> 
 <view class=&#39;weui-cell_ft&#39;> 
  <switch checked bindchange=&#39;switch1Change&#39;></switch> 
 </view> 
 </view> 
 <view class="weui-cell weui-cell_input weui-cell_vcode"> 
 <view class="weui-cell__hd"> 
  <view class="weui-label">年龄</view> 
 </view> 
 <view class="weui-cell__bd"> 
  <input class="weui-input" placeholder="请输入年龄" value=&#39;20&#39; name="age" /> 
 </view> 
 </view> 
 </view> 
 <view class="weui-btn-area"> 
 <button class="weui-btn" type="primary" bindtap="showTopTips" formType="submit">注册</button> 
 <button class="weui-btn" type="default" bindtap=&#39;cancel&#39;>返回上级</button> 
 </view> 
</form>
Copy after login

The wxml code for uploading pictures is as follows:

<view class="page" xmlns:wx="http://www.w3.org/1999/xhtml"> 
<view class="weui-cells__title text">图像采集</view> 
<view class="weui-cells__title text">{{options.name}} {{options.no}}</view> 
<form bindsubmit="formSubmit"> 
<view class="page__bd"> 
 <view class="weui-cells"> 
  <view class="weui-cell"> 
  <view class="weui-cell__bd"> 
   <view class="weui-uploader"> 
   <view class="weui-uploader__hd"> 
    <view class="weui-uploader__title">图片上传</view> 
    <view class="weui-uploader__info">{{files.length}}/1</view> 
   </view> 
   <view class="weui-uploader__bd"> 
    <view class="weui-uploader__files" id="uploaderFiles"> 
    <block wx:for="{{files}}" wx:key="*this"> 
     <view class="weui-uploader__file" bindtap="previewImage" id="{{item}}"> 
     <image class="weui-uploader__img" src="{{item}}" mode="aspectFill"/> 
     </view> 
    </block> 
    </view> 
    <view class="weui-uploader__input-box"> 
    <view class="weui-uploader__input" bindtap="chooseImage"></view> 
    </view> 
   </view> 
   </view> 
  </view> 
  </view> 
 </view> 
 </view> 
 <view class="weui-btn-area"> 
  <button class="weui-btn" type="primary" form-type="submit">确认</button> 
  <button class="weui-btn" type="default" bindtap=&#39;cancel&#39;>取消</button> 
 </view> 
 </form> 
</view>
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Two methods to jump to the page in the WeChat mini program

WeChat mini program implements the login page Animation effect of floating clouds

The above is the detailed content of How to implement face recognition in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Xianyu WeChat mini program officially launched Xianyu WeChat mini program officially launched Feb 10, 2024 pm 10:39 PM

Xianyu's official WeChat mini program has quietly been launched. In the mini program, you can post private messages to communicate with buyers/sellers, view personal information and orders, search for items, etc. If you are curious about what the Xianyu WeChat mini program is called, take a look now. What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3. If you want to use it, you must activate WeChat payment before you can purchase it;

WeChat applet implements image upload function WeChat applet implements image upload function Nov 21, 2023 am 09:08 AM

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

Implement the drop-down menu effect in WeChat applet Implement the drop-down menu effect in WeChat applet Nov 21, 2023 pm 03:03 PM

To implement the drop-down menu effect in WeChat Mini Programs, specific code examples are required. With the popularity of mobile Internet, WeChat Mini Programs have become an important part of Internet development, and more and more people have begun to pay attention to and use WeChat Mini Programs. The development of WeChat mini programs is simpler and faster than traditional APP development, but it also requires mastering certain development skills. In the development of WeChat mini programs, drop-down menus are a common UI component, achieving a better user experience. This article will introduce in detail how to implement the drop-down menu effect in the WeChat applet and provide practical

Implement image filter effects in WeChat mini programs Implement image filter effects in WeChat mini programs Nov 21, 2023 pm 06:22 PM

Implementing picture filter effects in WeChat mini programs With the popularity of social media applications, people are increasingly fond of applying filter effects to photos to enhance the artistic effect and attractiveness of the photos. Picture filter effects can also be implemented in WeChat mini programs, providing users with more interesting and creative photo editing functions. This article will introduce how to implement image filter effects in WeChat mini programs and provide specific code examples. First, we need to use the canvas component in the WeChat applet to load and edit images. The canvas component can be used on the page

How to turn off face recognition on Apple phone_How to disable face recognition on Apple phone settings How to turn off face recognition on Apple phone_How to disable face recognition on Apple phone settings Mar 23, 2024 pm 08:20 PM

1. We can ask Siri before going to bed: Whose phone is this? Siri will automatically help us disable face recognition. 2. If you don’t want to disable it, you can turn on Face ID and choose to turn on [Require gaze to enable Face ID]. In this way, the lock screen can only be opened when we are watching.

Use WeChat applet to achieve carousel switching effect Use WeChat applet to achieve carousel switching effect Nov 21, 2023 pm 05:59 PM

Use the WeChat applet to achieve the carousel switching effect. The WeChat applet is a lightweight application that is simple and efficient to develop and use. In WeChat mini programs, it is a common requirement to achieve carousel switching effects. This article will introduce how to use the WeChat applet to achieve the carousel switching effect, and give specific code examples. First, add a carousel component to the page file of the WeChat applet. For example, you can use the &lt;swiper&gt; tag to achieve the switching effect of the carousel. In this component, you can pass b

Use Go language to develop high-performance face recognition applications Use Go language to develop high-performance face recognition applications Nov 20, 2023 am 09:48 AM

Use Go language to develop high-performance face recognition applications Abstract: Face recognition technology is a very popular application field in today's Internet era. This article introduces the steps and processes for developing high-performance face recognition applications using Go language. By using the concurrency, high performance, and ease-of-use features of the Go language, developers can more easily build high-performance face recognition applications. Introduction: In today's information society, face recognition technology is widely used in security monitoring, face payment, face unlocking and other fields. With the rapid development of the Internet

How to enter DingTalk face recognition How to enter DingTalk face recognition Mar 05, 2024 am 08:46 AM

As an intelligent service software, DingTalk not only plays an important role in learning and work, but is also committed to improving user efficiency and solving problems through its powerful functions. With the continuous advancement of technology, facial recognition technology has gradually penetrated into our daily life and work. So how to use the DingTalk app for facial recognition entry? Below, the editor will bring you a detailed introduction. Users who want to know more about it can follow the pictures and text of this article! How to record faces on DingTalk? After opening the DingTalk software on your mobile phone, click "Workbench" at the bottom, then find "Attendance and Clock" and click to open. 2. Then click "Settings" on the lower right side of the attendance page to enter, and then click "My Settings" on the settings page to switch.

See all articles