人脸认证源码faceIdentify详解

作者:穆雄雄 时间:2023-05-19 09:57:25 

本文实例为大家分享了人脸认证源码faceIdentify的具体代码,供大家参考,具体内容如下

人脸认证:


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

namespace Camtest
{
public partial class faceIdentify : Form
{
 public faceIdentify()
 {
  InitializeComponent();
  //启动默认在屏幕中间
  this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
 }
 //Api_Key
 public static string Api_Key = "OVYw5Ok0y9U8n6CfVPYt0wfZ";
 //Secret_Key
 public static string Secret_Key = "aCN3lupCarq3rC9G8Rylqz1d36Towp8G";
 FilterInfoCollection videoDevices;
 VideoCaptureDevice videoSource;
 public int selectedDeviceIndex = 0;
 public int selectedPICIndex = 0;

//窗体加载
 private void faceIdentify_Load(object sender, EventArgs e)
 {
  //显示为正在检测
  this.label1.Text = this.label2.Text = this.label6.Text = this.label9.Text = "正在识别";

// 刷新可用相机的列表
  videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  comboBoxCameras.Items.Clear();

for (int i = 0; i < videoDevices.Count; i++)
  {
   comboBoxCameras.Items.Add(videoDevices[i].Name.ToString());
  }

if (comboBoxCameras.Items.Count > 0)
   comboBoxCameras.SelectedIndex = 0;
  picsize.SelectedIndex = 0;

//打开摄像头
  openCamera();
 }

//打开摄像头
 public void openCamera()
 {
  selectedPICIndex = picsize.SelectedIndex;

selectedDeviceIndex = comboBoxCameras.SelectedIndex;
  //连接摄像头。
  videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceIndex].MonikerString);
  videoSource.VideoResolution = videoSource.VideoCapabilities[selectedDeviceIndex];
  // 枚举所有摄像头支持的像素,设置拍照为1920*1080
  foreach (VideoCapabilities capab in videoSource.VideoCapabilities)
  {
   if (selectedPICIndex == 0)
   {
    if (capab.FrameSize.Width == 1920 && capab.FrameSize.Height == 1080)
    {
     videoSource.VideoResolution = capab;
     break;
    }
    if (capab.FrameSize.Width == 1280 && capab.FrameSize.Height == 720)
    {
     videoSource.VideoResolution = capab;
     break;
    }
   }
   else
   {
    if (capab.FrameSize.Width == 1280 && capab.FrameSize.Height == 720)
    {
     videoSource.VideoResolution = capab;
     break;
    }
   }
  }
  videoSourcePlayer1.VideoSource = videoSource;

// set NewFrame event handler
  videoSourcePlayer1.Start();
 }

/// <summary>
 /// 签到的按钮
 /// 先保存图片,然后进行比较,获取的id,查询
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void qiandao_Click(object sender, EventArgs e)
 {
  Users users = FaceIdentifys(SavePicture());
  this.label1.Text = users.age.ToString();
  this.label2.Text = users.name;
  this.label6.Text = users.phone;
  this.label9.Text = users.address;
  if (users.picture != null)
  {
   this.pictureBox1.Image = Image.FromFile(users.picture, false);
  }

}

