1. 程式人生 > >微信小程式-人臉識別+輸出人臉匹配資訊

微信小程式-人臉識別+輸出人臉匹配資訊

1.在微信小程式建立 camera頁面

camera.wxml程式碼為:

<camera device-position="{{show}}" flash="off" binderror="error" style="width: 100%; height: 400px;"></camera>
<!--除錯前後攝像頭  -->
 <view class='weui-cell__ft'>
            <switch checked bindchange='switch1Change'></switch>
            
  </view>
 <view class="weui-btn-area">
          <button class="weui-btn" type="primary" bindtap="takePhoto">拍照                   </button>
 </view> 


camera.js

Page({

  /**
   * 頁面的初始資料
   */
  data: {
    show: 'back',
    src:''//圖片的資訊
  },
  switch1Change: function (e) {//前後攝像頭
    if (e.detail.value) {
      this.setData({ show: 'back' })
    } else {
      this.setData({ show: 'front' })
    }
  },
  /**
   * 生命週期函式--監聽頁面載入
   */
  onLoad: function (options) {
    this.ctx = wx.createCameraContext()
  },
  // 拍照
  takePhoto() {
    this.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          src: res.tempImagePath
        })

        wx.uploadFile({
          url: '自己的介面', //僅為示例,非真實的介面地址
          filePath: this.data.src,

          name: 'file',
          formData: {
            
          },
          success: function (res) {
            // var data = res.data
            // console.log(res.data);
            //do something
            wx.showModal({
              title: '提示',
              content: res.data,
            })
          }
        })

      }
    })

  },

  /**
   * 生命週期函式--監聽頁面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命週期函式--監聽頁面顯示
   */
  onShow: function () {

  },

  /**
   * 生命週期函式--監聽頁面隱藏
   */
  onHide: function () {

  },

  /**
   * 生命週期函式--監聽頁面解除安裝
   */
  onUnload: function () {

  },

  /**
   * 頁面相關事件處理函式--監聽使用者下拉動作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 頁面上拉觸底事件的處理函式
   */
  onReachBottom: function () {

  },

  /**
   * 使用者點選右上角分享
   */
  onShareAppMessage: function () {

  }
})

3..呼叫的接口裡的方法

 //刷臉登入
   public function login(){
    //上傳路徑
        $dir="./Uploads/temp/";
        if(!file_exists($dir)){
            mkdir($dir,0777,true);
           }
        $upload = new \Think\Upload();// 例項化上傳類
        $upload->maxSize = 2048000;// 設定附件上傳大小2m
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設定附件上傳型別
        $upload->rootPath = $dir; // 設定附件上傳根目錄
        $upload->savePath = ''; // 設定附件上傳(子)目錄

        
        $upload->autoSub=false;
        // 上傳檔案
        $info = $upload->uploadOne($_FILES['file']);
        if(!$info) {// 上傳錯誤提示錯誤資訊
         // return $this->ajaxReturn(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE);
          echo json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE);
        }else{// 上傳成功
        	// $this->success('成功');
        	$file = $dir . $info['savepath'].$info['savename'];
        $image = base64_encode(file_get_contents($file));
        $client = $this->init_face();
        $options['liveness_control'] = 'NORMAL';
        $options['max_user_num']  = '1';
        $ret = $client->search($image,'BASE64','pingjiao',$options);
        // echo json_encode($ret,JSON_UNESCAPED_UNICODE);
        // exit;
          if($ret['error_code']==0){
          $user = $ret['result']['user_list'][0];
          $no = $user['user_id'];
          $score = $user['score'];
          if($score>=90){
            $data = M('face_student')->where("no = '{$no}'")->find();
            $data['score'] = $score;
            // $data['name'] = json_decode($data['name'],true);
            // $data['sex'] = json_decode($data['sex'],true);
            echo '識別成功' . json_encode($data,JSON_UNESCAPED_UNICODE);
          }
        }
          
   }
 }