CS0103 dlibdotnet and emu.cv facerect not in context

19 views Asked by At

I've been trying to fix the error in code cs0103, where the 'faceRect' does not exist in the current context, however when checking my code I don't know where I'm wrong, I'm new to both dlibdotnet and emgu.cv

Here are the 3 functions working together:

FrameGrabber:

void FrameGrabber(object sender, EventArgs e)
{
    try
    {
        face_detected_lbl.Text = "0";
        NamePersons.Add("");

        // Get the current frame from the capture device
        Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> currentFrame = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

        // Convert it to Grayscale
        Emgu.CV.Image<Emgu.CV.Structure.Gray, byte> gray = currentFrame.Convert<Emgu.CV.Structure.Gray, byte>();

        // Face Detector
        MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(face, 1.2, 15, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(30, 30));

        // Action for each element detected
        foreach (MCvAvgComp f in facesDetected[0])
        {
            t = t + 1;
            Emgu.CV.Image<Emgu.CV.Structure.Gray, byte> result = gray.Copy(f.rect).Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
            // Draw the face detected in the 0th (gray) channel with light green color
            currentFrame.Draw(f.rect, new Bgr(Color.LightGreen), 2);

            // Check if the face is live
            DlibDotNet.Rectangle dlibRect = new DlibDotNet.Rectangle(f.rect.Left, f.rect.Top, f.rect.Right, f.rect.Bottom);
            bool isLive = IsLiveFunction(currentFrame, dlibRect);

            if (trainingImages.ToArray().Length != 0)
            {
                // TermCriteria for face recognition with numbers of trained images like maxIteration
                MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);

                // Eigen face recognizer
                EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
                    trainingImages.ToArray(),
                    labels.ToArray(),
                    3000,
                    ref termCrit);

                string name = recognizer.Recognize(result);

                finalname = name;
                // Draw the label for each face detected and recognized
                if (string.IsNullOrEmpty(name))
                {
                    currentFrame.Draw(string.IsNullOrEmpty(name) ? "Unknown" : name, ref font, new System.Drawing.Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(Color.Red));
                }
                else
                {
                    currentFrame.Draw(name, ref font, new System.Drawing.Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(Color.Lime));
                }
            }

IsLiveFunction:

private bool IsLiveFunction(Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> currentFrame, DlibDotNet.Rectangle faceRect)
{
     bool isLive = false;

     try
     {
         System.Drawing.Rectangle emguRect = new System.Drawing.Rectangle((int)faceRect.Left, (int)faceRect.Top, (int)faceRect.Width, (int)faceRect.Height);

         // Convert the current frame to grayscale
         Emgu.CV.Image<Emgu.CV.Structure.Gray, byte> grayFrame = currentFrame.Convert<Emgu.CV.Structure.Gray, byte>();

         // Extract region of interest (ROI) from the current frame using the face rectangle
         Emgu.CV.Image<Emgu.CV.Structure.Gray, byte> faceImage = grayFrame.Copy(emguRect);

         double ear = CalculateEyeAspectRatio(faceImage);
         double blinkThreshold = 0.2;
         bool blinkingDetected = ear < blinkThreshold;
         DlibDotNet.Rectangle dlibRect = new DlibDotNet.Rectangle(emguRect.Left, emguRect.Top, emguRect.Right, emguRect.Bottom);
         bool headMovementDetected = DetectHeadMovement(grayFrame, dlibRect);
    }
    catch()
    { }
}

LogInLogOut:

private void login_timer_Tick(object sender, EventArgs e)
{
    try
    {
        pwede_na_magout = false;

        if (!string.IsNullOrEmpty(identified_name_lbl.Text) )
        {
            //--- TIMEIN 1
            bool checkuser = false;
            int face_detected = Convert.ToInt32(face_detected_lbl.Text);
            currentFrame = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
            bool isLive = IsLiveFunction(currentFrame, faceRect);
            string query = "SELECT Username, Date, Time_In FROM logbook_tb";

            MySqlCommand cmd = new MySqlCommand(query, connection);

            if (connection.State != ConnectionState.Open)
            {
                // blah blah
            }

            if (checkuser == false && !string.IsNullOrEmpty(lname.Text) && (face_detected == 1 && isLive) && (DateTime.Parse(time_login.Text, CultureInfo.InvariantCulture) < DateTime.Parse("11:59 AM", CultureInfo.InvariantCulture)))
            {
                MessageBox.Show("Blink once and move your head to log in.", "Instructions", MessageBoxButtons.OK, MessageBoxIcon.Information);

                LOGIN();
                READ_RECORD();
            }
            else
            {
                // If blinking or head movement is not detected
                DialogResult result = MessageBox.Show("Failed to detect real face. Would you like to try again?", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); ;

                if (result == DialogResult.Retry)
                {
                    // Retry
                    login_timer_Tick(sender, e);
                }
                else
                {
                    // Exit
                    Application.Exit();
                }
            }
        }
    }
}

I tried

DlibDotNet.Rectangle faceRect = GetFaceRectangleFromCurrentFrame();

however, this will lead me into creating a new function, which is not even guaranteed it will fix the problem.

Here's how it should work on my mind:

Facedect > Blink and headmovement > if both are true, then login, otherwise, show a prompt message.

I still don't know if there would be more issues regarding the functions, it would be a great help if you recognized some and tell me.

Thanks!

0

There are 0 answers