private void btSave_Click(object sender, EventArgs e) { // receive options from viewv PluginOptions.CameraDelayMs = Convert.ToInt32(NumCamDelay.Text); PluginOptions.UseImageCorrection = UseColorCorrection.Checked; // save options try { PluginOptions.SaveOptionsToXml(); // show that settings have been saved Color prevBtBackColor = btSave.BackColor; string prevBtMessage = btSave.Text; btSave.Enabled = false; btSave.BackColor = Color.FromArgb(202, 81, 0); btSave.Text = "Options saved."; var timer = new Timer { Interval = 2500 }; timer.Tick += delegate { btSave.BackColor = prevBtBackColor; btSave.Text = prevBtMessage; btSave.Enabled = true; timer.Stop(); timer.Dispose(); }; timer.Start(); } catch (Exception ex) { MessageBox.Show("An error occurred while saving options:\r\n" + ex); } }
private void DetectAndTrain_Click(object sender, EventArgs e) { using (var capture = new VideoCapture()) { Thread.Sleep(PluginOptions.CameraDelayMs); using (Image <Bgr, byte> imageFrame = capture.QueryFrame().ToImage <Bgr, byte>()) { if (imageFrame == null) { return; } using (Image <Gray, byte> grayframe = imageFrame.Convert <Gray, byte>()) { if (PluginOptions.UseImageCorrection) { grayframe._EqualizeHist(); } Rectangle[] part1; using (var classifier = new CascadeClassifier(PluginOptions.CascadesPath + "haarcascade_frontalface_default.xml")) { part1 = classifier.DetectMultiScale(grayframe, 1.1, 10); } if (part1.Length > 0) { Rectangle face = part1[0]; //выбор первого лица using (Image <Gray, byte> resultingImage = imageFrame.Copy(face).Convert <Gray, byte>().Resize(100, 100, Inter.Cubic)) { imageFrame.Draw(face, new Bgr(Color.Blue), 2); TestImage.Image = imageFrame; DetectedGrayFace.Image = resultingImage; #region Добавление лица if (string.IsNullOrEmpty(FaceName.Text)) { MessageBox.Show( "Сначала введите имя распознанного лица", "Имя лица не указано", MessageBoxButtons.OK, MessageBoxIcon.Warning ); return; } TrainedImages.Add(resultingImage); PluginOptions.PeopleFaces.Add( PluginOptions.PeopleFaces.Count + 1, FaceName.Text ); TrainedImages.Last().Save($"{PluginOptions.PluginPath}Faces\\face{TrainedImages.Count}.bmp"); #endregion } PluginOptions.SaveOptionsToXml(); using (FaceRecognizer recognizer = new EigenFaceRecognizer()) { recognizer.Train(TrainedImages.ToArray(), PluginOptions.PeopleFaces.Keys.ToArray()); recognizer.Write(PluginOptions.PluginPath + "SavedCascade.xml"); } MessageBox.Show("Лицо успешно добавлено", "Training OK", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("лиц не найдено", "Training OK", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } } }
private actionResult TrainFace(string[] parameters) { var ar = new actionResult(); #region Parameters parsing switch (parameters.Length) { case 0: { ar.setError("Path to image not specified."); return(ar); } case 1: { ar.setError("Face name not specified."); return(ar); } } Image <Gray, byte> grayImage; if (string.IsNullOrEmpty(parameters[0])) { using (var capture = new VideoCapture()) { Thread.Sleep(PluginOptions.CameraDelayMs); grayImage = capture.QueryFrame().ToImage <Gray, byte>(); } } else { try { grayImage = new Image <Gray, byte>(parameters[0]); } catch { ar.setError("Invalid path to image."); return(ar); } } if (PluginOptions.UseImageCorrection) { grayImage._EqualizeHist(); } #endregion Rectangle[] faces; using (var classifier = new CascadeClassifier($"{PluginOptions.CascadesPath}haarcascade_frontalface_default.xml")) { faces = classifier.DetectMultiScale(grayImage, 1.1, 10); } if (faces.Length == 0) { ar.setError("No face recognized."); return(ar); } using (Image <Gray, byte> faceImage = grayImage.Copy(faces[0]).Resize(100, 100, Inter.Cubic)) { MainCtl.TrainedImages.Add(faceImage); PluginOptions.PeopleFaces.Add(PluginOptions.PeopleFaces.Count + 1, parameters[1]); faceImage.Save($"{PluginOptions.PluginPath}Faces\\face{MainCtl.TrainedImages.Count}.bmp"); } PluginOptions.SaveOptionsToXml(); grayImage.Dispose(); using (FaceRecognizer recognizer = new EigenFaceRecognizer()) { recognizer.Train(MainCtl.TrainedImages.ToArray(), PluginOptions.PeopleFaces.Keys.ToArray()); recognizer.Write($"{PluginOptions.PluginPath}SavedCascade.xml"); } ar.setInfo($"Added face with name: {parameters[0]}."); return(ar); }