0

我正在研究人脸识别,当我运行代码时,它会在 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 被训练,然后它停止工作(退出运行)并且程序完全停止运行。

我会期待你的回应,谢谢 Sisay

4

1 回答 1

1

在市中心呆了 5 小时后,我第一次遇到异常情况,使用 try-catch 块获取调用堆栈,我意识到保存到训练集的图像和检测到的待识别图像并没有具有相同的大小。这就是为什么我的程序停止并退出而没有任何错误通知的原因。http://www.mediafire.com/view/?bfysqsze6n2zs9y是在 eigenObjectRecognizer 上阻止我的错误消息,我通过调整输入到训练集的图像大小来解决它,使其与检测到的图像大小相同认可。

于 2013-05-12T15:40:51.573 回答