i have implemented a face detection and recognition in c# windows form using Emgu and OpenCV, it works fine and when the saveButton is clicked the image gets stored in a folder, but when i close the program and re-run it, it doesn't recognize the face anymore, is there any way i can make the program recognize the face even after shutting the program down?
here is my code:
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System.IO;
namespace aiden
{
public partial class face_recognition : Form
{
MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 0.6, 0.6);
HaarCascade faceDetected;
Image<Bgr, byte> Frame;
Capture camera;
Image<Gray, byte> result;
Image<Gray, byte> trainedFace = null;
Image<Gray, byte> grayface = null;
List<Image<Gray, byte>> trainingImages = new List<Image<Gray, byte>>();
List<string> labels = new List<string>();
List<string> users = new List<string>();
int count, numLabels, t;
string name, names = null;
public face_recognition()
{
InitializeComponent();
faceDetected = new
HaarCascade("haarcascade_frontalface_default.xml");
try
{
string labelsinf = File.ReadAllText(Application.StartupPath +
"/Faces/Faces.txt");
string[] Labels = labelsinf.Split(',');
numLabels = Convert.ToInt16(Labels[0]);
count = numLabels;
string facesLoad;
for (int i = 1; i < numLabels + 1; i++)
{
facesLoad = "face" + i ;
trainingImages.Add(new Image<Gray, byte>( (Application.StartupPath + "/Faces/Faces.txt"));
labels.Add(labels[i]);
}
}
catch (Exception ex)
{
MessageBox.Show("not detected");
}
}
private void start_Click(object sender, EventArgs e)
{
camera = new Capture();
camera.QueryFrame();
Application.Idle += new EventHandler(frameProcedure);
}
private void frameProcedure(object sender, EventArgs e)
{
users.Add("");
Frame = camera.QueryFrame().Resize(320, 240, INTER.CV_INTER_CUBIC) ;
grayface = Frame.Convert<Gray, Byte>();
MCvAvgComp[][] facesDetectedNow=grayface.DetectHaarCascade(faceDetected,1.2,10,HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,new Size(20,20));
foreach(MCvAvgComp f in facesDetectedNow[0])
{
result=Frame.Copy(f.rect).Convert<Gray,Byte>().Resize(100,100,INTER.CV_INTER_CUBIC);
Frame.Draw(f.rect, new Bgr(Color.Green), 3);
if (trainingImages.ToArray().Length != 0)
{
MCvTermCriteria termCriteria = new MCvTermCriteria(count, 0.001);
EigenObjectRecognizer recognizer = new EigenObjectRecognizer(trainingImages.ToArray(), labels.ToArray(),1500,ref termCriteria);
name = recognizer.Recognize(result);
Frame.Draw(name, ref font, new Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(Color.Red));
}
users.Add("");
}
cameraBox.Image = Frame;
names = "";
users.Clear();
}
private void saveButton_Click(object sender, EventArgs e)
{
count = count + 1;
grayface = camera.QueryGrayFrame().Resize(320, 240, INTER.CV_INTER_CUBIC);
MCvAvgComp[][] detectedFaces = grayface.DetectHaarCascade(faceDetected, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
foreach (MCvAvgComp f in detectedFaces[0])
{
trainedFace = Frame.Copy(f.rect).Convert<Gray, byte>();
break;
}
trainedFace = result.Resize(100, 100, INTER.CV_INTER_CUBIC);
trainingImages.Add(trainedFace);
labels.Add(textBox1.Text);
File.WriteAllText(Application.StartupPath+"/Faces/Faces.txt",trainingImages.ToArray().Length.ToString()+",");
for (int i = 1; i < trainingImages.ToArray().Length + 1; i++)
{
trainingImages.ToArray()[i - 1].Save(Application.StartupPath+"/Faces/face"+i+".bmp");
File.AppendAllText(Application.StartupPath + "/Faces/Faces.txt", labels.ToArray()[i-1] + ",");
}
MessageBox.Show(textBox1.Text + "added");
}
I want to use face recognition in my login form for my research project. any help would be appreciated.
Thanks
What I have tried:
i havent had any idea on how to make the program to remember the persons use so that i can use it as a login option