From 57ddf2a2d9d0ab3463c304d73c184a3a3b8b9da0 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Thu, 20 Sep 2018 12:48:50 +0300 Subject: [PATCH 1/3] training button --- FaceRecognizer/MainForm.Designer.cs | 43 +++++++++- FaceRecognizer/MainForm.cs | 118 ++++++++++++++++------------ 2 files changed, 107 insertions(+), 54 deletions(-) diff --git a/FaceRecognizer/MainForm.Designer.cs b/FaceRecognizer/MainForm.Designer.cs index 528ac3d..fdf74ba 100644 --- a/FaceRecognizer/MainForm.Designer.cs +++ b/FaceRecognizer/MainForm.Designer.cs @@ -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; } } diff --git a/FaceRecognizer/MainForm.cs b/FaceRecognizer/MainForm.cs index faef2ea..01dfb1e 100644 --- a/FaceRecognizer/MainForm.cs +++ b/FaceRecognizer/MainForm.cs @@ -27,84 +27,98 @@ namespace FaceRecognizer List imageLabels = new List(); List imageIndicies = new List(); Rectangle[] detectedFaces; + string trainImagesFolder = Application.StartupPath + "\\Train"; public MainForm() { InitializeComponent(); - console.AppendText("Main OK!"); + 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(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 temp2 = new Image(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()); + console.AppendText(">>result:" + result.Label.ToString()); } } - } catch (Exception e) { - console.AppendText("Error: " + e.Message); + 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(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"); } + + } } From 185d221eab7fce71c985e781f93b49c69870938b Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Fri, 21 Sep 2018 15:23:33 +0300 Subject: [PATCH 2/3] BackWorkers, photo folder --- FaceRecognizer.sln | 6 ++ FaceRecognizer/FaceRecognizer.csproj | 20 ++++++ FaceRecognizer/MainForm.Designer.cs | 17 +++++ FaceRecognizer/MainForm.cs | 97 +++++++++++++++++++--------- FaceRecognizer/MainForm.resx | 6 ++ 5 files changed, 117 insertions(+), 29 deletions(-) diff --git a/FaceRecognizer.sln b/FaceRecognizer.sln index 0c0e380..14573d8 100644 --- a/FaceRecognizer.sln +++ b/FaceRecognizer.sln @@ -8,13 +8,19 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {AFE13E01-F592-4F23-B76C-1D2D33D475FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {AFE13E01-F592-4F23-B76C-1D2D33D475FD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AFE13E01-F592-4F23-B76C-1D2D33D475FD}.Debug|x64.ActiveCfg = Debug|x64 + {AFE13E01-F592-4F23-B76C-1D2D33D475FD}.Debug|x64.Build.0 = Debug|x64 {AFE13E01-F592-4F23-B76C-1D2D33D475FD}.Release|Any CPU.ActiveCfg = Release|Any CPU {AFE13E01-F592-4F23-B76C-1D2D33D475FD}.Release|Any CPU.Build.0 = Release|Any CPU + {AFE13E01-F592-4F23-B76C-1D2D33D475FD}.Release|x64.ActiveCfg = Release|x64 + {AFE13E01-F592-4F23-B76C-1D2D33D475FD}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FaceRecognizer/FaceRecognizer.csproj b/FaceRecognizer/FaceRecognizer.csproj index 3f339c8..c018859 100644 --- a/FaceRecognizer/FaceRecognizer.csproj +++ b/FaceRecognizer/FaceRecognizer.csproj @@ -34,6 +34,26 @@ prompt 4 + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + prompt + MinimumRecommendedRules.ruleset + true + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + prompt + MinimumRecommendedRules.ruleset + true + ..\packages\EMGU.CV.3.4.1.2976\lib\net35\Emgu.CV.UI.dll diff --git a/FaceRecognizer/MainForm.Designer.cs b/FaceRecognizer/MainForm.Designer.cs index fdf74ba..aa756a6 100644 --- a/FaceRecognizer/MainForm.Designer.cs +++ b/FaceRecognizer/MainForm.Designer.cs @@ -34,6 +34,8 @@ this.TrainBtn = new System.Windows.Forms.Button(); this.nameTxtBx = new System.Windows.Forms.TextBox(); this.RecconizeBtn = new System.Windows.Forms.Button(); + this.trainBackWorker = new System.ComponentModel.BackgroundWorker(); + this.recognizeBackWorker = new System.ComponentModel.BackgroundWorker(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit(); this.SuspendLayout(); @@ -54,6 +56,7 @@ // // pictureBox1 // + this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.pictureBox1.Location = new System.Drawing.Point(464, 12); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(324, 300); @@ -63,6 +66,7 @@ // // pictureBox2 // + this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.pictureBox2.Location = new System.Drawing.Point(12, 12); this.pictureBox2.Name = "pictureBox2"; this.pictureBox2.Size = new System.Drawing.Size(324, 300); @@ -96,6 +100,17 @@ this.RecconizeBtn.UseVisualStyleBackColor = true; this.RecconizeBtn.Click += new System.EventHandler(this.RecconizeBtn_Click); // + // trainBackWorker + // + this.trainBackWorker.WorkerReportsProgress = true; + this.trainBackWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.trainBackWorker_DoWork); + this.trainBackWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.trainBackWorker_RunWorkerCompleted); + // + // recognizeBackWorker + // + this.recognizeBackWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.recognizeBackWorker_DoWork); + this.recognizeBackWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.recognizeBackWorker_RunWorkerCompleted); + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -124,6 +139,8 @@ private System.Windows.Forms.Button TrainBtn; private System.Windows.Forms.TextBox nameTxtBx; private System.Windows.Forms.Button RecconizeBtn; + private System.ComponentModel.BackgroundWorker trainBackWorker; + private System.ComponentModel.BackgroundWorker recognizeBackWorker; } } diff --git a/FaceRecognizer/MainForm.cs b/FaceRecognizer/MainForm.cs index 01dfb1e..c22440e 100644 --- a/FaceRecognizer/MainForm.cs +++ b/FaceRecognizer/MainForm.cs @@ -26,8 +26,11 @@ namespace FaceRecognizer List> imageList = new List>(); List imageLabels = new List(); List imageIndicies = new List(); + List predictions = new List(); Rectangle[] detectedFaces; string trainImagesFolder = Application.StartupPath + "\\Train"; + string reconizeImagesFolder = Application.StartupPath + "\\photo"; + public MainForm() { @@ -39,29 +42,22 @@ namespace FaceRecognizer { faceCascade = new CascadeClassifier("haarcascade_frontalface_alt_tree.xml"); reconizer = new LBPHFaceRecognizer(); - + int j = 0; foreach (var file in Directory.GetFiles(trainImagesFolder)) - { + { if (file.EndsWith("jpg")) { - console.AppendText("found: " + Path.GetFileNameWithoutExtension(file)); temp = new Image(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; } + var detected_faces = faceCascade.DetectMultiScale(temp, 1.1, 2, new Size(24, 24), Size.Empty); + if (detected_faces.Length == 0) { continue; } temp.ROI = detected_faces[0]; temp = temp.Copy(); - temp = temp.Resize(100, 100, Inter.Cubic); + temp = temp.Resize(24, 24, Inter.Cubic); imageList.Add(temp); imageLabels.Add(Path.GetFileNameWithoutExtension(file)); } + j++; } for (int i = 0; i < imageList.Count; i++) @@ -73,52 +69,95 @@ namespace FaceRecognizer { reconizer.Train(imageList.ToArray(), imageIndicies.ToArray()); reconizer.Write(personName); - console.AppendText("Training done!\nFile Name: " + personName); + //console.AppendText("Training done!\nFile Name: " + personName); + reconizer.Dispose(); + imageList.Clear(); + faceCascade.Dispose(); + temp.Dispose(); } - catch (Exception e) { console.AppendText("Error: " + e.Message); } + catch (Exception e) { } //console.AppendText("Error: " + e.Message); } } void recognize(string personName) { - console.AppendText("Recognizing " + personName + "from local photos"); + int i = 0; try { - reconizer = new LBPHFaceRecognizer(1, 8, 8, 9, 65); + //reconizer = new LBPHFaceRecognizer(1, 8, 8, 9, 65); reconizer = new LBPHFaceRecognizer(); + faceCascade = new CascadeClassifier("haarcascade_frontalface_alt_tree.xml"); + predictions.Clear(); + //reconizer = new LBPHFaceRecognizer(); reconizer.Read(personName); - foreach (var file in Directory.GetFiles(trainImagesFolder)) + foreach (var file in Directory.GetFiles(reconizeImagesFolder)) { if (file.EndsWith("jpg")) { - Image temp2 = new Image(file); - pictureBox1.Image = temp2.Resize(pictureBox1.Width, pictureBox1.Height, Inter.Cubic).ToBitmap(); - var result = reconizer.Predict(temp2); - console.AppendText(">>result:" + result.Label.ToString()); + temp = new Image(file); + temp._EqualizeHist(); + + var faces = faceCascade.DetectMultiScale(temp, 1.1, 2, new Size(24, 24), Size.Empty); + if (faces.Length == 0) { continue; } + temp.ROI = faces[0]; + temp = temp.Copy(); + temp.Resize(24, 24, Inter.Cubic); + temp.ToBitmap().Save(reconizeImagesFolder + "\\" + i + ".jpg"); + var result = reconizer.Predict(temp); + predictions.Add(result.Label); } + i++; } } - catch (Exception e) - { - console.AppendText("Error: " + e.Message); - } + catch (Exception e){ throw e; } } private void TrainBtn_Click(object sender, EventArgs e) { - train(nameTxtBx.Text); + trainBackWorker.RunWorkerAsync(); + console.AppendText("Training started for " + nameTxtBx.Text); } private void RecconizeBtn_Click(object sender, EventArgs e) { - recognize(nameTxtBx.Text); + recognizeBackWorker.RunWorkerAsync(); + console.AppendText("Started recognition"); } private void console_TextChanged(object sender, EventArgs e) { console.AppendText("\n"); + console.SelectionStart = console.Text.Length; + console.ScrollToCaret(); } - + private void trainBackWorker_DoWork(object sender, DoWorkEventArgs e) + { + train(nameTxtBx.Text); + } + + private void trainBackWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) + { + console.AppendText("Finished training: " + nameTxtBx.Text); + } + + private void recognizeBackWorker_DoWork(object sender, DoWorkEventArgs e) + { + recognize(nameTxtBx.Text); + } + + private void recognizeBackWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) + { + console.AppendText("Recognition finished\nResults:"); + printResults(); + } + + void printResults() + { + foreach(var number in predictions) + { + console.AppendText(number.ToString()); + } + } } } diff --git a/FaceRecognizer/MainForm.resx b/FaceRecognizer/MainForm.resx index 1af7de1..b3cb861 100644 --- a/FaceRecognizer/MainForm.resx +++ b/FaceRecognizer/MainForm.resx @@ -117,4 +117,10 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 158, 17 + \ No newline at end of file From 5f75edfcb2422b58f68e7446e8980b006ff4ca46 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Fri, 21 Sep 2018 15:25:13 +0300 Subject: [PATCH 3/3] disposeing --- FaceRecognizer/MainForm.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/FaceRecognizer/MainForm.cs b/FaceRecognizer/MainForm.cs index c22440e..e95262b 100644 --- a/FaceRecognizer/MainForm.cs +++ b/FaceRecognizer/MainForm.cs @@ -108,6 +108,10 @@ namespace FaceRecognizer } i++; } + reconizer.Dispose(); + imageList.Clear(); + faceCascade.Dispose(); + temp.Dispose(); } catch (Exception e){ throw e; } }