C# Winform调用百度接口实现人脸识别教程(附源码)

2020-05-11 13:58:18于海丽

using AForge.Video.DirectShow;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AegeanHotel_management_system
{
  public partial class FrmFaceDemo : Form
  {
    string tocken = "";
    FrmLogin login;
    public FrmFaceDemo(FrmLogin login)
    {
      
      this.login = login;
      InitializeComponent();
      //获取Token并反序列化
      Tocken tk = JsonConvert.DeserializeObject<Tocken>(AccessToken.getAccessToken());
      this.tocken = tk.AccessToken;
    }
    
    private FilterInfoCollection videoDevices;
    private VideoCaptureDevice videoDevice;

    private void FrmFaceDemo_Load(object sender, EventArgs e)
    {
      videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
      videoDevice = new VideoCaptureDevice(videoDevices[0].MonikerString);
      videoSourcePlayer1.VideoSource = videoDevice;
      //开启摄像头
      videoSourcePlayer1.Start();
    }
    private void NewMethod()
    {
      //获取图片 拍照
      Bitmap img = videoSourcePlayer1.GetCurrentVideoFrame();
      //关闭相机
      videoSourcePlayer1.Stop();
      //图片转Base64
      string imagStr = ImagHelper.ImgToBase64String(img);
      Face faceInfo = new Face();
      faceInfo.Image = imagStr;
      faceInfo.ImageType = "BASE64";
      faceInfo.GroupIdList = "admin";
      this.Hide();
      using (FaceOperate faceOperate = new FaceOperate())
      {
        try
        {
          faceOperate.token = tocken;
          //调用查找方法
          var msg = faceOperate.FaceSearch(faceInfo);
            
          foreach (var item in msg.Result.UserList)
          {
            //置信度大于90 认为是本人
            if (item.Score > 90)
            {
              DialogResult dialog = MessageBox.Show("登陆成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
              //this.label1.Text = item.UserId;
              if (dialog == DialogResult.OK)
              {
                FrmShouYe shouye = new FrmShouYe();
                shouye.Show();
                login.Hide();
                this.Close();
                
              }
              return;
            }
            else
            {
              DialogResult dialog = MessageBox.Show("人员不存在", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
              if (dialog == DialogResult.OK)
              {
                this.Close();
              }
            }
          }
        }
        catch (Exception e)
        {
          DialogResult dialog = MessageBox.Show("人员不存在,错误提示"+e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
          if (dialog == DialogResult.OK)
          {
            this.Close();
          }
        }
        
      }
    }

    private void videoSourcePlayer1_Click(object sender, EventArgs e)
    {
      NewMethod();
    }
  }
}