当前位置:   article > 正文

c# winform人脸识别_winform facedetect

winform facedetect

本系统开发环境为Visual Studio 2010,使用.net 4.0开发,使用AForge库和Aipsdk库和Newtonsoft.json库和system.sqlite库以及第三方插件DevExpress完成。

本系统特点:分为人脸库的录入,将信息保存在sqlite数据库中,该数据库中使用一张表,字段有用户姓名,性别,工号,人脸图片(图像存入数据库中可以点击此链接查看)。
在这里插入图片描述

数据库字段
本系统功能介绍:
在这里插入图片描述
打卡系统界面
首先构造出的是本界面,首先说下个人信息栏,上方的人脸录入和打卡是一个功能只要是调用本机摄像头,找到一张合适的角度拍下此张图片,当界面运行时:界面隐藏了“确定打卡”和“登记按钮”,因为不确定的是当前是打卡还是录入信息。
在这里插入图片描述

界面运行时
如果选择打卡,该打卡功能只要是调用摄像头,此时界面变成
在这里插入图片描述

打卡界面
如果点击确认打卡,循环读取数据库人脸信息,当相似度大于90的时候跳出循环,读取该条信息显示在界面上,打卡状态为成功。如果没有大于90的就返回重新打卡(不方便人脸不截图)。
在这里插入图片描述
打卡成功
下面我将说下信息录入功能,当点击信息录入时打卡按钮变成人脸录入,个人信息文本框变成可用,此时可以输入此人的信息,信息输入完毕,打开人脸录入,最后点击登记功能。

在这里插入图片描述

录入信息
登记完成,信息读入数据库:
在这里插入图片描述
数据库
此时整个功能就实现了。

下面讲一下具体功能实现,人脸识别当然不是自己写的,调用的是百度AI开放平台的SDK,
在这里插入图片描述
百度AI
然后需要创建一个应用列表,需要使用到的是API Key和Secret Key
在这里插入图片描述
应用列表

在这里插入图片描述
调用代码
然后调用摄像头方面代码。首先是获取摄像头代码
在这里插入图片描述
摄像头
FaceCommon是我自己写的一个类,获取已插USB摄像头硬件id
在这里插入图片描述
FaceCommon
最后最核心的还是人脸对比

在这里插入图片描述

人脸对比

核心代码到此结束文末附源码

FaceCommon代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using AForge.Video.DirectShow;

namespace RenLianShiBie

{

  1. public class FaceCommon
  2. {
  3. #region 方法
  4. /// <summary>
  5. /// 获取已插USB摄像头硬件Id
  6. /// </summary>
  7. /// <returns></returns>
  8. public static List<string> GetCameraDeviceId()
  9. {
  10. List<string> _cameraList = new List<string>();
  11. FilterInfoCollection _filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);//获取所有已插USB摄像头驱动信息
  12. if (_filterInfoCollection != null && _filterInfoCollection.Count > 0)
  13. {
  14. for (int i = 0; i < _filterInfoCollection.Count; i++)
  15. {
  16. _cameraList.Add(_filterInfoCollection[i].MonikerString); //向集合中添加USB摄像头硬件Id
  17. }
  18. _cameraList.Remove(""); //移出空项
  19. return _cameraList;
  20. }
  21. else
  22. {
  23. return null;
  24. }
  25. }
  26. #endregion
  27. }

}

宏定义

public class UserImation

  1. {
  2. /// <summary>
  3. /// 用户
  4. /// </summary>
  5. public const string strUser = "用户";
  6. /// <summary>
  7. /// 性别
  8. /// </summary>
  9. public const string strGender = "性别";
  10. /// <summary>
  11. /// 工号
  12. /// </summary>
  13. public const string strNumber = "工号";
  14. /// <summary>
  15. /// 人脸库图片
  16. /// </summary>
  17. public const string strFace = "人脸库图片";
  18. }
  19. public class UserImation2
  20. {
  21. /// <summary>
  22. /// 用户
  23. /// </summary>
  24. public static string strUser { get; set; }
  25. /// <summary>
  26. /// 性别
  27. /// </summary>
  28. public static string strGender { get; set; }
  29. /// <summary>
  30. /// 工号
  31. /// </summary>
  32. public static string strNumber { get; set; }
  33. /// <summary>
  34. /// 人脸库图片
  35. /// </summary>
  36. public static string strFace { get; set; }
  37. }

