training button
This commit is contained in:
		
							parent
							
								
									3b2b5b8c45
								
							
						
					
					
						commit
						57ddf2a2d9
					
				
							
								
								
									
										43
									
								
								FaceRecognizer/MainForm.Designer.cs
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										43
									
								
								FaceRecognizer/MainForm.Designer.cs
									
									
									
										generated
									
									
									
								
							@ -31,6 +31,9 @@
 | 
			
		||||
            this.console = new System.Windows.Forms.RichTextBox();
 | 
			
		||||
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
 | 
			
		||||
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
 | 
			
		||||
            this.TrainBtn = new System.Windows.Forms.Button();
 | 
			
		||||
            this.nameTxtBx = new System.Windows.Forms.TextBox();
 | 
			
		||||
            this.RecconizeBtn = new System.Windows.Forms.Button();
 | 
			
		||||
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
 | 
			
		||||
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
 | 
			
		||||
            this.SuspendLayout();
 | 
			
		||||
@ -46,6 +49,7 @@
 | 
			
		||||
            this.console.Size = new System.Drawing.Size(776, 120);
 | 
			
		||||
            this.console.TabIndex = 0;
 | 
			
		||||
            this.console.Text = "";
 | 
			
		||||
            this.console.ZoomFactor = 2F;
 | 
			
		||||
            this.console.TextChanged += new System.EventHandler(this.console_TextChanged);
 | 
			
		||||
            // 
 | 
			
		||||
            // pictureBox1
 | 
			
		||||
@ -53,6 +57,7 @@
 | 
			
		||||
            this.pictureBox1.Location = new System.Drawing.Point(464, 12);
 | 
			
		||||
            this.pictureBox1.Name = "pictureBox1";
 | 
			
		||||
            this.pictureBox1.Size = new System.Drawing.Size(324, 300);
 | 
			
		||||
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
 | 
			
		||||
            this.pictureBox1.TabIndex = 1;
 | 
			
		||||
            this.pictureBox1.TabStop = false;
 | 
			
		||||
            // 
 | 
			
		||||
