FaceRcognizer/FaceRecognizer/MainForm.cs

168 lines
5.7 KiB
C#
Raw Permalink Normal View History

2018-09-17 15:20:28 +02:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
2018-09-18 09:25:01 +02:00
using System.IO;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.Util;
using Emgu.CV.Face;
2018-09-17 15:20:28 +02:00
namespace FaceRecognizer
{
2018-09-18 07:43:19 +02:00
public partial class MainForm : Form
2018-09-17 15:20:28 +02:00
{
2018-09-18 09:25:01 +02:00
LBPHFaceRecognizer reconizer;
CascadeClassifier faceCascade;
Image<Gray, byte> temp;
List<Image<Gray, byte>> imageList = new List<Image<Gray, byte>>();
List<string> imageLabels = new List<string>();
List<int> imageIndicies = new List<int>();
2018-09-21 14:23:33 +02:00
List<int> predictions = new List<int>();
2018-09-18 09:25:01 +02:00
Rectangle[] detectedFaces;
2018-09-20 11:48:50 +02:00
string trainImagesFolder = Application.StartupPath + "\\Train";
2018-09-21 14:23:33 +02:00
string reconizeImagesFolder = Application.StartupPath + "\\photo";
2018-09-18 09:25:01 +02:00
2018-09-18 07:43:19 +02:00
public MainForm()
2018-09-17 15:20:28 +02:00
{
2018-09-18 09:25:01 +02:00
InitializeComponent();
2018-09-20 11:48:50 +02:00
console.AppendText("Main OK!");
}
void train(string personName)
{
faceCascade = new CascadeClassifier("haarcascade_frontalface_alt_tree.xml");
reconizer = new LBPHFaceRecognizer();
2018-09-21 14:23:33 +02:00
int j = 0;
2018-09-20 11:48:50 +02:00
foreach (var file in Directory.GetFiles(trainImagesFolder))
2018-09-21 14:23:33 +02:00
{
2018-09-20 11:48:50 +02:00
if (file.EndsWith("jpg"))
{
temp = new Image<Gray, byte>(file);
temp._EqualizeHist();
2018-09-21 14:23:33 +02:00
var detected_faces = faceCascade.DetectMultiScale(temp, 1.1, 2, new Size(24, 24), Size.Empty);
if (detected_faces.Length == 0) { continue; }
2018-09-20 11:48:50 +02:00
temp.ROI = detected_faces[0];
temp = temp.Copy();
2018-09-21 14:23:33 +02:00
temp = temp.Resize(24, 24, Inter.Cubic);
2018-09-20 11:48:50 +02:00
imageList.Add(temp);
imageLabels.Add(Path.GetFileNameWithoutExtension(file));
}
2018-09-21 14:23:33 +02:00
j++;
2018-09-20 11:48:50 +02:00
}
for (int i = 0; i < imageList.Count; i++)
{
imageIndicies.Add(i);
}
2018-09-18 09:25:01 +02:00
try
{
2018-09-20 11:48:50 +02:00
reconizer.Train(imageList.ToArray(), imageIndicies.ToArray());
reconizer.Write(personName);
2018-09-21 14:23:33 +02:00
//console.AppendText("Training done!\nFile Name: " + personName);
reconizer.Dispose();
imageList.Clear();
faceCascade.Dispose();
temp.Dispose();
2018-09-20 11:48:50 +02:00
}
2018-09-21 14:23:33 +02:00
catch (Exception e) { } //console.AppendText("Error: " + e.Message); }
2018-09-20 11:48:50 +02:00
}
2018-09-18 09:25:01 +02:00
2018-09-20 11:48:50 +02:00
void recognize(string personName)
{
2018-09-21 14:23:33 +02:00
int i = 0;
2018-09-20 11:48:50 +02:00
try
{
2018-09-21 14:23:33 +02:00
//reconizer = new LBPHFaceRecognizer(1, 8, 8, 9, 65);
2018-09-20 11:48:50 +02:00
reconizer = new LBPHFaceRecognizer();
2018-09-21 14:23:33 +02:00
faceCascade = new CascadeClassifier("haarcascade_frontalface_alt_tree.xml");
predictions.Clear();
//reconizer = new LBPHFaceRecognizer();
2018-09-20 11:48:50 +02:00
reconizer.Read(personName);
2018-09-18 09:25:01 +02:00
2018-09-21 14:23:33 +02:00
foreach (var file in Directory.GetFiles(reconizeImagesFolder))
2018-09-18 09:25:01 +02:00
{
if (file.EndsWith("jpg"))
{
2018-09-21 14:23:33 +02:00
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);
2018-09-18 09:25:01 +02:00
}
2018-09-21 14:23:33 +02:00
i++;
2018-09-18 09:25:01 +02:00
}
2018-09-21 14:25:13 +02:00
reconizer.Dispose();
imageList.Clear();
faceCascade.Dispose();
temp.Dispose();
2018-09-18 09:25:01 +02:00
}
2018-09-21 14:23:33 +02:00
catch (Exception e){ throw e; }
2018-09-20 11:48:50 +02:00
}
2018-09-18 09:25:01 +02:00
2018-09-20 11:48:50 +02:00
private void TrainBtn_Click(object sender, EventArgs e)
{
2018-09-21 14:23:33 +02:00
trainBackWorker.RunWorkerAsync();
console.AppendText("Training started for " + nameTxtBx.Text);
2018-09-20 11:48:50 +02:00
}
2018-09-18 09:25:01 +02:00
2018-09-20 11:48:50 +02:00
private void RecconizeBtn_Click(object sender, EventArgs e)
{
2018-09-21 14:23:33 +02:00
recognizeBackWorker.RunWorkerAsync();
console.AppendText("Started recognition");
2018-09-17 15:27:56 +02:00
}
private void console_TextChanged(object sender, EventArgs e)
{
console.AppendText("\n");
2018-09-21 14:23:33 +02:00
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);
2018-09-17 15:20:28 +02:00
}
2018-09-20 11:48:50 +02:00
2018-09-21 14:23:33 +02:00
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());
}
}
2018-09-17 15:20:28 +02:00
}
}