private void trainBtn_Click(object sender, EventArgs e)
        {
            try
            {
                //Trained face counter
                ContTrain = ContTrain + 1;

                //Get a gray frame from capture device
                gray = grabber.QueryGrayFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

                //Face Detector
                MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
                    face,
                    1.2,
                    10,
                    Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                    new Size(20, 20));

                //Action for each element detected
                foreach (MCvAvgComp f in facesDetected[0])
                {
                    TrainedFace = currentFrame.Copy(f.rect).Convert <Gray, byte>();
                    break;
                }

                //resize face detected image for force to compare the same size with the
                //test image with cubic interpolation type method
                TrainedFace = result.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                trainingImages.Add(TrainedFace);
                labels.Add(rollnoBox.Text);

                //Show face added in gray scale
                pictureBox2.Image = TrainedFace.ToBitmap();

                //Write the number of triained faces in a file text for further load
                File.WriteAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt", trainingImages.ToArray().Length.ToString() + "%");

                //Write the labels of triained faces in a file text for further load
                for (int i = 1; i < trainingImages.ToArray().Length + 1; i++)
                {
                    trainingImages.ToArray()[i - 1].Save(Application.StartupPath + "/TrainedFaces/face" + i + ".bmp");
                    File.AppendAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt", labels.ToArray()[i - 1] + "%");
                }

                Student student = new Student(nameBox.Text, rollnoBox.Text);

                foreach (var index in courseListBox.CheckedIndices)
                {
                    ListNode <Course> temp = loadcourses.getHead();
                    for (int i = 0; temp != null; i++, temp = temp.next)
                    {
                        if (i == (int)index)
                        {
                            break;
                        }
                    }
                    student.studentcourses.Add(temp.val);
                }
                student_list.Add(student);
                string studentfile = "student_file.bin";

                //serialize
                using (Stream stream = File.Open(studentfile, FileMode.Create))
                {
                    var bformatter = new BinaryFormatter();

                    bformatter.Serialize(stream, student_list);
                }


                MessageBox.Show(nameBox.ToString() + "Student registered", "Training OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception exc)
            {
                MessageBox.Show("Failed To Register " + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.Hide();
            Form1 form1 = new Form1();

            form1.ShowDialog();
        }
Exemple #2
0
        private void registerBtn_Click(object sender, EventArgs e)
        {
            if ((fallRadioBtn.Checked == false && springRadioBtn.Checked == false) || (textBox1.Text == "") || (textBox2.Text == "") || (textBox3.Text == ""))
            {
                MessageBox.Show("All fields must be filled");
            }
            else
            {
                Course course = new Course(textBox1.Text, textBox3.Text, textBox2.Text, (fallRadioBtn.Checked == true) ? fallRadioBtn.Text : springRadioBtn.Text);
                courses.Add(course);
                string            message = "Course Added Successfully. Do you want to add more?";
                string            title   = "Registered!";
                MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                DialogResult      result  = MessageBox.Show(message, title, buttons);
                if (result == DialogResult.Yes)
                {
                    textBox1.Text      = "";
                    textBox1.ForeColor = Color.Black;
                    textBox1.Text      = "Course Name";
                    textBox2.ForeColor = Color.Black;
                    textBox2.Text      = "Taught By:";
                    textBox3.ForeColor = Color.Black;
                    textBox3.Text      = "Course Code";
                }
                else
                {
                    string serializationFile = "course_file.bin";

                    //serialize
                    using (Stream stream = File.Open(serializationFile, FileMode.Create))
                    {
                        var bformatter = new BinaryFormatter();

                        bformatter.Serialize(stream, courses);
                    }
                    this.Hide();
                    this.Close();
                    Form1 form1 = new Form1();
                    form1.ShowDialog();
                }
                //try
                //{
                //    MemoryStream memoryStream = new MemoryStream();
                //    // create new BinaryFormatter
                //    BinaryFormatter binaryFormatter = new BinaryFormatter();
                //    // Serializes an object, or graph of connected objects, to the given stream.
                //    binaryFormatter.Serialize(memoryStream, course);
                //    byte[] byteArray = memoryStream.ToArray();
                //    // Open file for writing
                //    string filename = "course_file";
                //    FileStream fileStream = new FileStream(filename, FileMode.Append, FileAccess.Write);
                //    // Writes a block of bytes to this stream using data from a byte array.
                //    fileStream.Write(byteArray.ToArray(), 0, byteArray.Length);
                //    // close file stream
                //    fileStream.Close();
                //    // cleanup
                //    memoryStream.Close();
                //    memoryStream.Dispose();
                //    memoryStream = null;
                //    byteArray = null;
                //}
                //catch (Exception _Exception)
                //{
                //    // Error
                //    Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
                //}
            }
        }