【问题标题】:If condition is false start checking again | How to do that?如果条件为假,重新开始检查 |怎么做?
【发布时间】:2013-08-21 01:39:37
【问题描述】:

我有一个简单的问题。

我有一个识别人脸的程序

我想这样做,如果识别出的人脸是“Muhand”,则显示一个消息框,否则返回并再次检查,直到识别出的人脸是“Muhand”。

这是我以前认识的函数。

namespace FaceReco
{
     public partial class FrmPrincipal : Form
     {
     //Declararation of all variables, vectors and haarcascades
    Image<Bgr, Byte> currentFrame;
    Capture grabber;
    HaarCascade face;
    HaarCascade eye;
    MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 0.5d, 0.5d);
    Image<Gray, byte> result, TrainedFace = null;
    Image<Gray, byte> gray = null;
    List<Image<Gray, byte>> trainingImages = new List<Image<Gray, byte>>();
    List<string> labels= new List<string>();
    List<string> NamePersons = new List<string>();
    int ContTrain, NumLabels, t;
    string name, names = null;

 public FrmPrincipal()
    {
        InitializeComponent();
        //Load haarcascades for face detection
        face = new HaarCascade("haarcascade_frontalface_default.xml");
        //eye = new HaarCascade("haarcascade_eye.xml");
        try
        {
            //Load of previus trainned faces and lamabels for each image
            string Labelsinfo = File.ReadAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt");
            string[] Labels = Labelsinfo.Split('%');
            NumLabels = Convert.ToInt16(Labels[0]);
            ContTrain = NumLabels;
            string LoadFaces;

            for (int tf = 1; tf < NumLabels+1; tf++)
            {
                LoadFaces = "face" + tf + ".bmp";
                trainingImages.Add(new Image<Gray, byte>(Application.StartupPath + "/TrainedFaces/" + LoadFaces));
                labels.Add(Labels[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 button1_Click(object sender, EventArgs e)
    {
        //Initialize the capture device
        grabber = new Capture(1);
        grabber.QueryFrame();
        //Initialize the FrameGraber event
        Application.Idle += new EventHandler(FrameGrabber);
        button1.Enabled = false;
    }

  void FrameGrabber(object sender, EventArgs e)
        {
            label3.Text = "0";
            //label4.Text = "";
            NamePersons.Add("");


            //Get the current frame form capture device
            currentFrame = grabber.QueryFrame().Resize(600, 600, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

                    //Convert it to Grayscale
                    gray = currentFrame.Convert<Gray, Byte>();

                    //Face Detector
                    MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
                  face,
                  1.1,
                  10,
                  Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                  new Size(15, 15));

                    //Action for each element detected
                    foreach (MCvAvgComp f in facesDetected[0])
                    {
                        t = t + 1;
                        result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                        //draw the face detected in the 0th (gray) channel with blue color
                        currentFrame.Draw(f.rect, new Bgr(Color.Red), 2);


                        if (trainingImages.ToArray().Length != 0)
                        {
                            //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(
                           trainingImages.ToArray(),
                           labels.ToArray(),
                           3000,
                           ref termCrit);

                        name = recognizer.Recognize(result);

                            //Draw the label for each face detected and recognized
                        currentFrame.Draw(name, ref font, new Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(Color.LightGreen));

                        }

                            NamePersons[t-1] = name;
                            NamePersons.Add("");


                        //Set the number of faces detected on the scene
                        label3.Text = facesDetected[0].Length.ToString();

                    }
                        t = 0;

                        //Names concatenation of persons recognized
                    for (int nnn = 0; nnn < facesDetected[0].Length; nnn++)
                    {
                        names = names + NamePersons[nnn] + ", ";
                    }
                    //Show the faces procesed and recognized
                    imageBoxFrameGrabber.Image = currentFrame;
                    label4.Text = names;
                    names = "";
                    //Clear the list(vector) of names
                    NamePersons.Clear();

                }
}

这就是我的程序识别的方式

【问题讨论】:

  • 你可能想在后台线程上执行此操作,或者触发一个表明 true 的事件
  • 发生这种检查的原因是什么?点击按钮?
  • 如果您添加更多关于为什么要这样做的详细信息,我们或许可以为您提供更好的解决方案。在 Windows 窗体应用程序之类的事件驱动应用程序中,循环和检查并不是最好的办法。
  • @SimonWhitehead 好吧,我按下一个按钮,这个按钮开始相机捕捉。相机捕捉将实时识别人脸。所以我认为这个事件发生在相机捕捉事件中
  • @Blorgbeard 这就是我想做的。我按下一个按钮,这个按钮开始相机捕捉。相机捕捉将实时识别人脸。我希望它在捕获时检查识别的面孔是否让说“Muhand”然后停止显示消息框,否则返回并再次检查谁被识别等等

标签: c# loops for-loop


【解决方案1】:

您似乎在Application.Idle 事件中检测到人脸。

我认为检查是否检测到特定面部的最佳位置就在您检测到它们之后!

例子:

//Names concatenation of persons recognized
bool foundMuhand = false;
for (int nnn = 0; nnn < facesDetected[0].Length; nnn++)
{
    // check for VIP
    if (NamePersons[nnn] == "Muhand") {
        foundMuhand = true;
    }
    names = names + NamePersons[nnn] + ", ";
}
//Show the faces procesed and recognized
imageBoxFrameGrabber.Image = currentFrame;
label4.Text = names;
names = "";
//Clear the list(vector) of names
NamePersons.Clear();

// show message box after the other UI is updated
if (foundMuhand) {
    MessageBox.Show("Hi Muhand!");
}

【讨论】:

  • 嗯我评论了你的评论检查我想做什么,如果可能的话你可能会告诉我一个更好的情况?非常感谢您的帮助。
  • 那么如果没有找到“Muhand”,那么使用此代码它会一直工作直到找到它?
  • Application.Idle 不断提升,所以是的 - 它会在下次处理帧时再次检查。
【解决方案2】:
int i=0;
while(i!=1)
{
    If (textbox1.text == "Don't Check")
    {
         MessageBox.Show("Textbox results were found");
         i=1;
    }
    else
    {
        //anything that you want to do
    }
}

我的方式,我一直用,但是会消耗很多cpu和内存。

我刚刚看到你更新你的问题,很抱歉我帮不了你。

【讨论】:

  • 它不会消耗任何内存,并且可以简化很多。您不需要 i - 只需使用 while (true)break 代替。
  • @PengWu 别担心 ;) 感谢您的尝试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-01
  • 1970-01-01
  • 2019-12-30
  • 1970-01-01
  • 2013-10-15
  • 2014-10-17
  • 1970-01-01
相关资源
最近更新 更多