Click here to Skip to main content
11,932,446 members (61,635 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Image-Recognition Emgu-CV , +
0 down vote favorite

I was working on Face Recognition code. After training the database and calling EigenObjectRecognizer, the result is a black image with unrecognized label.While the code running it looks like the following .And as shown in the picture the detected and supposed to be recognized and extracted face in the image box is totally black. And the input image for recognition is exactly the same as the one the database is trained with.So why it has kept giving Unknown or Unrecognized result. Part of the code looks

Images from the training set loaded as

public FaceRecognizer()
        ContTrain = ContTrain + 1;
        //Load previous trained and labels for each image from the database Here
        string NameLabelsinfo = File.ReadAllText(Application.StartupPath +
        string[] NameLabels = NameLabelsinfo.Split('%');
        NumNameLabels = Convert.ToInt16(NameLabels[0]);
        string IDLabelsinfo = File.ReadAllText(Application.StartupPath +
        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)));
    catch (Exception e)
        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);
The face recognizer method looks like
  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

       var faces = grayframe.DetectHaarCascade(haar, ScaleIncreaseRate,
                            new Size(WindowsSize, WindowsSize))[0];
    if (faces.Length > 0 && trainingImages.ToArray().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);
            //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);
            ExtFaces[faceNo] = ExtFaces[faceNo].Resize(100, 100,
            //TermCriteria for face recognition with numbers of trained images
            //    like maxIteration
            MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
                //Eigen face recognizer
                    EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
                     ref termCrit);
           stringOutput[faceNo] = recognizer.Recognize(ExtFaces[faceNo]);
        pbExtractedFaces.Image = ExtFaces[0].ToBitmap(); //draw the face detected
              // in the 0th (gray) channel with blue color

        if (stringOutput[0] == "")
                label1.Text = "Unknown";
                label9.Text = "";
            //Draw the label for each face detected and recognized
                //string[] label = stringOutput[faceNo].Split(',');
                label1.Text = "Known";
               // for (int i = 0; i < 2; i++)
                label9.Text = stringOutput[0];
                    //label7.Text = label[1];
    if (faceNo == 0)
            MessageBox.Show("No face detected");
        btnNextRec.Enabled = true;
        btnPreviousRec.Enabled = true;
The training set is trained with detected faces as follows
 private void saveFaceToDB_Click(object sender, EventArgs e)
     abd = (Bitmap) pbExtractedFaces.Image;
     TrainedFaces = new Image<Gray, byte>(abd);
     //Write the number of trained faces in a file text for further load
    File.WriteAllText(Application.StartupPath + "/TrainedFaces
    /TrainedNameLables.txt", trainingImages.ToArray().Length + "%");
    File.WriteAllText(Application.StartupPath + "/TrainedFaces
     /TrainedIDLables.txt", trainingImages.ToArray().Length + "%");
    //Write the labels of trained faces in a file text for further load
    for (int i = 1; i < trainingImages.ToArray().Length + 1; i++)
       trainingImages.ToArray()[i - 1].Save(String.Format("{0}/TrainedFaces
         /face{1}.bmp", Application.StartupPath, i));
        File.AppendAllText(Application.StartupPath + "/TrainedFaces
   /TrainedIDLables.txt", NameLabless.ToArray()[i - 1] + "%");
    File.AppendAllText(Application.StartupPath + "/TrainedFaces
   /TrainedNameLables.txt", IDLabless.ToArray()[i - 1] + "%");
MessageBox.Show(StudentName.Text + "´s face detected and added :)", "Training
    OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
Posted 12-May-13 13:02pm
Member 10045355 28-May-13 9:59am
hi I have problem win I clic recognize eroor :An unhandled exception of type 'Emgu.CV.Util.CvException' occurred in Emgu.CV.dll

Additional information: OpenCV: Different sizes of objects
Member 10045355 28-May-13 10:01am
plzzzzzzzz help me
Member 10045355 28-May-13 16:25pm
hi I have problem :An unhandled exception of type 'Emgu.CV.Util.CvException' occurred in Emgu.CV.dll
Additional information: OpenCV: Different sizes of objects
sizusuzu 30-May-13 18:47pm
You have to check the size of images in your training set and input image to the recognizer.May be you will get a better help if you post your question?
Member 10045355 1-Jun-13 13:58pm
thank you I solved by resizing but I have problem I have eroor :An unhandled exception of type 'System.NullReferenceException' occurred in CameraCapture.exe

Additional information: La référence d'objet n'est pas définie à une instance d'un objet.

name = recognizer.Recognize(ExtFaces[faceNo]).ToString();
stringOutput[faceNo] = name; please help me thanks

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 12 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100