public LogIn() { InitializeComponent(); try { myRecognizer = new MEigenRecognizer("" + Environment.GetEnvironmentVariable("appdata") + "/Project_1_1/holdRecFilePath/thephotos.yml"); sqlite_conn = new SQLiteConnection("Data Source=" + Environment.GetEnvironmentVariable("appdata") + "/Project_1_1/Project.db ; Version =3;"); sqlite_conn.Open(); sqlite_cmd = sqlite_conn.CreateCommand(); //create command sqlite_cmd.CommandText = "select image_path , doctor_id from doctorsFaces ;"; //datareader to read from db sqlite_datareader = sqlite_cmd.ExecuteReader(); while (sqlite_datareader.Read()) { image.Add(sqlite_datareader.GetString(0)); Labels.Add(sqlite_datareader.GetString(1)); } sqlite_cmd.ExecuteNonQuery(); sqlite_conn.Close(); sqlite_datareader.Close(); for (int i = 0; i < image.Count; i++) { // LoadFaces = "face" + i + ".bmp"; trainingImagesHolder.Add(new Image <Gray, byte>(image[i])); labelsHolder.Add(Labels[i]); } // sqlite_conn.Close(); //Now Load the images from the TrainedFaces Folder to our List that the images to be used in the code later //And the labels to our List that holds the lables to be used later . //Now after wwe added the photos to our training set holder and the lables integerLablesHolder = new int[labelsHolder.ToArray().Length]; for (int i = 0; i < labelsHolder.ToArray().Length; i++) { string[] arrayLablesHolder = labelsHolder.ToArray(); Int32.TryParse(arrayLablesHolder[i], out integerLablesHolder[i]); } myRecognizer.TrainRecognizer(trainingImagesHolder.ToArray(), integerLablesHolder); } catch (Exception ex) { } }
private void pictureBox1_Click(object sender, EventArgs e) { //--------------------------------------------------------------------------------------------------------------------------------- try { myStudentRecognizer = new MEigenRecognizer("" + Environment.GetEnvironmentVariable("appdata") + "/Project_1_1/holdRecFilePath/thephotos.yml"); sqlite_conn = new SQLiteConnection("Data Source=" + Environment.GetEnvironmentVariable("appdata") + "/Project_1_1/Project.db ; Version =3;"); //------------------------------------------------------------------------- //here we ghet the current subject from the databse sqlite_conn.Open(); sqlite_cmd = sqlite_conn.CreateCommand(); int d1 = datetime.Hour; sqlite_cmd.CommandText = "select subj_id from classRoomT where place = '15' and day = '" + System.DateTime.Now.ToString("dddd") + "' " + "and " + d1 + "between fromT and toT"; sqlite_datareader = sqlite_cmd.ExecuteReader(); while (sqlite_datareader.Read()) { subj = sqlite_datareader.GetString(0); } sqlite_conn.Close(); sqlite_datareader.Close(); //------------------------------------------- //Now get the Students that study this subject sqlite_conn.Open(); sqlite_cmd = sqlite_conn.CreateCommand(); sqlite_cmd.CommandText = "select student_id from studentSubjects where subject_id ='" + subj + "';"; sqlite_datareader = sqlite_cmd.ExecuteReader(); while (sqlite_datareader.Read()) { m.Add(int.Parse(sqlite_datareader.GetString(0))); } sqlite_conn.Close(); sqlite_datareader.Close(); //---------------------------------------------- //Now we retrieve the photos paths and the labels for the found students for (int i = 0; i < m.Count; ++i) { sqlite_conn.Open(); sqlite_cmd = sqlite_conn.CreateCommand(); sqlite_cmd.CommandText = "select image_path , student_id from studentsFaces where student_id = " + m[i]; sqlite_datareader = sqlite_cmd.ExecuteReader(); while (sqlite_datareader.Read()) { image.Add(sqlite_datareader.GetString(0)); Labels.Add(sqlite_datareader.GetString(1)); } sqlite_cmd.ExecuteNonQuery(); //close connections sqlite_conn.Close(); sqlite_datareader.Close(); } //------------------------------------------------------------------------ //Now we get the photos saved in the paths ant convert it to Gray images to be able to use it in recognition //the add the labels to to labels holder for (int i = 0; i < image.Count; i++) { // LoadFaces = "face" + i + ".bmp"; trainingImagesHolder.Add(new Image <Gray, byte>(image[i])); labelsHolder.Add(Labels[i]); } //---------------------------------------------------------------------------------------------- //Now after we add the training photos and the labels to TrainingImageHolder and LAbelsHolder //we parse labels to intger to be able to path them to the recognizer integerLablesHolder = new int[labelsHolder.ToArray().Length]; for (int i = 0; i < labelsHolder.ToArray().Length; i++) { string[] arrayLablesHolder = labelsHolder.ToArray(); Int32.TryParse(arrayLablesHolder[i], out integerLablesHolder[i]); } //-------------------------------------------------------------------------------------------------------------------------------- } catch (Exception ex) { // MessageBox.Show(ex.Message); } try { myStudentRecognizer.TrainRecognizer(trainingImagesHolder.ToArray(), integerLablesHolder); Atten attendance = new Atten(); attendance.Show(); this.Hide(); } catch (Exception error) { //MessageBox.Show(e.ToString()); NoLecture no = new NoLecture(); no.Show(); } }
private void AddaFaceToTrainingSet_Click(object sender, EventArgs e) { try { trainCascadeClassifier = new CascadeClassifier("C:/Emgu/emgucv-windesktop 3.2.0.2682/opencv/data/haarcascades/haarcascade_frontalface_default.xml"); using (var currentFrame = _capture.QueryFrame().ToImage <Bgr, Byte>()) { if (currentFrame != null) { var grayframe = currentFrame.Convert <Gray, byte>(); var faces = trainCascadeClassifier.DetectMultiScale(grayframe, 1.1, 10, Size.Empty); //the actual face detection happens here foreach (var face in faces) { grayframe.Draw(face, new Gray(1.523), 3); //the detected face(s) is highlighted here using a box that is drawn around it/them } grayframe = grayframe.Resize(100, 100, Inter.Cubic); TrainedFace = grayframe; } } trainingImagesHolder.Add(TrainedFace); labelsHolder.Add(textBox1.Text); //Show face added in gray scale imageBox1.Image = TrainedFace; //Write the number of triained faces in a file text for further load // File.WriteAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt", trainingImagesHolder.ToArray().Length.ToString() + "%"); sqlite_cmd = sqlite_conn.CreateCommand(); sqlite_conn.Open(); MessageBox.Show("insert into studentsFaces values (" + int.Parse(STUid.Text) + ",'" + Application.StartupPath + "\\TrainedFaces" + LoadFaces + "')"); sqlite_cmd.CommandText = "insert into studentsFaces values (" + int.Parse(STUid.Text) + ",'" + Application.StartupPath + "\\TrainedFaces" + LoadFaces + "')"; sqlite_conn.Close(); //Write the labels of triained faces in a file text for further load /* for (int i = 1; i < trainingImagesHolder.ToArray().Length + 1; i++) * { * trainingImagesHolder.ToArray()[i - 1].Save(Application.StartupPath + "/TrainedFaces/face" + i + ".bmp"); * File.AppendAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt", labelsHolder.ToArray()[i - 1] + "%"); * }*/ MessageBox.Show(textBox1.Text + "´s face detected and added :)", "Training OK", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { MessageBox.Show("Enable the face detection first", "Training Fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } //---------------------------------------------------------------------------------------------------------------- //Now after we added the new images and lables to our training set we will train our eigen recognizer integerLablesHolder = new int[labelsHolder.ToArray().Length]; for (int i = 0; i < labelsHolder.ToArray().Length; i++) { string[] arrayLablesHolder = labelsHolder.ToArray(); // integerLablesHolder[i]= Convert.ToInt32(arrayLablesHolder[i]); Int32.TryParse(arrayLablesHolder[i], out integerLablesHolder[i]); } myRecognizer.TrainRecognizer(trainingImagesHolder.ToArray(), integerLablesHolder); }
public Atten() { InitializeComponent(); try { myRecognizer = new MEigenRecognizer("" + Environment.GetEnvironmentVariable("appdata") + "/Project_1_1/holdRecFilePath/thephotos.yml"); sqlite_conn = new SQLiteConnection("Data Source=" + Environment.GetEnvironmentVariable("appdata") + "/Project_1_1/Project.db ; Version =3;"); String s = ""; DateTime dateTime = new DateTime(); sqlite_conn.Open(); sqlite_cmd = sqlite_conn.CreateCommand(); int d = datetime.Hour; Console.WriteLine(System.DateTime.Now.ToString("dddd")); sqlite_cmd.CommandText = "select subj_id from classRoomT where place = '15' and day = '" + System.DateTime.Now.ToString("dddd") + "' " + "and " + d + "between fromT and toT"; sqlite_datareader = sqlite_cmd.ExecuteReader(); while (sqlite_datareader.Read()) { subj = sqlite_datareader.GetString(0); } sqlite_conn.Close(); sqlite_datareader.Close(); // Console.WriteLine("subj" +subj); /* if (subj == "") { * NoLecture no = new NoLecture(); * no.Show(); * this.Hide(); * }*/ sqlite_conn.Open(); sqlite_cmd = sqlite_conn.CreateCommand(); sqlite_cmd.CommandText = "select student_id from studentSubjects where subj_id ='" + subj + "';"; sqlite_datareader = sqlite_cmd.ExecuteReader(); while (sqlite_datareader.Read()) { m.Add(int.Parse(sqlite_datareader.GetString(0))); } sqlite_conn.Close(); sqlite_datareader.Close(); for (int i = 0; i < m.Count; ++i) { sqlite_conn.Open(); sqlite_cmd = sqlite_conn.CreateCommand(); sqlite_cmd.CommandText = "select image_path , student_id from studentsFaces where student_id = " + m[i]; sqlite_datareader = sqlite_cmd.ExecuteReader(); while (sqlite_datareader.Read()) { image.Add(sqlite_datareader.GetString(0)); Labels.Add(sqlite_datareader.GetString(1)); } sqlite_cmd.ExecuteNonQuery(); //close connections sqlite_conn.Close(); sqlite_datareader.Close(); } for (int i = 0; i < image.Count; i++) { // LoadFaces = "face" + i + ".bmp"; trainingImagesHolder.Add(new Image <Gray, byte>(image[i])); labelsHolder.Add(Labels[i]); } // sqlite_conn.Close(); //Now Load the images from the TrainedFaces Folder to our List that the images to be used in the code later //And the labels to our List that holds the lables to be used later . //Now after wwe added the photos to our training set holder and the lables integerLablesHolder = new int[labelsHolder.ToArray().Length]; for (int i = 0; i < labelsHolder.ToArray().Length; i++) { string[] arrayLablesHolder = labelsHolder.ToArray(); Int32.TryParse(arrayLablesHolder[i], out integerLablesHolder[i]); } myRecognizer.TrainRecognizer(trainingImagesHolder.ToArray(), integerLablesHolder); } catch (Exception ex) { } }