本系统开发环境为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
最后最核心的还是人脸对比

人脸对比
核心代码到此结束文末附源码,喜欢的话给个赞和打赏,不理解的地方,欢迎各位留言或加qq

FaceCommon代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using AForge.Video.DirectShow;

namespace RenLianShiBie

{

public class FaceCommon{#region 方法/// <summary>/// 获取已插USB摄像头硬件Id/// </summary>/// <returns></returns>public static List<string> GetCameraDeviceId(){List<string> _cameraList = new List<string>();FilterInfoCollection _filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);//获取所有已插USB摄像头驱动信息if (_filterInfoCollection != null && _filterInfoCollection.Count > 0){for (int i = 0; i < _filterInfoCollection.Count; i++){_cameraList.Add(_filterInfoCollection[i].MonikerString); //向集合中添加USB摄像头硬件Id}_cameraList.Remove(""); //移出空项return _cameraList;}else{return null;}}#endregion}

}

宏定义

public class UserImation

{/// <summary>/// 用户/// </summary>public const string strUser = "用户";/// <summary>/// 性别/// </summary>public const string strGender = "性别";/// <summary>/// 工号/// </summary>public const string strNumber = "工号";/// <summary>/// 人脸库图片/// </summary>public const string strFace = "人脸库图片";}public class UserImation2{/// <summary>/// 用户/// </summary>public static string strUser { get; set; }/// <summary>/// 性别/// </summary>public static string strGender { get; set; }/// <summary>/// 工号/// </summary>public static string strNumber { get; set; }/// <summary>/// 人脸库图片/// </summary>public static string strFace { get; set; }}

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

{

public partial class Form1 : Form{public Form1(){InitializeComponent();}/// <summary>/// USB摄像头硬件Id集合/// </summary>private List<string> _cameraList = new List<string>();/// <summary>/// 视频驱动/// </summary>private VideoCaptureDevice _videoCaptureDevice;/// <summary>/// 人脸图片/// </summary>private Image imgobj;/// <summary>/// 路径/// </summary>string strPath = @"F:\中地学习\人脸识别\测试\" ;/// <summary>/// 录入信息按钮/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void simpleButton1_Click(object sender, EventArgs e){simpleButton2.Visible = false;simpleButton4.Text = "人脸录入";simpleButton4.Visible =  true;simpleButton3.Visible = true;textEdit1.Properties.ReadOnly = textEdit2.Properties.ReadOnly = textEdit3.Properties.ReadOnly = false;}/// <summary>/// 界面初始化/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Form1_Load(object sender, EventArgs e){simpleButton2.Visible=false;simpleButton4.Text = "打卡";simpleButton4.Visible = true;simpleButton3.Visible = false;#region 填充摄像头下拉框_cameraList = FaceCommon.GetCameraDeviceId();//获取所有USB摄像头硬件Id集合if (_cameraList != null && _cameraList.Count > 0){foreach (var item in _cameraList){labelControl7.Text = item; //向下拉框中添加摄像头列表}}else{//如何未获取到USB摄像头则禁用此选择labelControl7.Text = "当前无摄像头"; ;}#endregion}/// <summary>/// 人脸录入/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void simpleButton4_Click(object sender, EventArgs e){if (simpleButton4.Text == "打卡"){simpleButton1.Visible = false;simpleButton2.Visible = true;}if (this.labelControl7.Text != null){_videoCaptureDevice = new VideoCaptureDevice(this.labelControl7.Text);_videoCaptureDevice.NewFrame += HandNewFrame;}if (_videoCaptureDevice != null){_videoCaptureDevice.Start();}}/// <summary>/// 播放事件/// </summary>/// <param name="sender"></param>/// <param name="args"></param>private void HandNewFrame(object sender, NewFrameEventArgs args){try{this.Invoke(new Action(() =>{if (args != null){this.pictureBox1.Image = args.Frame.Clone() as Image;//  imgobj = args.Frame.Clone() as Image;// imgobj.Save(@"F:\中地学习\人脸识别\测试\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg");}}));}catch (Exception exception){//throw;} }/// <summary>/// 全部信息录入/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void simpleButton3_Click(object sender, EventArgs e){MySqlite.MySqliteConnection();UserImation2.strUser = textEdit1.Text;UserImation2.strGender = textEdit2.Text;UserImation2.strNumber = textEdit3.Text;if(UserImation2.strUser=="")//图片以姓名命名,要求必须先填用户姓名,才能录照片{return;}//关闭if (_videoCaptureDevice != null){_videoCaptureDevice.SignalToStop();}//点击全部录入时也就是拍照,获取定格图片imgobj = this.pictureBox1.Image;imgobj.Save(strPath + textEdit1.Text + ".jpg");MySqlite.MyInsertTable(UserImation2.strUser, UserImation2.strGender, UserImation2.strNumber, strPath + textEdit1.Text + ".jpg");}private void simpleButton2_Click(object sender, EventArgs e){//关闭if (_videoCaptureDevice != null){_videoCaptureDevice.SignalToStop();}imgobj = this.pictureBox1.Image;imgobj.Save(strPath +@"\打卡\" + "1.jpg");var API_KEY = "zKASe0f2AtMvzdd05fBQEEl4";var SECRET_KEY = "Q7C5nwHw9aLA815vL60mRRkhProZusq7";Baidu.Aip.Face.Face client = new Baidu.Aip.Face.Face(API_KEY, SECRET_KEY);MySqlite.MySqliteConnection();while(true){string strPa = "";int nNum = 0;Dictionary<int ,List<string>> dic=  MySqlite.MySelectGridViewTable();foreach (KeyValuePair<int, List<string>> ergodic in dic){foreach (var myValues in ergodic.Value){nNum++;if (nNum == 1){textEdit1.Text = myValues;}else if (nNum == 2){textEdit2.Text = myValues;}else if (nNum == 3){textEdit3.Text = myValues;}else if (nNum == 4){// textEdit4.Text = myValues;strPa = myValues;//获取最后一个图片的位置}else{}}}var faces = new JArray{new JObject{{"image", ReadImg(strPa)},{"image_type", "BASE64"},{"face_type", "LIVE"},{"quality_control", "LOW"},{"liveness_control", "NONE"},},new JObject{{"image", ReadImg(strPath +@"\打卡\"+ "1.jpg")},{"image_type", "BASE64"},{"face_type", "LIVE"},{"quality_control", "LOW"},{"liveness_control", "NONE"},}};var result = client.Match(faces);try{double dData = double.Parse(result.First.Next.Next.Next.Next.Next.First.First.First.ToString());if (dData >= 90){textEdit4.Text = "打卡成功";break;}}catch (System.Exception ex){}}}/// <summary>/// 格式转换/// </summary>/// <param name="img"></param>/// <returns></returns>public string ReadImg(string img){return Convert.ToBase64String(File.ReadAllBytes(img));}}

}

数据库代码

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

{

class MySqlite{static SQLiteConnection sqlCnn = null;/// <summary>/// 数据库链接/// </summary>public static void MySqliteConnection(){try{string strPath = @"F:\中地学习\yuanma\FacecorePlatform_53da9c13-237f-4e2b-a5bd-29b74c3ea02e\faceView\RenMan.db";sqlCnn = new SQLiteConnection();sqlCnn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath;sqlCnn.Open();if (sqlCnn.State != ConnectionState.Open){XtraMessageBox.Show("数据库连接失败 ");return;}}catch (System.Exception ex){return;}}/// <summary>/// 创建表/// </summary>public static void MyCreateTable(){SQLiteCommand mDbCmd = sqlCnn.CreateCommand();mDbCmd.CommandText = "SELECT COUNT(*) FROM sqlite_master where type='table' and name='QrCode';";if (0 == Convert.ToInt32(mDbCmd.ExecuteScalar())){string sql = "CREATE TABLE " + "QrCode" + "(" + "用户 string,性别 string,工号 string,  人脸库 oleobject" + ")";SQLiteCommand oledbCmdup1 = new SQLiteCommand(sql, sqlCnn);oledbCmdup1.ExecuteNonQuery();}else{}}/// <summary>/// 插入/// </summary>public static void MyInsertTable(string strUser,string strGender,string strNumber,string strFace){//读取人脸图片FileStream fileStream = new FileStream(strFace, FileMode.Open);byte[] bFile = new byte[fileStream.Length];//分配数组大小fileStream.Read(bFile, 0, (int)fileStream.Length);//将文件内容读进数组fileStream.Close();//关闭文件对象SQLiteCommand com = sqlCnn.CreateCommand();com.CommandText = "Insert into QrCode(用户,性别,工号,人脸库) Values(@用户,@性别, @工号,@人脸库)";com.Parameters.AddWithValue("@用户", strUser);com.Parameters.AddWithValue("@性别", strGender);com.Parameters.AddWithValue("@工号", strNumber);com.Parameters.AddWithValue("@人脸库", bFile);com.ExecuteNonQuery();XtraMessageBox.Show("增加人员信息成功");}/// <summary>/// 查询/// </summary>/// <param name="table"></param>/// <returns></returns>public static Dictionary<int ,List<string>> MySelectGridViewTable(){string strUser = "";string strGender = "";string strNumber = "";string strFace = "";Dictionary<int ,List<string>> dic = new Dictionary<int,List<string>>();List<string> lstInformation = new List<string>();string sql = "select * from QrCode";SQLiteCommand oledbCmdup = new SQLiteCommand(sql, sqlCnn);SQLiteDataReader r = oledbCmdup.ExecuteReader();int num = 0;while (r.Read()){num++;strUser = r[UserImation.strUser].ToString();strGender = r[UserImation.strGender].ToString();strNumber = r[UserImation.strNumber].ToString();SQLiteCommand com = sqlCnn.CreateCommand();com.CommandText = "Select 人脸库 From QrCode where 用户=" + "'" + strUser + "'";byte[] bFile = (byte[])com.ExecuteScalar();//读取之后转换成二进制字节数组if (bFile == null){XtraMessageBox.Show("数据出错");return null;}MemoryStream stream = new MemoryStream(bFile);Image img = Image.FromStream(stream);//将二进制字节数组还原成原本的图像img.Save(@"F:\中地学习\人脸识别\测试\临时数据\" + num + ".jpg");strFace = @"F:\中地学习\人脸识别\测试\临时数据\" + num + ".jpg";lstInformation.Add(strUser);lstInformation.Add(strGender);lstInformation.Add(strNumber);lstInformation.Add(strFace);dic.Add(num, lstInformation);}return dic;}}

}

c# winform实现人脸识别系统(文末附源码)相关推荐

  1. 毕业设计:自主开发的害虫识别系统--文档附源码

    [基于yolov5多目标检测算法的农业害虫识别查询系统] 设计文档 目标问题与意义价值 [研究意义]本项目能够及时准确地识别农业害虫的种类,是害虫准确测报和合理防治的前提.传统的害虫识别方法主要依赖个 ...

  2. python程序员专用壁纸_程序员炫技必备:用Python生成马赛克画!(文末附源码)...

    原标题:程序员炫技必备:用Python生成马赛克画!(文末附源码) 源 | Python与数据分析文 | 强哥 大家知道马赛克画是什么吗?不是动作片里的马赛克哦~~ 马赛克画是一张由小图拼成的大图,本 ...

  3. 微信小程序/uni-app 蓝牙打印开发教程和常见问题总结【文末附源码】

    微信小程序/uni-app 蓝牙打印开发教程和常见问题总结[文末附源码] 文章目录 微信小程序/uni-app 蓝牙打印开发教程和常见问题总结[文末附源码] 1️⃣ 写在前面 2️⃣ 蓝牙连接流程 3 ...

  4. JQuery实现灯泡开关【文末附源码】

    JQuery实现灯泡开关[文末附源码]

  5. C# 30分钟完成百度人脸识别——进阶篇(文末附源码)

    距离上次入门篇时隔两个月才出这进阶篇,小编惭愧,对不住关注我的卡哇伊的小伙伴们,为此小编用这篇博来谢罪. 前面的准备工作我就不说了,注册百度账号api,创建web网站项目,引入动态链接库引入. 不了解 ...

  6. RoI Pooling 系列方法介绍(文末附源码)

    作者简介 CW,广东深圳人,毕业于中山大学(SYSU)数据科学与计算机学院,毕业后就业于腾讯计算机系统有限公司技术工程与事业群(TEG)从事Devops工作,期间在AI LAB实习过,实操过道路交通元 ...

  7. 轻松实现全国高校地理位置数据爬取(文末附源码和数据集)

    大家好,我是小一 一个城市的历史底蕴,不光可以从经济.文化和人文景点,还可以从高校的数量可以看出来.所以,今天就来试试如何爬取全国高校的分布数据. 以下文章比较适合初学者,老读者请酌情加速阅读. 今天 ...

  8. 20份可视化大屏模板,直接套用真香(文末附源码)

    最近有不少小伙伴问我:有没有数据可视化大屏模板,而且要B格很高的. 这不,立马安排.特地给大家准备了20张精美.炫酷而且十分实用的可视化大屏模板,涉及机械.加工.零售.银行.交通等行业. 只要你有数据 ...

  9. AI预测彩票,使用chatgpt和lstm神经网络(文末附源码)

    提示:经过2个月的使用AI预测彩票的测试写一篇文章记录下心路历程 文章目录 前言 一.什么是lstm和chatgpt? 二.chat使用步骤 1. wetab浏览器插件 2. 整理的训练话术如下(重点 ...

  10. C++实现坦克大战(超详细)(文末附源码!!!)

    一.成果展示 二.开发环境及工具 C++开发,使用工具为vs2019的community版本,坦克大战需要借助EasyX库来完成坦克大战的图形绘制. 三.游戏规则设定 (1)玩家移动及发射炮弹: 单人 ...

最新文章

  1. 一只蝙蝠的自述,在朋友圈火了
  2. UDSMProt:蛋白质分类通用深度序列模型
  3. Spring Security 决策器前缀修改
  4. 高考题(可作为试讲资料)
  5. java optional_JAVA Optional总结
  6. 中国塑溶胶密封剂行业市场供需与战略研究报告
  7. 启动报错:Unsatisfied dependency expressed through field ‘baseMapper‘
  8. 频率单位Hz、MHz、GHz、THz、PHz、EHz换算关系
  9. python执行excel公式 语法_Python读取excel文件中带公式的值的实现
  10. 在Linux中连接和使用云存储的三种途径
  11. 记一次固态硬盘数据恢复
  12. android前置摄像头拍摄,Android前置摄像头拍摄倒置照片
  13. speedoffice(Word)怎么添加页码
  14. 关于技嘉雷电扩展卡SSDT驱动教程
  15. 心知天气Android开发,H5 实现天气效果(心知天气插件)
  16. 低依赖C++ GUI库imgui笔记
  17. 基于Aforge的物体运动识别-入门篇
  18. 有什么JAVA 网站或者论坛资料丰富呢
  19. JSP 9大内置对象
  20. 免费电子书籍下载站点●大全

热门文章

  1. 推荐一个图片在线生成链接的网站
  2. DHCP配置实验(包括接口模式、全局模式和中继模式)
  3. dorado 7 注意总结
  4. vue word 转换html渲染页面(mammoth)
  5. python开根号_python开根号_python 开根号_python开根号函数 - 云+社区 - 腾讯云
  6. coap 返回版本信息_CoAP协议学习笔记——CoAP格式详解
  7. 系列学习 Gateway 之第 3 篇 —— 过滤器 Filter,自定义全局过滤器
  8. 达梦数据库导出表格形式(Excel)方式
  9. 区块链入门教程(1)--概述
  10. 解决谷歌浏览器最新版本CORS跨域问题