BackWorkers, photo folder

This commit is contained in:
HeshamTB 2018-09-21 15:23:33 +03:00
parent 57ddf2a2d9
commit 185d221eab
5 changed files with 117 additions and 29 deletions

View File

@ -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

View File

@ -34,6 +34,26 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Emgu.CV.UI, Version=3.4.1.2976, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\EMGU.CV.3.4.1.2976\lib\net35\Emgu.CV.UI.dll</HintPath>

View File

@ -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;
}
}

View File

@ -26,8 +26,11 @@ namespace FaceRecognizer
List<Image<Gray, byte>> imageList = new List<Image<Gray, byte>>();
List<string> imageLabels = new List<string>();
List<int> imageIndicies = new List<int>();
List<int> predictions = new List<int>();
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<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; }
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<Gray, byte> temp2 = new Image<Gray, byte>(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<Gray, byte>(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());
}
}
}
}

View File

@ -117,4 +117,10 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="trainBackWorker.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="recognizeBackWorker.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>158, 17</value>
</metadata>
</root>