C# - Emgu Cv - 人脸识别 - 将面部的训练集加载到Access数据库中作为二进制到EigenObjectRecognizer进行人脸识别

时间:2013-05-09 12:55:07

标签: ms-access database-connection emgucv face-recognition training-data

我很难将Access数据库中的训练集加载到执行人脸识别的主窗体中。我将训练集及其名称和ID保存到数据库中,作为具有OLE对象格式的二进制数据。我用来更改,保存和读取数据库中的数据以及训练集的方法是

private static byte[] ConvertImageToBytes(Image InputImage)
    {
        using (Bitmap BmpImage = new Bitmap(InputImage))
        {
            using (MemoryStream MyStream = new MemoryStream())
            {
                BmpImage.Save(MyStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] ImageAsBytes = MyStream.ToArray();
                return ImageAsBytes;
            }
        }
    }

我用来将转换后的字节数据存储到数据库的方法如下:

   private void StoreData(byte[] ImageAsBytes,String NameStudent,String IDStudent)
    {

        if (DBConnection.State.Equals(ConnectionState.Closed))
            DBConnection.Open();
        try
        {

            //MessageBox.Show("Saving image at index : " + rowPosition);
            using (OleDbCommand insert = new OleDbCommand(String.Format("Insert INTO
               TrainingSet(rowPosition,StudentName,StudentID,StudentFace) values ('
                {0}','{1}','{2}',@StudentFace)", rowPosition, NameStudent, IDStudent),
                DBConnection))
            {
        OleDbParameter imageParameter = insert.Parameters.AddWithValue(@"StudentFace",
                   SqlDbType.Binary);
       imageParameter.Value = ImageAsBytes;
       imageParameter.Size = ImageAsBytes.Length;
       int rowsAffected = insert.ExecuteNonQuery();
       MessageBox.Show(String.Format("Data stored successfully in {0}
                                       Row",rowsAffected));
         }
            rowPosition++;

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            MessageBox.Show(ex.Message);
        }
        finally
        {
            RefreshDBConnection();
        }
      }

我用来读取这个二进制数据的方法如下:

    private Image ReadImageFromDB()
    {

        Image FetchedImg;
        if (rowNumber >= 0)
        {

        byte[] FetchedImgBytes = (byte[])LocalDataTable.Rows[rowNumber]["StudentFace"];
            MemoryStream stream = new MemoryStream(FetchedImgBytes);
            FetchedImg = Image.FromStream(stream);
            return FetchedImg;
        }
        else
        {

            MessageBox.Show("There are no images in the database yet.Please reconnect
                       or add some pictures.");
            return null;
        }

    }

我已成功将训练集/图像作为二进制数据保存到数据库中。问题是当我加载这些训练集以进行识别时。

        // Declaring the variables=====trainingImages is where the training sets are
        // loaded from the database NameLabels and IDLabels are text in the database
        // and where name and Id of subject
        //is saved.
      List<Image<Gray,byte>> trainingImages = new List<Image<Gray,byte>>();
      List<string> NameLables= new List<string>();
      List<string> IDLables = new List<string>();
      int ContTrain, NumNameLabels,NumIDLabels, t;

   //The training sets from the database are loaded in to the facerecognizer code as 
   //       follows

  public FaceRecognizer()
    {
        InitializeComponent();

        try
        {
            //Load previous trained and labels for each image from the database Here
        RefreshDBConnection();
        String[] NameLabels = (String[])LocalDataTable.Rows[rowNumber]["StudentName"];
        NumNameLabels = Convert.ToInt16(NameLabels[0]);
        String[] IDLabels = (String[])LocalDataTable.Rows[rowNumber]["StudentID"];
        NumIDLabels = Convert.ToInt16(IDLabels[0]);

        if (NumNameLabels == NumIDLabels)
         {
          ContTrain = NumNameLabels;
          string LoadFaces;
          // Converting the master image to a bitmap
          Image imageFromDB;
          Bitmap imageChangedToBitmap;
          // Normalizing it to grayscale
          Image<Gray, Byte> normalizedMasterImage;

          for (int tf = 1; tf < NumNameLabels + 1; tf++)
           {
              imageFromDB = ReadImageFromDB();
              //image loaded from the database is converted in to Bitmap and then 
              //convert the bitmap image in to Image<Gray,byte> for input to 
              //EigenObjectRecognizer(,,,) 
             imageChangedToBitmap = new Bitmap(imageFromDB);
              normalizedMasterImage = new Image<Gray, Byte>(imageChangedToBitmap);
              LoadFaces = String.Format("face{0}.bmp", tf);
              trainingImages.Add(normalizedMasterImage);
                //trainingImages.Add(new Image<Gray, byte>());
              NameLables.Add(NameLabels[tf]);
              IDLables.Add(IDLabels[tf]);
              rowNumber = rowNumber + 1;
            }
         }
       else
           MessageBox.Show("There's a conflict between Name labels and id labels");
        }
      catch (Exception e)
        {
   MessageBox.Show("Nothing in the database, please add at least a
               face.Train the database","Triained faces load",MessageBoxButtons.OK,
               MessageBoxIcon.Exclamation);
        }

    }

即使数据库中保存了面,我只会在表单加载时在catch中获取消息。我使用了EigenObjectRecognizer,如有必要,我会发布代码。

1 个答案:

答案 0 :(得分:0)

在加载面的部分,你没有通过face1,face2,face3等保存。所以你无法加载使用;

LoadFaces = String.Format("face{0}.bmp", tf);