user2701873
user2701873

Reputation:

If condition is false start checking again | How to do that?

I have a simple question.

I have a program that recognizes faces

I want to do this, if the recognized face is let say "Muhand" then show a messagebox otherwise go back and check again until the recognized face is "Muhand".

Here is the function I used to recognize.

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

                }
}

That's how my program is recognizing

Upvotes: 1

Views: 624

Answers (2)

Blorgbeard
Blorgbeard

Reputation: 103447

It appears that you are detecting faces in the Application.Idle event.

I think the best place to check whether a specific face was detected is right there, just after you detect them!

Example:

//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!");
}

Upvotes: 3

PengWu
PengWu

Reputation: 65

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

My way, I always use, but it will cost much cpu ,and memory.

I just see you update you question ,i am so sorry i can't help you.

Upvotes: 0

Related Questions