How to implement face recognition in WeChat applet
Jun 22, 2018 pm 04:21 PMThis 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 ;// 設(shè)置附件上傳大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設(shè)置附件上傳類型 $upload->rootPath = $dir; // 設(shè)置附件上傳根目錄 $upload->savePath = ''; // 設(shè)置附件上傳(子)目錄 $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){ //有數(shù)據(jù),則更新 $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"; // 帶參數(shù)調(diào)用人臉檢測 $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){//人臉數(shù)量為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('圖片質(zhì)量'); // die('圖片質(zhì)量僅為:'.$face_probability.',上傳失敗'); } }else{ die('人臉數(shù)量大于1'); // die('人臉數(shù)量大于1,失敗'); } }else{ die('沒有人臉'); // die('沒有人臉,失敗'); } }
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) } } }) } })
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'], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有 sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認(rèn)二者都有 success: function (res) { console.log(res) // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標(biāo)簽的src屬性顯示圖片 that.setData({ files: res.tempFilePaths, }); } }) }, //預(yù)覽照片 previewImage: function () { var current = e.target.dataset.src wx.previewImage({ current: current, urls: this.data.imageList }) }, cancel:function(){ wx.redirectTo({ url: '../index/index', }) }, /** * 頁面的初始數(shù)據(jù) */ data: { files: [], options:null, id:null, }, formSubmit:function(e){ upload(this,this.data.id); }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function (options) { console.log(options); this.setData({options:options}) this.setData({ id: options.id }) }, /** * 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成 */ onReady: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面顯示 */ onShow: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面隱藏 */ onHide: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面卸載 */ onUnload: function () { }, /** * 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函數(shù) */ onReachBottom: function () { }, /** * 用戶點擊右上角分享 */ onShareAppMessage: function () { } })
The wxml code is as follows:
<view class="weui-cells__title text">錄入學(xué)生信息</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">學(xué)號</view> </view> <view class="weui-cell__bd"> <input class="weui-input" placeholder="請輸入學(xué)號" value='1635050739' 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='小蘇' name="name" /> </view> </view> <view class="weui-cell weui-cell_input"> <view class="weui-label">性別</view> <input class="weui-input" name='sex' value='{{sex}}'/> <view class='weui-cell_ft'> <switch checked bindchange='switch1Change'></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='20' 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='cancel'>返回上級</button> </view> </form>
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">確認(rèn)</button> <button class="weui-btn" type="default" bindtap='cancel'>取消</button> </view> </form> </view>
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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;

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

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

The official WeChat mini program of Xianyu has been quietly launched. It provides users with a convenient platform that allows you to easily publish and trade idle items. In the mini program, you can communicate with buyers or sellers via private messages, view personal information and orders, and search for the items you want. So what exactly is Xianyu called in the WeChat mini program? This tutorial guide will introduce it to you in detail. Users who want to know, please follow this article and continue reading! 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.

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

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.

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 <swiper> tag to achieve the switching effect of the carousel. In this component, you can pass b

To implement the picture rotation effect in WeChat Mini Program, specific code examples are required. WeChat Mini Program is a lightweight application that provides users with rich functions and a good user experience. In mini programs, developers can use various components and APIs to achieve various effects. Among them, the picture rotation effect is a common animation effect that can add interest and visual effects to the mini program. To achieve image rotation effects in WeChat mini programs, you need to use the animation API provided by the mini program. The following is a specific code example that shows how to
