顔認証ソースfaceIdentify詳細


本論文の例では、顔認証のソースコード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("
") + 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(); } } }
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。