服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - 人脸认证源码faceIdentify详解

人脸认证源码faceIdentify详解

2022-08-04 09:43穆雄雄 C#

这篇文章主要为大家详细介绍了人脸认证源码faceIdentify的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

人脸认证:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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

延伸 · 阅读

精彩推荐
  • C#C#项目中跨文件调用公共类的实例方法

    C#项目中跨文件调用公共类的实例方法

    在本篇文章里小编给大家整理的是关于C#项目中如何跨文件调用公共类的知识点内容,需要的朋友们学习下。...

    仙国小生4992022-07-31
  • C#C#实现Access通用访问类OleDbHelper完整实例

    C#实现Access通用访问类OleDbHelper完整实例

    这篇文章主要介绍了C#实现Access通用访问类OleDbHelper,结合完整实例形式分析了C#针对access数据库的连接、查询、遍历、分页显示等相关操作技巧,需要的朋友...

    蓝之风7882021-12-27
  • C#C# 的析构以及垃圾回收实例分析

    C# 的析构以及垃圾回收实例分析

    这篇文章主要介绍了C# 的析构以及垃圾回收实例分析的相关资料,需要的朋友可以参考下...

    开源中国4512022-01-11
  • C#C#中ValueTuple的原理详解

    C#中ValueTuple的原理详解

    C# 7.0已经出来一段时间了,大家都知道新特性里面有个对元组的优化:ValueTuple,下面这篇文章主要给大家介绍了关于C#中ValueTuple原理的相关资料,需要的朋...

    lindexi9952022-02-24
  • C#C#如何检测操作系统版本

    C#如何检测操作系统版本

    这篇文章主要为大家详细介绍了C#如何检测操作系统版本的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    IT菠萝蜜11532021-12-08
  • C#解析C#设计模式编程中外观模式Facade Pattern的应用

    解析C#设计模式编程中外观模式Facade Pattern的应用

    这篇文章主要介绍了C#设计模式编程中外观模式Facade Pattern的应用,外观模式中分为门面(Facade)和子系统(subsystem)两个角色来进行实现,需要的朋友可以参...

    田志良5992021-11-12
  • C#C# 7.0中解构功能详解

    C# 7.0中解构功能详解

    这篇文章主要为大家详细介绍了C# 7.0中的解构功能,解构元组、解构对象等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    莫问今朝乄5292022-02-28
  • C#C#微信公众号开发 微信事件交互

    C#微信公众号开发 微信事件交互

    这篇文章主要介绍了C#微信公众号开发,微信事件交互的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    garfieldzf6322021-12-18