public bool FolderCreator() { const string directory = @"C:\FRClient"; if (!Directory.Exists(directory)) { var di = Directory.CreateDirectory(directory); di.Attributes = FileAttributes.Directory | FileAttributes.Hidden; File.Create($"{directory}\\TrainingData.yaml"); TrainingDataAvailable = false; return(false); } if (!File.Exists($"{directory}\\TrainingData.yaml")) { File.Create($"{directory}\\TrainingData.yaml"); } if (new FileInfo($"{directory}\\TrainingData.yaml").Length == 0) { TrainingDataAvailable = false; return(false); } Recognizer = new RecognizerEngine($"{directory}\\TrainingData.yaml"); TrainingDataAvailable = true; return(true); }
private void button2_Click(object sender, EventArgs e) { var numbers = new List <int>(); using (var con = new FRModel()) { var users = con.Users.ToList(); numbers.AddRange(users.Select(user => user.PersonalNumber)); } textBox1.Enabled = false; textBox2.Enabled = false; textBox3.Enabled = false; comboBox1.Enabled = false; comboBox2.Enabled = false; pictureBox6.Enabled = false; if (GrayFaces.Count < 12) { MessageBox.Show(@"Please wait until the data set is ready", @"Wait a bit", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text) && !string.IsNullOrWhiteSpace(textBox3.Text) && Avatar != null) { var exist = false; foreach (var number in numbers) { if (number == Convert.ToInt32(textBox3.Text)) { exist = true; } } if (!exist) { try { using (var context = new FRModel()) { var genreId = Convert.ToInt32(comboBox2.SelectedValue); var sectorId = Convert.ToInt32(comboBox1.SelectedValue); var personalNumber = Convert.ToInt32(textBox3.Text); var gender = context.Genders.Single(g => g.Id == genreId); var sector = context.Sectors.Single(s => s.Id == sectorId); var user = new User { Name = textBox1.Text, Surname = textBox2.Text, PersonalNumber = personalNumber, Gender = gender, Sector = sector, Avatar = Avatar }; context.Users.Add(user); context.SaveChanges(); foreach (var grayFace in GrayFaces) { var converter = new ImageConverter(); var image = (byte[])converter.ConvertTo(grayFace.ToBitmap(), typeof(byte[])); context.Images.Add(new Image { Face = image, UserId = Convert.ToInt32(textBox3.Text) }); context.SaveChanges(); } } } catch (SqlException) { MessageBox.Show(@"Failed saving a user. Check your connection or try again.", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } var recognizer = new RecognizerEngine("C:\\FRClient\\TrainingData.yaml"); recognizer.TrainRecognizer(); } else { MessageBox.Show(@"User with the same personal number already exists.", @"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { MessageBox.Show(@"You need to enter obligatory fields in order to add a user.", @"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } textBox1.Enabled = true; textBox2.Enabled = true; textBox3.Enabled = true; comboBox1.Enabled = true; comboBox2.Enabled = true; pictureBox6.Enabled = true; textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; GrayFaces.Clear(); }