C# - Emgu CV - 面部识别代码在EigenObjectRecognizer处停止执行并退出而不会出错

时间:2013-05-11 17:10:20

标签: c# emgucv face-recognition eigen

我正在进行人脸识别,当我运行代码时,它会在EigenObjectRecognizer初始化时停止执行并退出程序而不会出现任何错误。还有其他任何人遇到过同样的问题吗?如果你需要额外的代码我可以发布更多。我已经看到我的代码正常工作,直到识别器使用训练集中的数据进行训练

     EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
                       trainingImages.ToArray(),
                        NameLabless.ToArray(),
                        3000,
                       ref termCrit);
                       name = recognizer.Recognize(ExtFaces[faceNo]).ToString();

我用来从训练集加载的代码是

    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);
        }
    }

脸部识别器功能如下

      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;
        }
    }

当这个面部识别器方法被调用为事件时,它会一直工作,直到EigenObjectRecognizer被训练,然后它停止工作(退出运行)并且程序停止运行。

我期待你的回复,谢谢 西赛

1 个答案:

答案 0 :(得分:1)

在市中心用精神打了5个小时之后,我做了第一次使用try-catch块来获得一个调用堆栈的异常,我开始意识到保存到训练集的图像和捕获的图像被识别为待识别没有相同的大小。所以这就是为什么我的程序停止并退出而没有任何错误通知的原因。http://www.mediafire.com/view/?bfysqsze6n2zs9y是错误信息,它阻止我在eigenObjectRecognizer,我通过调整馈送到训练集的图像来解决它检测到的图像大小相同。