当前位置: 移动技术网 > IT编程>开发语言>c# > 人脸认证源码faceIdentify详解

人脸认证源码faceIdentify详解

2019年09月09日  | 移动技术网IT编程  | 我要评论
本文实例为大家分享了人脸认证源码faceidentify的具体代码,供大家参考,具体内容如下 人脸认证: using aforge.video.directsh

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

人脸认证:

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();
  }


 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网