from1.cs如下

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using DevExpress.XtraEditors;

using AForge.Video.DirectShow;

using AForge.Video;

using System.Threading;

using Newtonsoft.Json.Linq;

using System.IO;

namespace RenLianShiBie

{

  1. public partial class Form1 : Form
  2. {
  3. public Form1()
  4. {
  5. InitializeComponent();
  6. }
  7. /// <summary>
  8. /// USB摄像头硬件Id集合
  9. /// </summary>
  10. private List<string> _cameraList = new List<string>();
  11. /// <summary>
  12. /// 视频驱动
  13. /// </summary>
  14. private VideoCaptureDevice _videoCaptureDevice;
  15. /// <summary>
  16. /// 人脸图片
  17. /// </summary>
  18. private Image imgobj;
  19. /// <summary>
  20. /// 路径
  21. /// </summary>
  22. string strPath = @"F:\中地学习\人脸识别\测试\" ;
  23. /// <summary>
  24. /// 录入信息按钮
  25. /// </summary>
  26. /// <param name="sender"></param>
  27. /// <param name="e"></param>
  28. private void simpleButton1_Click(object sender, EventArgs e)
  29. {
  30. simpleButton2.Visible = false;
  31. simpleButton4.Text = "人脸录入";
  32. simpleButton4.Visible = true;
  33. simpleButton3.Visible = true;
  34. textEdit1.Properties.ReadOnly = textEdit2.Properties.ReadOnly = textEdit3.Properties.ReadOnly = false;
  35. }
  36. /// <summary>
  37. /// 界面初始化
  38. /// </summary>
  39. /// <param name="sender"></param>
  40. /// <param name="e"></param>
  41. private void Form1_Load(object sender, EventArgs e)
  42. {
  43. simpleButton2.Visible=false;
  44. simpleButton4.Text = "打卡";
  45. simpleButton4.Visible = true;
  46. simpleButton3.Visible = false;
  47. #region 填充摄像头下拉框
  48. _cameraList = FaceCommon.GetCameraDeviceId();//获取所有USB摄像头硬件Id集合
  49. if (_cameraList != null && _cameraList.Count > 0)
  50. {
  51. foreach (var item in _cameraList)
  52. {
  53. labelControl7.Text = item; //向下拉框中添加摄像头列表
  54. }
  55. }
  56. else
  57. {
  58. //如何未获取到USB摄像头则禁用此选择
  59. labelControl7.Text = "当前无摄像头"; ;
  60. }
  61. #endregion
  62. }
  63. /// <summary>
  64. /// 人脸录入
  65. /// </summary>
  66. /// <param name="sender"></param>
  67. /// <param name="e"></param>
  68. private void simpleButton4_Click(object sender, EventArgs e)
  69. {
  70. if (simpleButton4.Text == "打卡")
  71. {
  72. simpleButton1.Visible = false;
  73. simpleButton2.Visible = true;
  74. }
  75. if (this.labelControl7.Text != null)
  76. {
  77. _videoCaptureDevice = new VideoCaptureDevice(this.labelControl7.Text);
  78. _videoCaptureDevice.NewFrame += HandNewFrame;
  79. }
  80. if (_videoCaptureDevice != null)
  81. {
  82. _videoCaptureDevice.Start();
  83. }
  84. }
  85. /// <summary>
  86. /// 播放事件
  87. /// </summary>
  88. /// <param name="sender"></param>
  89. /// <param name="args"></param>
  90. private void HandNewFrame(object sender, NewFrameEventArgs args)
  91. {
  92. try
  93. {
  94. this.Invoke(new Action(() =>
  95. {
  96. if (args != null)
  97. {
  98. this.pictureBox1.Image = args.Frame.Clone() as Image;
  99. // imgobj = args.Frame.Clone() as Image;
  100. // imgobj.Save(@"F:\中地学习\人脸识别\测试\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg");
  101. }
  102. }));
  103. }
  104. catch (Exception exception)
  105. {
  106. //throw;
  107. }
  108. }
  109. /// <summary>
  110. /// 全部信息录入
  111. /// </summary>
  112. /// <param name="sender"></param>
  113. /// <param name="e"></param>
  114. private void simpleButton3_Click(object sender, EventArgs e)
  115. {
  116. MySqlite.MySqliteConnection();
  117. UserImation2.strUser = textEdit1.Text;
  118. UserImation2.strGender = textEdit2.Text;
  119. UserImation2.strNumber = textEdit3.Text;
  120. if(UserImation2.strUser=="")//图片以姓名命名,要求必须先填用户姓名,才能录照片
  121. {
  122. return;
  123. }
  124. //关闭
  125. if (_videoCaptureDevice != null)
  126. {
  127. _videoCaptureDevice.SignalToStop();
  128. }
  129. //点击全部录入时也就是拍照,获取定格图片
  130. imgobj = this.pictureBox1.Image;
  131. imgobj.Save(strPath + textEdit1.Text + ".jpg");
  132. MySqlite.MyInsertTable(UserImation2.strUser, UserImation2.strGender, UserImation2.strNumber, strPath + textEdit1.Text + ".jpg");
  133. }
  134. private void simpleButton2_Click(object sender, EventArgs e)
  135. {
  136. //关闭
  137. if (_videoCaptureDevice != null)
  138. {
  139. _videoCaptureDevice.SignalToStop();
  140. }
  141. imgobj = this.pictureBox1.Image;
  142. imgobj.Save(strPath +@"\打卡\" + "1.jpg");
  143. var API_KEY = "zKASe0f2AtMvzdd05fBQEEl4";
  144. var SECRET_KEY = "Q7C5nwHw9aLA815vL60mRRkhProZusq7";
  145. Baidu.Aip.Face.Face client = new Baidu.Aip.Face.Face(API_KEY, SECRET_KEY);
  146. MySqlite.MySqliteConnection();
  147. while(true)
  148. {
  149. string strPa = "";
  150. int nNum = 0;
  151. Dictionary<int ,List<string>> dic= MySqlite.MySelectGridViewTable();
  152. foreach (KeyValuePair<int, List<string>> ergodic in dic)
  153. {
  154. foreach (var myValues in ergodic.Value)
  155. {
  156. nNum++;
  157. if (nNum == 1)
  158. {
  159. textEdit1.Text = myValues;
  160. }
  161. else if (nNum == 2)
  162. {
  163. textEdit2.Text = myValues;
  164. }
  165. else if (nNum == 3)
  166. {
  167. textEdit3.Text = myValues;
  168. }
  169. else if (nNum == 4)
  170. {
  171. // textEdit4.Text = myValues;
  172. strPa = myValues;//获取最后一个图片的位置
  173. }
  174. else
  175. {
  176. }
  177. }
  178. }
  179. var faces = new JArray
  180. {
  181. new JObject
  182. {
  183. {"image", ReadImg(strPa)},
  184. {"image_type", "BASE64"},
  185. {"face_type", "LIVE"},
  186. {"quality_control", "LOW"},
  187. {"liveness_control", "NONE"},
  188. },
  189. new JObject
  190. {
  191. {"image", ReadImg(strPath +@"\打卡\"+ "1.jpg")},
  192. {"image_type", "BASE64"},
  193. {"face_type", "LIVE"},
  194. {"quality_control", "LOW"},
  195. {"liveness_control", "NONE"},
  196. }
  197. };
  198. var result = client.Match(faces);
  199. try
  200. {
  201. double dData = double.Parse(result.First.Next.Next.Next.Next.Next.First.First.First.ToString());
  202. if (dData >= 90)
  203. {
  204. textEdit4.Text = "打卡成功";
  205. break;
  206. }
  207. }
  208. catch (System.Exception ex)
  209. {
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// 格式转换
  215. /// </summary>
  216. /// <param name="img"></param>
  217. /// <returns></returns>
  218. public string ReadImg(string img)
  219. {
  220. return Convert.ToBase64String(File.ReadAllBytes(img));
  221. }
  222. }

}

数据库代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.SQLite;

using System.Data;

using DevExpress.XtraEditors;

using System.IO;

using System.Drawing;

namespace RenLianShiBie

{

  1. class MySqlite
  2. {
  3. static SQLiteConnection sqlCnn = null;
  4. /// <summary>
  5. /// 数据库链接
  6. /// </summary>
  7. public static void MySqliteConnection()
  8. {
  9. try
  10. {
  11. string strPath = @"F:\中地学习\yuanma\FacecorePlatform_53da9c13-237f-4e2b-a5bd-29b74c3ea02e\faceView\RenMan.db";
  12. sqlCnn = new SQLiteConnection();
  13. sqlCnn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath;
  14. sqlCnn.Open();
  15. if (sqlCnn.State != ConnectionState.Open)
  16. {
  17. XtraMessageBox.Show("数据库连接失败 ");
  18. return;
  19. }
  20. }
  21. catch (System.Exception ex)
  22. {
  23. return;
  24. }
  25. }
  26. /// <summary>
  27. /// 创建表
  28. /// </summary>
  29. public static void MyCreateTable()
  30. {
  31. SQLiteCommand mDbCmd = sqlCnn.CreateCommand();
  32. mDbCmd.CommandText = "SELECT COUNT(*) FROM sqlite_master where type='table' and name='QrCode';";
  33. if (0 == Convert.ToInt32(mDbCmd.ExecuteScalar()))
  34. {
  35. string sql = "CREATE TABLE " + "QrCode" + "(" + "用户 string,性别 string,工号 string, 人脸库 oleobject" + ")";
  36. SQLiteCommand oledbCmdup1 = new SQLiteCommand(sql, sqlCnn);
  37. oledbCmdup1.ExecuteNonQuery();
  38. }
  39. else
  40. {
  41. }
  42. }
  43. /// <summary>
  44. /// 插入
  45. /// </summary>
  46. public static void MyInsertTable(string strUser,string strGender,string strNumber,string strFace)
  47. {
  48. //读取人脸图片
  49. FileStream fileStream = new FileStream(strFace, FileMode.Open);
  50. byte[] bFile = new byte[fileStream.Length];//分配数组大小
  51. fileStream.Read(bFile, 0, (int)fileStream.Length);//将文件内容读进数组
  52. fileStream.Close();//关闭文件对象
  53. SQLiteCommand com = sqlCnn.CreateCommand();
  54. com.CommandText = "Insert into QrCode(用户,性别,工号,人脸库) Values(@用户,@性别, @工号,@人脸库)";
  55. com.Parameters.AddWithValue("@用户", strUser);
  56. com.Parameters.AddWithValue("@性别", strGender);
  57. com.Parameters.AddWithValue("@工号", strNumber);
  58. com.Parameters.AddWithValue("@人脸库", bFile);
  59. com.ExecuteNonQuery();
  60. XtraMessageBox.Show("增加人员信息成功");
  61. }
  62. /// <summary>
  63. /// 查询
  64. /// </summary>
  65. /// <param name="table"></param>
  66. /// <returns></returns>
  67. public static Dictionary<int ,List<string>> MySelectGridViewTable()
  68. {
  69. string strUser = "";
  70. string strGender = "";
  71. string strNumber = "";
  72. string strFace = "";
  73. Dictionary<int ,List<string>> dic = new Dictionary<int,List<string>>();
  74. List<string> lstInformation = new List<string>();
  75. string sql = "select * from QrCode";
  76. SQLiteCommand oledbCmdup = new SQLiteCommand(sql, sqlCnn);
  77. SQLiteDataReader r = oledbCmdup.ExecuteReader();
  78. int num = 0;
  79. while (r.Read())
  80. {
  81. num++;
  82. strUser = r[UserImation.strUser].ToString();
  83. strGender = r[UserImation.strGender].ToString();
  84. strNumber = r[UserImation.strNumber].ToString();
  85. SQLiteCommand com = sqlCnn.CreateCommand();
  86. com.CommandText = "Select 人脸库 From QrCode where 用户=" + "'" + strUser + "'";
  87. byte[] bFile = (byte[])com.ExecuteScalar();//读取之后转换成二进制字节数组
  88. if (bFile == null)
  89. {
  90. XtraMessageBox.Show("数据出错");
  91. return null;
  92. }
  93. MemoryStream stream = new MemoryStream(bFile);
  94. Image img = Image.FromStream(stream);//将二进制字节数组还原成原本的图像
  95. img.Save(@"F:\中地学习\人脸识别\测试\临时数据\" + num + ".jpg");
  96. strFace = @"F:\中地学习\人脸识别\测试\临时数据\" + num + ".jpg";
  97. lstInformation.Add(strUser);
  98. lstInformation.Add(strGender);
  99. lstInformation.Add(strNumber);
  100. lstInformation.Add(strFace);
  101. dic.Add(num, lstInformation);
  102. }
  103. return dic;
  104. }
  105. }

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/893984
推荐阅读
  

闽ICP备14008679号