private void LoadTrainnedFace() { try { LabelList.Clear(); TrainingImages.Clear(); // Load haarcascades for face detection Face = new HaarCascade("haarcascade_frontalface_default.xml"); // Load the number of trained faces var files = GetAllFiles(Application.StartupPath + "/TrainedFaces"); TrainedFacesCounter = files.Count; foreach (var file in files) { TrainingImages.Add(new Image <Gray, byte>(file.FullName)); LabelList.Add(file.Name.Split('@')[0]); } } catch { // By pass } }
public bool AddTrainingImage(string filename) { if (File.Exists(filename)) { if (!TrainingImages.Contains(filename)) { var fn = Path.GetFileNameWithoutExtension(filename); var extension = Path.GetExtension(filename); var fn_mark = Path.Combine(Path.GetDirectoryName(filename), fn + "_train" + extension); if (File.Exists(fn_mark)) { MarkedTrainingImages.Add(fn_mark); TrainingImages.Add(filename); return(true); } else { return(false); } } else { return(false); } } else { return(false); } }
void button2Click(object sender, EventArgs e) { if (textBox1.Text.Equals("")) { MessageBox.Show("vui long nhap ten truoc khi luu...."); return; } else { try { ContTrain = ContTrain + 1; grey = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC); MCvAvgComp[][] facesDetected = gray.DetectHaarCasscade( face, 1.2, 10, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUMING, new Size(20, 20)); foreach (MCvAvgComp f in faceDetected[0]) { TraniedFace = currentFrame.Copy(f.rect).Convert <Gray, byte>(); break; } TrainedFace = result.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC); TrainingImages.Add(TrainedFace); labels.Add(textBox1.Text); imageBox1.Image = TrainedFace; File.writeAllText(Application.StartupPath + "/TrainedFaces/Trainedlabels.txt", trainingImages.ToArray().Length.ToString() + "%"); for (int i = 1; i < trainingImages.ToArray().Length + 1; i++) { rainingImages.ToArray()[i - 1].Save(Application.StartupPath + "TrainedFaces/face" + i + ".bmp"); File.AppendAllText(Application.StartupPath + "/TrainedFaces/Trainedlabels.txt", labels.ToArray()[i - 1] + "%"); } MessageBox.Show("khuon mat duoc phat hien va them vao", "Training ok", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { MessageBox.Show("vui long chon chuc nang phat hien khuon mat truoc", "Training fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }