人脸注册源码faceregiste
人脸注册:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Controls;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
using face;
using System.Data.SqlClient;
using System.Drawing.Imaging;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace Camtest
{
public partial class faceregiste : Form
{
//Api_Key
public static string Api_Key = "OVYw5Ok0y9U8n6CfVPYt0wfZ";
//Secret_Key
public static string Secret_Key = "aCN3lupCarq3rC9G8Rylqz1d36Towp8G";
public faceregiste()
{
InitializeComponent();
//启动默认在屏幕中间
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
}
FilterInfoCollection videoDevices;
VideoCaptureDevice videoSource;
public int selectedDeviceIndex = 0;
public int selectedPICIndex = 0;
//加载项目
private void faceregiste_Load(object sender, EventArgs e)
{
// 刷新可用相机的列表
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();
}
//注册的按钮
private void register_Click(object sender, EventArgs e)
{
Users user = new Users();
user.name = this.name.Text;
user.id = DateTime.Now.Ticks / 10000;
user.password = this.password.Text;
user.phone = this.phone.Text;
user.age =(int)this.age.Value;
user.address = this.address.Text;
user.picture = SavePicture() ;
//注册人脸通过的话进去
if (FaceRegister(user))
{
int rel = AddUsers(user);//添加信息
if (rel > 0)
{
MessageBox.Show("注册成功", "提示信息");
}
else
{
MessageBox.Show("添加失败", "提示信息");
}
}
}
/// <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;
}
//取消的按钮
private void close_Click(object sender, EventArgs e)
{
//停止摄像头
videoSourcePlayer1.Stop();
this.Close();
welcome we = new welcome();
we.Show();
}
/// <summary>
/// 用户注册
/// </summary>
/// <param name="users"></param>
/// <returns></returns>
public int AddUsers(Users users)
{
int rel = 0;
string sql = "insert INTO Users VALUES(@id,@name,@age,@phone,@password,@address,@picture)";
SqlParameter[] param = {
new SqlParameter("@id",users.id),
new SqlParameter("@name",users.name),
new SqlParameter("@age",users.age),
new SqlParameter("@phone",users.phone),
new SqlParameter("@password",users.password),
new SqlParameter("@address",users.address),
new SqlParameter("@picture",users.picture)
};
rel = SqlHelper.ExecuteNonQuery(sql, CommandType.Text, param);
return rel;
}
//关闭窗体
private void faceregiste_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="picture"></param>
public static bool FaceRegister(Users user)
{
var client = new Baidu.Aip.Face.Face(Api_Key, Secret_Key);
//当前毫秒数可能是负数,取绝对值
var image1 = File.ReadAllBytes(user.picture);
var result = client.User.Register(image1, user.id.ToString(), user.name, new[] { "gr_test" });
//得到根节点
JObject jo_result = (JObject)JsonConvert.DeserializeObject(result.ToString());
if ((string)jo_result["error_msg"] != null)
{
MessageBox.Show("对不起,请把脸放上!","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop);
return false;
}
return true;
}
}
}