khaledd khaledd

khaledd khaledd

  • NA
  • 3
  • 1.8k

An unhandled exception of type 'System.NullReferenceExceptio

Jun 3 2013 4:36 PM

hi when you click on botton Recognize it display a error as follows: An unhandled exception of type 'System.NullReferenceException' occurred in CameraCapture.exe Additional information: Object reference not set to an instance of an object

in part: name = recognizer.Recognize (ExtFaces [faceNo]) ToString ().; stringOutput [faceNo] = name;

here is the link the code part:

 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

            //for (int i = 0; i < NumNameLabels; i++)
            //{ 
            //NamePersons[i]=NameLabless[i]+"&"+IDLabless[i];
            //}

            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
                 {
                        //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");
                }
            else
            {
                btnNextRec.Enabled = true;
                btnPreviousRec.Enabled = true;
            }
        }

Answers (4)