微信小程序之人脸识别

人脸识别,是基于人的脸部特征信息进行身份识别的一种生物识别技术。用摄像机或摄像头采集含有人脸的图像或视频流,并自动在图像中检测和跟踪人脸,进而对检测到的人脸进行脸部的一系列相关技术,通常也叫做人像识别、面部识别。

下面是前台页面,用到了camera组件,进行拍照

<camera device-position="{{position}}" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
<switch checked bindchange= 'switchChange' />
<button type="primary" bindtap="takePhoto">拍照</button>

后台代码如下(其中init_face方法是获取APP_ID,API_KEY,SECRET_KEY):

//刷脸登录
    public function login(){
    	//上传路径
    	$dir = "./Uploads/temp/";
    	if(!file_exists($dir)){
            mkdir($dir, 0777, true);
        }
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 2048000 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = $dir; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传(子)目录
        $upload->saveName = $no;
        $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));
            // 调用人脸检测
        	$client = $this->init_face();

        	$options['liveness_control'] = 'NORMAL';
        	$options["max_face_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(!empty($no)){
	        		$data = M('student')->field('no,name,sex')->where("no='{$no}'")->find();
	        		if($data){
                        $data['score']=$score;
                        // if($score>95){
                        //     echo '姓名:'.$data['name'];
                        // }else{
                            echo json_encode($data,JSON_UNESCAPED_UNICODE);
                        // }
                    }else{
                        echo "本地数据库没有该学生,百度云信息:个人信息:{$no}, 分值:{$score}";
                    }

	        	}
	        }else{
                echo "活体检测失败," . json_encode($ret,JSON_UNESCAPED_UNICODE);
             }

        }
    }

js代码如下,通过接口实现人脸识别:

Page({

  /**
   * 页面的初始数据
   */
  data: {
    position: 'back',
  },
  switchChange: function(e){
    if (e.detail.value == true) {
      this.setData({ position: 'back' });
    } else {
      this.setData({ position: 'front' });
    }
  },
  takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        var tempImagePath = res.tempImagePath;
        // console.log(tempImagePath);
        wx.showLoading({
          title: '正在核验身份。。。。。',
        })
        this.setData({ loindisables: true });

        wx.uploadFile({
          url: ....., //仅为示例,非真实的接口地址
          header: {
            Cookie: wx.getStorageSync('session_id')
          },
          filePath: tempImagePath,
          name: 'file',
          success: (res) => {
            wx.hideLoading();
            this.setData({ loindisables: false });
            var data = res.data
            console.log(data)
            //do something
            wx.showModal({
              title: '提示',
              content: data,
              showCancel: false
            })
          }
        })
      }
    })
  },



猜你喜欢

转载自blog.csdn.net/tang_tss/article/details/80550438