示例#1
0
 /// <summary>
 /// Method for training the images from the database.
 /// </summary>
 public void TrainRecognizer()
 {
     try
     {
         using (var context = new FRModel())
         {
             var allFaces   = context.Images.ToList();
             var faceImages = new Image <Gray, byte> [allFaces.Count];
             var faceLabels = new int[allFaces.Count];
             for (var i = 0; i < allFaces.Count; i++)
             {
                 Stream stream = new MemoryStream();
                 stream.Write(allFaces[i].Face, 0, allFaces[i].Face.Length);
                 var faceImage = new Image <Gray, byte>(new Bitmap(stream));
                 faceImages[i] = faceImage.Resize(100, 100, Inter.Cubic);
                 faceLabels[i] = allFaces[i].UserId;
             }
             _faceRecognizer.Train(faceImages, faceLabels);
             _faceRecognizer.Save(_recognizerFilePath);
         }
     }
     catch (SqlException)
     {
         MessageBox.Show(@"Error occured while trying to train the data for facial recognition.",
                         @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
示例#2
0
 private void button2_Click(object sender, EventArgs e)
 {
     textBox1.Enabled  = false;
     textBox2.Enabled  = false;
     textBox3.Enabled  = false;
     comboBox2.Enabled = false;
     if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text) &&
         !string.IsNullOrWhiteSpace(textBox3.Text))
     {
         try
         {
             using (var context = new FRModel())
             {
                 var genreId = Convert.ToInt32(comboBox2.SelectedValue);
                 var gender  = context.Genders.Single(g => g.Id == genreId);
                 var visitor = new Visitor
                 {
                     Name    = textBox1.Text,
                     Surname = textBox2.Text,
                     CardId  = textBox3.Text,
                     Gender  = gender
                 };
                 context.Visitors.Add(visitor);
                 context.SaveChanges();
             }
         }
         catch (SqlException)
         {
             MessageBox.Show(@"Failed saving a visitor. Check your connection or try again.", @"Error",
                             MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
     else
     {
         MessageBox.Show(@"You need to enter obligatory fields in order to add a visitor.", @"Warning",
                         MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
     textBox1.Enabled  = true;
     textBox2.Enabled  = true;
     textBox3.Enabled  = true;
     comboBox2.Enabled = true;
     textBox1.Text     = "";
     textBox2.Text     = "";
     textBox3.Text     = "";
 }
示例#3
0
 public UserInfo(int id)
 {
     InitializeComponent();
     Id = id;
     using (var context = new FRModel())
     {
         var    user = context.Users.Include(u => u.Gender).Include(u => u.Sector).Single(u => u.PersonalNumber == Id);
         Bitmap bmp;
         using (var stream = new MemoryStream(user.Avatar))
         {
             bmp = new Bitmap(stream);
         }
         var resizedBmp = new Bitmap(bmp, new Size(200, 200));
         label1.Text       = user.Name;
         label2.Text       = user.Surname;
         label3.Text       = Convert.ToString(Id);
         label4.Text       = user.Gender.Name;
         label5.Text       = user.Sector.Name;
         pictureBox1.Image = resizedBmp;
     }
 }
示例#4
0
        public void Displayer()
        {
            var result = RecognizedLabels.GroupBy(x => x)
                         .ToDictionary(y => y.Key, y => y.Count())
                         .OrderByDescending(z => z.Value);

            foreach (var x in result)
            {
                if (x.Value < 3)
                {
                    continue;
                }
                using (var context = new FRModel())
                {
                    var    person = context.Users.First(u => u.PersonalNumber == x.Key);
                    Bitmap bmp;
                    using (var stream = new MemoryStream(person.Avatar))
                    {
                        bmp = new Bitmap(stream);
                    }
                    var resizedBmp = new Bitmap(bmp, new Size(64, 64));
                    var employee   = new Employee(person.PersonalNumber)
                    {
                        LabelText = $"{person.Name} {person.Surname}",
                        Image     = resizedBmp
                    };
                    flowLayoutPanel1.Controls.Add(employee);
                    var entrance = new EntranceRegister
                    {
                        Time   = DateTime.Now,
                        UserId = x.Key
                    };
                    context.EntranceRegisters.Add(entrance);
                    context.SaveChanges();
                }
            }
            RecognizedLabels.Clear();
        }
示例#5
0
        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();
        }