Software Archive
Read-only legacy content
17061 Discussions

Sample C# and SQL Server

wiko_a_
Beginner
345 Views

hi everyone, i want to modify Sample Face tracking C# with sql server.

is it possible for me to use sql server to save face data?

can someone help me?

0 Kudos
3 Replies
Peter_O_Hanlon
Innovator
345 Views

Part of the problem people tend to have with the concept here is the use of the term face recognition database. This tends to suggest to people that the face information should be saved to a database somehow - and this isn't necessarily the case. Basically, the "database" is just a binary file so, in the sample - where you write to a file, you should just write out to a BLOB in the database. Your code would end up looking something like this:

private void SaveFaceRecognitionData(PXCMFaceData.RecognitionData recognitionData)
{
  PXCMFaceData.RecognitionModuleData moduleData = recognitionData.QueryRecognitionModule();
  int dataSize = moduleData.QueryDatabaseSize();
  byte[] buffer = new byte[dataSize];
  moduleData.QueryDatabaseBuffer(buffer);

  WriteFaceRecognitionDataToDatabase(buffer);
}

private void WriteFaceRecognitionDataToDatabase(byte[] buffer)
{
  using (SqlConnection connection = new SqlConnection(GlobalConnectionString); // You'll need to add this yourself
  {
    SqlTransaction transaction = connection.BeginTransaction();
    // You only want 1 row
    using (SqlCommand cmd = new SqlCommand("DELETE FROM FaceData")
    {
      cmd.Transaction = transaction;
      try
      {
        cmd.CommandText = "DELETE FROM FaceData";
        cmd.ExecuteNonQuery();
        cmd.CommandText = "NSERT INTO FaceData(Face) VALUES (@Buffer)";
        cmd.Parameters.Add("@Buffer@, SqlType.Image).Value = buffer;
        cmd.ExecuteNonQuery();
        cmd.Commit();
      }
      catch (Exception ex)
      {
        cmd.Rollback();
      }
    }
  }
}

private void LoadFaceRecognitionData(PXCMFaceConfiguration.RecognitionConfiguration instance)
{
  // Again, you would want to read from the database to populate buffer 
  instance.SetDatabaseBuffer(buffer);
}

What you may notice in the snippet above (I just typed it in the editor here so I apologise if there are a couple of typos), is that I have managed there only being one facial recognition database - there could be more so you would have to adjust as necessary. I could have done a SQL Upsert command but I decided to show you that you would have to maintain one entry per face data.

I haven't added the code to read the database back in (although I have added the call you would need to put in to populate the database), as it's pretty trivial to read the values back out of the database.

What is particularly noticeable is that you don't actually need to use these lines (from the SDK documentation)

rcfg.CreateStorage("MyDB", desc);
rcfg.UseStorage("MyDB");

 

0 Kudos
wiko_a_
Beginner
345 Views

thanks peter, i hope it'll work. I'll try it now

0 Kudos
wiko_a_
Beginner
345 Views

previously i try to insert an int value when face detected, and i notice that after application start, the connection in server explorer is disconnect.

when i open connection in form load, the app wont run

0 Kudos
Reply