如果condition为false,则再次开始检查|怎么做?

时间:2013-08-21 01:39:38

标签: c# loops for-loop

我有一个简单的问题。

我有一个识别面孔的程序

我想这样做,如果被识别的面孔被说成“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();

                }
}

这就是我的程序识别

的方式

2 个答案:

答案 0 :(得分:3)

您正在检测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!");
}

答案 1 :(得分:0)

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和内存。

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