//关闭窗口
 private void faceIdentify_FormClosing(object sender, FormClosingEventArgs e)
 {
  DialogResult r = MessageBox.Show("确定要退出程序?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  if (r != DialogResult.OK)
  {

e.Cancel = true;
  }
  videoSourcePlayer1.Stop();//停止摄像头
  videoSourcePlayer1.Dispose();
 }

/// <summary>
 /// 人脸识别
 /// </summary>
 /// <param name="filename"></param>
 public static Users FaceIdentifys(string filename)
 {
  long id = 0;
  string ids = "";
  double scores_num = 0;
  Users user = new Users();
  var client = new Baidu.Aip.Face.Face(Api_Key, Secret_Key);
  var image1 = File.ReadAllBytes(filename);
  var result = client.User.Identify(image1, new[] { "gr_test" }, 1, 1);
  //先判断脸是不是在上面,在继续看有匹配的没,否则提示放上脸
  //得到根节点
  JObject jo_result = (JObject)JsonConvert.DeserializeObject(result.ToString());
  if ((string)jo_result["error_msg"] != null)
  {
   MessageBox.Show("对不起,请把脸放上!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  }
  else
  {
   //检测到脸
   //得到result节点
   JArray jo_age = (JArray)JsonConvert.DeserializeObject(jo_result["result"].ToString());
   foreach (var val in jo_age)
   {
    id = long.Parse(((JObject)val)["uid"].ToString()); //获取uid
    string scores = ((JObject)val)["scores"].ToString();//获取scores
    int num1 = scores.IndexOf("\n") + 2;
    int num2 = scores.LastIndexOf("]")-8;
    ids = scores.Substring(num1, num2);
    scores_num =double.Parse(ids);
   }
   if (scores_num > 80)
   {
    user = QueryUsersById(id);
    if (user.id != 0)
    {
     MessageBox.Show("签到成功,已检测到您的信息", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
     MessageBox.Show("对不起,系统根据您的脸未检测到信息", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
    }
   }
   else {
    MessageBox.Show("对不起,系统根据您的脸未检测到信息", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
   }

}
  return user;

}

/// <summary>
 /// 保存图片
 /// </summary>
 public string SavePicture()
 {
  if (videoSource == null)
  {
   return null;
  }

Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame();
  //图片名称,年月日时分秒毫秒.jpg
  string fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".jpg";

//获取项目的根目录
  string path = AppDomain.CurrentDomain.BaseDirectory;
  string picture = path + "\\picture\\" + fileName;
  //将图片保存在服务器里面
  bitmap.Save(picture, ImageFormat.Jpeg);
  bitmap.Dispose();
  return picture;
 }

/// <summary>
 /// 根据编号查询用户信息
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Users QueryUsersById(long id)
 {

Users user = new Users();
  string sql = "select * from users where id = @id";
  using (SqlDataReader reader = SqlHelper.ExcuteReader(sql, CommandType.Text, new SqlParameter("@id", id)))
  {
   if (reader.Read())
   {
    user.id = long.Parse(reader[0].ToString());
    user.name = reader[1].ToString();
    user.age = Convert.ToInt32(reader[2]);
    user.phone = reader[3].ToString();
    user.password = reader[4].ToString();
    user.address = reader[5].ToString();
    user.picture = reader[6].ToString();
   }
  }
  return user;
 }

//取消的按钮
 private void close_Click(object sender, EventArgs e)
 {
  //停止摄像头
  videoSourcePlayer1.Stop();
  this.Close();
  welcome we = new welcome();
  we.Show();
 }

}
}

来源:https://blog.csdn.net/qq_34137397/article/details/78685549

标签:人脸认证,faceIdentify
0
投稿

猜你喜欢

  • c#二叉树存储介绍

    2022-11-02 07:58:30
  • Java 集合中的类关于线程安全

    2023-03-13 12:53:22
  • C++实现String类的方法详解

    2023-04-27 08:49:27
  • Java设计模式之访问者模式

    2023-04-17 20:00:15
  • Java使用arthas修改日志级别详解

    2023-02-04 23:09:26
  • Android 获取设备屏幕大小的几种方法总结

    2022-11-30 14:29:07
  • SpringMVC之AbstractAnnotationConfigDispatcherSer解读

    2023-06-21 21:42:32
  • C#图片查看器实现方法

    2021-06-17 16:50:03
  • SpringBoot 静态资源导入及首页设置问题

    2023-11-26 22:45:07
  • 浅谈对象数组或list排序及Collections排序原理

    2021-10-18 08:35:31
  • 详解c# 数组(Array)

    2022-04-15 14:19:47
  • Android编程实现调用相册、相机及拍照后直接裁剪的方法

    2023-12-19 10:27:08
  • 完美解决idea创建文件时,文件不分级展示的情况

    2022-01-01 22:10:19
  • Spring Boot启动端口修改方法

    2022-11-21 19:37:53
  • android效果TapBarMenu绘制底部导航栏的使用方式示例

    2023-07-29 20:53:36
  • OpenCV实现平均背景法

    2023-06-28 18:25:41
  • springboot+rabbitmq实现智能家居实例详解

    2022-09-05 12:23:39
  • Spring IOC与DI核心重点分析

    2023-11-12 14:35:55
  • Java常量池详解

    2023-05-19 19:36:27
  • Android端代码量非常小的分页加载库

    2022-08-25 17:24:48
  • asp之家 软件编程 m.aspxhome.com