Click here to Skip to main content
15,946,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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();
        //for face detection
        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
Posted
Comments
Richard MacCutchan 19-Oct-19 4:19am    
You need to provide more information. How is it failing when you rerun the application?
aiden12 19-Oct-19 7:57am    
basically when the program detects my face i write my name in a textBox and i save it, when i get out of the frame and come back in and detects my face again, it shows the name i have previously entered, but when close and re run the program that doesn't seem to be the case, it doesn't show me the name i have entered my face under before, it doesn't recognize my face, i have been searching for days to find a solution or even a substitute on how to create a face recognizer, i want to use it in my login form so the user can access some part of the program if the program recognizes the face
Richard MacCutchan 19-Oct-19 8:16am    
Where do you load the saved image, and how do you compare it to the current capture?
aiden12 19-Oct-19 8:23am    
that's what i'm dealing with, i want to compare the pictures that i stored in a folder to the current live image and if they match i want it to tell me the name that i saved the picture under, i have no idea on how to compare a live image to already stored pictures
Richard MacCutchan 19-Oct-19 9:06am    
Sorry, neither do I. But this is a fairly common issue these days so I would use Google to find samples and tutorials on the subject.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900