人臉認(rèn)證源碼faceIdentify詳解
本文實(shí)例為大家分享了人臉認(rèn)證源碼faceIdentify的具體代碼,供大家參考,具體內(nèi)容如下
人臉認(rèn)證:
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();
//啟動(dòng)默認(rèn)在屏幕中間
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)
{
//顯示為正在檢測(cè)
this.label1.Text = this.label2.Text = this.label6.Text = this.label9.Text = "正在識(shí)別";
// 刷新可用相機(jī)的列表
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];
// 枚舉所有攝像頭支持的像素,設(shè)置拍照為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>
/// 簽到的按鈕
/// 先保存圖片,然后進(jìn)行比較,獲取的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);
}
}
//關(guān)閉窗口
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>
/// 人臉識(shí)別
/// </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);
//先判斷臉是不是在上面,在繼續(xù)看有匹配的沒,否則提示放上臉
//得到根節(jié)點(diǎn)
JObject jo_result = (JObject)JsonConvert.DeserializeObject(result.ToString());
if ((string)jo_result["error_msg"] != null)
{
MessageBox.Show("對(duì)不起,請(qǐng)把臉放上!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
else
{
//檢測(cè)到臉
//得到result節(jié)點(diǎn)
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("簽到成功,已檢測(cè)到您的信息", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("對(duì)不起,系統(tǒng)根據(jù)您的臉未檢測(cè)到信息", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
else {
MessageBox.Show("對(duì)不起,系統(tǒng)根據(jù)您的臉未檢測(cè)到信息", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
return user;
}
/// <summary>
/// 保存圖片
/// </summary>
public string SavePicture()
{
if (videoSource == null)
{
return null;
}
Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame();
//圖片名稱,年月日時(shí)分秒毫秒.jpg
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".jpg";
//獲取項(xiàng)目的根目錄
string path = AppDomain.CurrentDomain.BaseDirectory;
string picture = path + "\\picture\\" + fileName;
//將圖片保存在服務(wù)器里面
bitmap.Save(picture, ImageFormat.Jpeg);
bitmap.Dispose();
return picture;
}
/// <summary>
/// 根據(jù)編號(hào)查詢用戶信息
/// </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();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#讀取XML的CDATA節(jié)點(diǎn)內(nèi)容實(shí)例詳解
欄 目:C#教程
下一篇:C#中OpenCvSharp 通過特征點(diǎn)匹配圖片的方法
本文標(biāo)題:人臉認(rèn)證源碼faceIdentify詳解
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4671.html
您可能感興趣的文章
- 01-10WPF實(shí)現(xiàn)類似360安全衛(wèi)士界面的程序源碼分享
- 01-10C#實(shí)現(xiàn)DataTable映射成Model的方法(附源碼)
- 01-10基于C#生成條形碼操作知識(shí)匯總附源碼下載
- 01-10C#進(jìn)階系列 WebApi身份認(rèn)證解決方案推薦:Basic基礎(chǔ)認(rèn)證
- 01-10基于C#實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲 C#抓取網(wǎng)頁Html源碼
- 01-10C# WinForm 判斷程序是否已經(jīng)在運(yùn)行,且只允許運(yùn)行一個(gè)實(shí)例,附
- 01-10LZW壓縮算法 C#源碼
- 01-10C#實(shí)現(xiàn)的Windows剪貼板監(jiān)視器功能實(shí)例【附demo源碼下載】
- 01-10C#實(shí)現(xiàn)導(dǎo)出List數(shù)據(jù)到xml文件的方法【附demo源碼下載】
- 01-10C#圖書管理系統(tǒng) 附源碼下載


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置