@ -64,19 +69,50 @@
 | 
			
		||||
            this.pictureBox2.TabIndex = 1;
 | 
			
		||||
            this.pictureBox2.TabStop = false;
 | 
			
		||||
            // 
 | 
			
		||||
            // Form1
 | 
			
		||||
            // TrainBtn
 | 
			
		||||
            // 
 | 
			
		||||
            this.TrainBtn.Location = new System.Drawing.Point(363, 249);
 | 
			
		||||
            this.TrainBtn.Name = "TrainBtn";
 | 
			
		||||
            this.TrainBtn.Size = new System.Drawing.Size(75, 23);
 | 
			
		||||
            this.TrainBtn.TabIndex = 2;
 | 
			
		||||
            this.TrainBtn.Text = "Train";
 | 
			
		||||
            this.TrainBtn.UseVisualStyleBackColor = true;
 | 
			
		||||
            this.TrainBtn.Click += new System.EventHandler(this.TrainBtn_Click);
 | 
			
		||||
            // 
 | 
			
		||||
            // nameTxtBx
 | 
			
		||||
            // 
 | 
			
		||||
            this.nameTxtBx.Location = new System.Drawing.Point(348, 167);
 | 
			
		||||
            this.nameTxtBx.Name = "nameTxtBx";
 | 
			
		||||
            this.nameTxtBx.Size = new System.Drawing.Size(100, 20);
 | 
			
		||||
            this.nameTxtBx.TabIndex = 3;
 | 
			
		||||
            // 
 | 
			
		||||
            // RecconizeBtn
 | 
			
		||||
            // 
 | 
			
		||||
            this.RecconizeBtn.Location = new System.Drawing.Point(363, 209);
 | 
			
		||||
            this.RecconizeBtn.Name = "RecconizeBtn";
 | 
			
		||||
            this.RecconizeBtn.Size = new System.Drawing.Size(75, 23);
 | 
			
		||||
            this.RecconizeBtn.TabIndex = 4;
 | 
			
		||||
            this.RecconizeBtn.Text = "Recognize";
 | 
			
		||||
            this.RecconizeBtn.UseVisualStyleBackColor = true;
 | 
			
		||||
            this.RecconizeBtn.Click += new System.EventHandler(this.RecconizeBtn_Click);
 | 
			
		||||
            // 
 | 
			
		||||
            // MainForm
 | 
			
		||||
            // 
 | 
			
		||||
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
 | 
			
		||||
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
 | 
			
		||||
            this.ClientSize = new System.Drawing.Size(800, 450);
 | 
			
		||||
            this.Controls.Add(this.RecconizeBtn);
 | 
			
		||||
            this.Controls.Add(this.nameTxtBx);
 | 
			
		||||
            this.Controls.Add(this.TrainBtn);
 | 
			
		||||
            this.Controls.Add(this.pictureBox2);
 | 
			
		||||
            this.Controls.Add(this.pictureBox1);
 | 
			
		||||
            this.Controls.Add(this.console);
 | 
			
		||||
            this.Name = "Form1";
 | 
			
		||||
            this.Name = "MainForm";
 | 
			
		||||
            this.Text = "Form1";
 | 
			
		||||
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
 | 
			
		||||
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
 | 
			
		||||
            this.ResumeLayout(false);
 | 
			
		||||
            this.PerformLayout();
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -85,6 +121,9 @@
 | 
			
		||||
        private System.Windows.Forms.RichTextBox console;
 | 
			
		||||
        private System.Windows.Forms.PictureBox pictureBox1;
 | 
			
		||||
        private System.Windows.Forms.PictureBox pictureBox2;
 | 
			
		||||
        private System.Windows.Forms.Button TrainBtn;
 | 
			
		||||
        private System.Windows.Forms.TextBox nameTxtBx;
 | 
			
		||||
        private System.Windows.Forms.Button RecconizeBtn;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -27,84 +27,98 @@ namespace FaceRecognizer
 | 
			
		||||
        List<string> imageLabels = new List<string>();
 | 
			
		||||
        List<int> imageIndicies = new List<int>();
 | 
			
		||||
        Rectangle[] detectedFaces;
 | 
			
		||||
        string trainImagesFolder = Application.StartupPath + "\\Train";
 | 
			
		||||
 | 
			
		||||
        public MainForm()
 | 
			
		||||
        {
 | 
			
		||||
            InitializeComponent();
 | 
			
		||||
            console.AppendText("Main OK!");            
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        void train(string personName)
 | 
			
		||||
        {            
 | 
			
		||||
            faceCascade = new CascadeClassifier("haarcascade_frontalface_alt_tree.xml");
 | 
			
		||||
            reconizer = new LBPHFaceRecognizer();
 | 
			
		||||
 | 
			
		||||
            foreach (var file in Directory.GetFiles(trainImagesFolder))
 | 
			
		||||
            {
 | 
			
		||||
                if (file.EndsWith("jpg"))
 | 
			
		||||
                {
 | 
			
		||||
                    console.AppendText("found: " + Path.GetFileNameWithoutExtension(file));
 | 
			
		||||
                    temp = new Image<Gray, byte>(file);
 | 
			
		||||
                    console.AppendText("loaded: " + Path.GetFileNameWithoutExtension(file));
 | 
			
		||||
                    temp._EqualizeHist();
 | 
			
		||||
 | 
			
		||||
                    var detected_faces = faceCascade.DetectMultiScale(temp
 | 
			
		||||
                        , 1.1
 | 
			
		||||
                        , 3
 | 
			
		||||
                        , new Size(24, 24)
 | 
			
		||||
                        , Size.Empty);
 | 
			
		||||
 | 
			
		||||
                    if (detected_faces.Length == 0) { console.AppendText("lenght zero"); continue; }
 | 
			
		||||
                    temp.ROI = detected_faces[0];                        
 | 
			
		||||
                    temp = temp.Copy();
 | 
			
		||||
                    temp = temp.Resize(100, 100, Inter.Cubic);
 | 
			
		||||
                    imageList.Add(temp);
 | 
			
		||||
                    imageLabels.Add(Path.GetFileNameWithoutExtension(file));
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            for (int i = 0; i < imageList.Count; i++)
 | 
			
		||||
            {
 | 
			
		||||
                imageIndicies.Add(i);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                reconizer.Train(imageList.ToArray(), imageIndicies.ToArray());
 | 
			
		||||
                reconizer.Write(personName);
 | 
			
		||||
                console.AppendText("Training done!\nFile Name: " + personName);
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception e) { console.AppendText("Error: " + e.Message); }                            
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        void recognize(string personName)
 | 
			
		||||
        {
 | 
			
		||||
            console.AppendText("Recognizing " + personName + "from local photos");
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                reconizer = new LBPHFaceRecognizer(1, 8, 8, 9, 65);
 | 
			
		||||
                faceCascade = new CascadeClassifier("faceCascade.xml");
 | 
			
		||||
                console.AppendText("created regocnizer\n" +
 | 
			
		||||
                                   "loading cascade\n" +
 | 
			
		||||
                                   "loading images");
 | 
			
		||||
                reconizer.Read("ss");
 | 
			
		||||
                reconizer = new LBPHFaceRecognizer();
 | 
			
		||||
                reconizer.Read(personName);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                foreach (var file in Directory.GetFiles(Application.StartupPath))
 | 
			
		||||
                foreach (var file in Directory.GetFiles(trainImagesFolder))
 | 
			
		||||
                {
 | 
			
		||||
 | 
			
		||||
                    if (file.EndsWith("jpg"))
 | 
			
		||||
                    {
 | 
			
		||||
                        Image<Gray, byte> temp2 = new Image<Gray, byte>(file);
 | 
			
		||||
                        pictureBox1.Image = temp2.ToBitmap();
 | 
			
		||||
                        pictureBox1.Image = temp2.Resize(pictureBox1.Width, pictureBox1.Height, Inter.Cubic).ToBitmap();
 | 
			
		||||
                        var result = reconizer.Predict(temp2);
 | 
			
		||||
                        console.AppendText(">>result:" + result.Label.ToString());
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                   
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception e)
 | 
			
		||||
            {
 | 
			
		||||
                console.AppendText("Error: " + e.Message);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
            //try
 | 
			
		||||
            //{
 | 
			
		||||
            //    foreach (var file in Directory.GetFiles(Application.StartupPath))
 | 
			
		||||
            //    {
 | 
			
		||||
            //        if (file.EndsWith("jpg"))
 | 
			
		||||
            //        {
 | 
			
		||||
            //            console.AppendText("found: " + Path.GetFileNameWithoutExtension(file));
 | 
			
		||||
            //            temp = new Image<Gray, byte>(file);
 | 
			
		||||
            //            console.AppendText("loaded: " + Path.GetFileNameWithoutExtension(file));
 | 
			
		||||
            //            temp._EqualizeHist();
 | 
			
		||||
 | 
			
		||||
            //            //var detected_faces = faceCascade.DetectMultiScale(temp
 | 
			
		||||
            //            //    , 1.1
 | 
			
		||||
            //            //    , 20
 | 
			
		||||
            //            //    , new Size(24, 24)
 | 
			
		||||
            //            //    , Size.Empty);
 | 
			
		||||
            //            //if (detected_faces.Length == 0) { console.AppendText("lenght zero"); continue; }
 | 
			
		||||
            //            //temp.ROI = detected_faces[0];
 | 
			
		||||
 | 
			
		||||
            //            temp = temp.Copy();
 | 
			
		||||
            //            temp = temp.Resize(100, 100, Inter.Cubic);
 | 
			
		||||
            //            imageList.Add(temp);
 | 
			
		||||
            //            imageLabels.Add(Path.GetFileNameWithoutExtension(file));                        
 | 
			
		||||
            //        }
 | 
			
		||||
            //    }
 | 
			
		||||
            //    for (int i = 0; i < imageList.Count; i++)
 | 
			
		||||
            //    {
 | 
			
		||||
            //        imageIndicies.Add(i);
 | 
			
		||||
            //    }
 | 
			
		||||
 | 
			
		||||
            //    try { reconizer.Train(imageList.ToArray(), imageIndicies.ToArray()); }
 | 
			
		||||
            //    catch (Exception e) { console.AppendText("Error: " + e.Message); }
 | 
			
		||||
            //    reconizer.Write("faceRecognizer");
 | 
			
		||||
                
 | 
			
		||||
            //}
 | 
			
		||||
            //catch (Exception e)
 | 
			
		||||
            //{
 | 
			
		||||
            //    console.AppendText("Error: " + e.Message);
 | 
			
		||||
            //}
 | 
			
		||||
        private void TrainBtn_Click(object sender, EventArgs e)
 | 
			
		||||
        {
 | 
			
		||||
            train(nameTxtBx.Text);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void RecconizeBtn_Click(object sender, EventArgs e)
 | 
			
		||||
        {
 | 
			
		||||
            recognize(nameTxtBx.Text);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void console_TextChanged(object sender, EventArgs e)
 | 
			
		||||
        {
 | 
			
		||||
            console.AppendText("\n");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user