SoFunction
Updated on 2025-03-03

Detailed explanation of faceIdentify source code

This article shares the specific code of faceIdentify, the source code of faceIdentify, for your reference. The specific content is as follows

Face authentication:

using ;
using face;
using ;
using ;
using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;

namespace Camtest
{
 public partial class faceIdentify : Form
 {
  public faceIdentify()
  {
   InitializeComponent();
   //Start by default in the middle of the screen    = ;
  }
  //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;

  //Form loading  private void faceIdentify_Load(object sender, EventArgs e)
  {
   //Showing as being detected   this. = this. = this. = this. = "Identifying";

   // Refresh the list of available cameras   videoDevices = new FilterInfoCollection();
   ();

   for (int i = 0; i < ; i++)
   {
    (videoDevices[i].());
   }


   if ( > 0)
     = 0;
    = 0;

   //Open the camera   openCamera();
  }

  //Open the camera  public void openCamera()
  {
   selectedPICIndex = ;

   selectedDeviceIndex = ;
   //Connect the camera.   videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceIndex].MonikerString);
    = [selectedDeviceIndex];
   // Enumerate all the pixels supported by the camera and set the photo to 1920*1080   foreach (VideoCapabilities capab in )
   {
    if (selectedPICIndex == 0)
    {
     if ( == 1920 &&  == 1080)
     {
       = capab;
      break;
     }
     if ( == 1280 &&  == 720)
     {
       = capab;
      break;
     }
    }
    else
    {
     if ( == 1280 &&  == 720)
     {
       = capab;
      break;
     }
    }
   }
    = videoSource;

   // set NewFrame event handler
   ();
  }

  /// <summary>
  /// Sign in button  /// First save the picture, then compare, get the id, query  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void qiandao_Click(object sender, EventArgs e)
  {
   Users users = FaceIdentifys(SavePicture());
   this. = ();
   this. = ;
   this. = ;
   this. = ;
   if ( != null)
   {
    this. = (, false);
   }

  }

  //Close the window  private void faceIdentify_FormClosing(object sender, FormClosingEventArgs e)
  {
   DialogResult r = ("Are you sure you want to exit the program?", "Operation prompt", , );
   if (r != )
   {

     = true;
   }
   ();//Stop the camera   ();
  }

  /// <summary>
  /// Face recognition  /// </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 (Api_Key, Secret_Key);
   var image1 = (filename);
   var result = (image1, new[] { "gr_test" }, 1, 1);
   //First determine whether the face is on it, and continue to see if there is a match, otherwise it is prompted to put the face on.   //Get the root node   JObject jo_result = (JObject)(());
   if ((string)jo_result["error_msg"] != null)
   {
    ("Sorry, please put your face on!", "hint", , );
   }
   else
   {
    //Face detected    //Get result node    JArray jo_age = (JArray)(jo_result["result"].ToString());
    foreach (var val in jo_age)
    {
     id = (((JObject)val)["uid"].ToString()); //Get uid     string scores = ((JObject)val)["scores"].ToString();//Get scores     int num1 = ("\n") + 2;
     int num2 = ("]")-8;
     ids = (num1, num2);
     scores_num =(ids);
    }
    if (scores_num > 80)
    {
     user = QueryUsersById(id);
     if ( != 0)
     {
      ("Successfully checked in, your information has been detected", "Operation prompt", , );
     }
     else
     {
      ("Sorry, the system has not detected information based on your face", "Operation prompt", , );
     }
    }
    else {
     ("Sorry, the system has not detected information based on your face", "Operation prompt", , );
    }

   }
   return user;

  }

  /// <summary>
  /// Save the picture  /// </summary>
  public string SavePicture()
  {
   if (videoSource == null)
   {
    return null;
   }

   Bitmap bitmap = ();
   //Picture name, year, month, day, hour, minute, second, millisecond.jpg   string fileName = ("yyyyMMddHHmmssff") + ".jpg";

   //Get the root directory of the project   string path = ;
   string picture = path + "\\picture\\" + fileName;
   //Save the image in the server   (picture, );
   ();
   return picture;
  }

  /// <summary>
  /// Query user information according to the number  /// </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 = (sql, , new SqlParameter("@id", id)))
   {
    if (())
    {
      = (reader[0].ToString());
      = reader[1].ToString();
      = Convert.ToInt32(reader[2]);
      = reader[3].ToString();
      = reader[4].ToString();
      = reader[5].ToString();
      = reader[6].ToString();
    }
   }
   return user;
  }

  //Cancel button  private void close_Click(object sender, EventArgs e)
  {
   //Stop the camera   ();
   ();
   welcome we = new welcome();
   ();
  }


 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.