public Training() { InitializeComponent(); TooMany.Hide(); RemainingImages.Hide(); PicLabel.Hide(); Start.Enabled = false; Stop.Enabled = false; Save.Enabled = false; Client.Connect(); if (Client.Request("start") == "ERROR") { MessageBox.Show("El servidor no responde."); Application.Exit(); } try { FaceDetected = new HaarCascade("haarcascade_frontalface_default.xml"); } catch (Exception e) { MessageBox.Show(e.Message); Application.Exit(); } Camera = new Capture(); Application.Idle += new EventHandler(DisplayFrame); }
void DisplayFrame(object sender, EventArgs e) { if (!Processing && NameBox.Text != string.Empty && NameBox.Text.Length > 2) { Start.Enabled = true; } Image <Bgr, Byte> Frame = Camera.QueryFrame().Resize(400, 300, INTER.CV_INTER_CUBIC); CamView.Image = Frame; Image <Gray, byte> GrayFace = Frame.Convert <Gray, Byte>(); MCvAvgComp[][] faceDetectedShow = GrayFace.DetectHaarCascade(FaceDetected, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20)); int faces = faceDetectedShow[0].Length; if (faces > 1) { TooMany.Show(); return; } TooMany.Hide(); if (faces == 0) { return; } var f = faceDetectedShow[0][0]; Image <Gray, byte> Result = Frame.Copy(f.rect).Convert <Gray, Byte>().Resize(100, 100, INTER.CV_INTER_CUBIC); switch (PicturesToTake) { case 9: FaceBox1.Image = Result; Save1.Checked = true; break; case 8: Result2 = Result; FaceBox2.Image = Result; break; case 7: Result3 = Result; FaceBox3.Image = Result; break; case 6: Result4 = Result; FaceBox4.Image = Result; break; case 5: Result5 = Result; FaceBox5.Image = Result; break; case 4: Result6 = Result; FaceBox6.Image = Result; break; case 3: Result7 = Result; FaceBox7.Image = Result; break; case 2: Result8 = Result; FaceBox8.Image = Result; break; case 1: Result9 = Result; FaceBox9.Image = Result; break; } Frame.Draw(f.rect, new Bgr(Color.Green), 3); string cmd = "test?" + (PicturesToTake == MaxPictures ? "T?" : "F?") + NameBox.Text.Trim() + "?" + Convert.ToBase64String(Result.Bytes); string[] reply = Client.Request(cmd).Split('?'); if (reply[0] == "OK") { if (reply[1] != "") { Frame.Draw(reply[1], ref Font, new Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(Color.Black)); } if (Processing) { if (reply[2] == "+") { switch (PicturesToTake) { case 8: Save2.Checked = true; break; case 7: Save3.Checked = true; break; case 6: Save4.Checked = true; break; case 5: Save5.Checked = true; break; case 4: Save6.Checked = true; break; case 3: Save7.Checked = true; break; case 2: Save8.Checked = true; break; case 1: Save9.Checked = true; break; } } PicturesToTake--; RemainingImages.Text = PicturesToTake.ToString(); if (PicturesToTake == 0) { StopTraining(); } } } }