Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
I was working on Face Recognition and when i run the code it stops execution at the point where EigenObjectRecognizer is initialized and exits the program with out any error.Have any one else faced the same problem ever before?If you need additional codes i can post more. I have seen my code working until the point where the recognizer trained with data in the training set
C#
EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
                       trainingImages.ToArray(),
                        NameLabless.ToArray(),
                        3000,
                       ref termCrit);
                       name = recognizer.Recognize(ExtFaces[faceNo]).ToString();

The code that i have used to load from the training set is
C#
public FaceRecognizer()
    {
        InitializeComponent();

        try
        {
            ContTrain = ContTrain + 1;
            //Load previous trained and labels for each image from the database Here
            string NameLabelsinfo = File.ReadAllText(Application.StartupPath +
                          "/TrainedFaces/TrainedNameLables.txt");
            string[] NameLabels = NameLabelsinfo.Split('%');
            NumNameLabels = Convert.ToInt16(NameLabels[0]);
            string IDLabelsinfo = File.ReadAllText(Application.StartupPath +
                "/TrainedFaces/TrainedNameLables.txt");
            string[] IDLables = IDLabelsinfo.Split('%');
            NumIDLabels = Convert.ToInt16(IDLables[0]);


            if (NumNameLabels == NumIDLabels)
            {
                ContTrain = NumNameLabels;
                string LoadFaces;
                // Converting the master image to a bitmap

                for (int tf = 1; tf < NumNameLabels + 1; tf++)
                {
                    LoadFaces = String.Format("face{0}.bmp", tf);
                    trainingImages.Add(new Image<Gray, byte>(String.Format("
                    {0}/TrainedFaces/{1}", Application.StartupPath,
                            LoadFaces)));
                    IDLabless.Add(IDLables[tf]);
                    NameLabless.Add(NameLabels[tf]);

                }
            }
        }
        catch (Exception e)
        {
            //MessageBox.Show(e.ToString());
            MessageBox.Show("Nothing in binary database, please add at least a
            face(Simply train the prototype with the Add
                   Face Button).", "Triained faces load", MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation);
        }
    }

And the face recognizer function is as follows
C#
private void RecognizeFaces()
  {
      //detect faces from the gray-scale image and store into an array of type
       //            'var',i.e 'MCvAvgComp[]'

      Image<Gray, byte> grayframe = GetGrayframe();
      //Assign user-defined Values to parameter variables:
      MinNeighbors = int.Parse(comboBoxMinNeigh.Text);  // the 3rd parameter
      WindowsSize = int.Parse(textBoxWinSiz.Text);   // the 5th parameter
      ScaleIncreaseRate = Double.Parse(comboBoxScIncRte.Text); //the 2nd parameter



      var faces = grayframe.DetectHaarCascade(haar, ScaleIncreaseRate, MinNeighbors,
                              HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                              new Size(WindowsSize, WindowsSize))[0];

      if (faces.Length > 0)
      {
          Bitmap ExtractedFace;   //empty
          ExtFaces = new Image<Gray, byte>[faces.Length];

          faceNo = 0;

          foreach (var face in faces)
          {
              // ImageFrame.Draw(face.rect, new Bgr(Color.Green), 3);
              t = t + 1;
              //set the size of the empty box(ExtractedFace) which will later
               //        contain the detected face
              ExtractedFace = new Bitmap(face.rect.Width, face.rect.Height);

              ExtFaces[faceNo] = new Image<Gray, byte>(ExtractedFace);
                  //= newExtractedImage;
              ExtFaces[faceNo] = ExtFaces[faceNo].Resize(100, 100,
                  Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
              //TermCriteria for face recognition with numbers of trained images
               //             like maxIteration
              MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
              if (trainingImages.ToArray().Length != 0)
              {
                  //Eigen face recognizer
                  EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
                   trainingImages.ToArray(),
                  NameLabless.ToArray(),
                   3000,
                   ref termCrit);
                   name = recognizer.Recognize(ExtFaces[faceNo]).ToString();
                  stringOutput[faceNo] = name;
              }
              faceNo++;
          }

          pbExtractedFaces.Image = ExtFaces[0].ToBitmap(); //draw the face detected
                   in the 0th (gray) channel with blue
                              color
          t = 0;

          if (stringOutput[0] == null)
              {
                  label1.Text = "Unknown";
                  label9.Text = "";
              }
              //Draw the label for each face detected and recognized
          else
           {
                 label1.Text = "Known";
                 label9.Text = stringOutput[0];

           }
      }
      if (faceNo == 0)
          {
              MessageBox.Show("No face detected");
          }
      else
      {
          btnNextRec.Enabled = true;
          btnPreviousRec.Enabled = true;
      }
  }


When this face recognizer method is called as an event it works until the point where EigenObjectRecognizer is trained and then it stops working(Exit running) and the program stops running at all.

I will look forward for your response,Thanks Sisay
Posted

1 solution

After 5 hrs at downtown with down spirit, i did the first chance exceptions to get a call stack using try-catch block and i came to realize that the images saved to the training set and image captured detected to-be-recognized didn't have the same size. So that was the reason why my program stopped and exits with out any error notification.This captured picture was the error message that stops me at the eigenObjectRecognizer and i solved it by resizing the images that are fed to the training set to have the same size with the image detected to be recognized.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900