2013-08-21 35 views
1

我有一個簡單的問題。如果條件爲false,請再次開始檢查|怎麼做?

我有認識的面孔

我想這樣做,如果識別到的臉部是讓的說一個程序「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(); 

       } 
} 

那怎麼我的程序識別

+0

然後您可能想要在後臺線程上執行此操作,或者觸發一個信號爲真的事件 – Jonesopolis

+0

導致此檢查發生的原因是什麼?點擊按鈕? –

+0

如果您添加更多關於* why *的細節,您可以爲您提供更好的解決方案。在windows窗體應用程序等事件驅動的應用程序中,循環和檢查並不是最好的主意。 – Blorgbeard

回答

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

嗯我評論你的評論檢查我想做什麼,如果可能你可能會告訴我一個更好的情況?並且非常感謝您的幫助。 – user2701873

+0

因此,如果沒有找到「Muhand」這個代碼,它會繼續工作,直到找到它? – user2701873

+0

'Application.Idle'不斷提高,所以是的 - 它會在下一次處理幀時再次檢查。 – Blorgbeard

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

我只是看到你更新你的問題,我很抱歉,我無法幫助你。

+1

它不會花費任何內存,並且可以被簡化很多。你不需要'我' - 只需使用'while(true)'和'break'來代替。 – Blorgbeard

+0

你是對的:D – PengWu

+0

@彭武不用擔心;)謝謝你嘗試壽 – user2701873

相關問題