CS0103 dlibdotnet 和 emu.cvfacerect 不在上下文中

问题描述 投票:0回答:1

我一直在尝试修复代码 cs0103 中的错误,其中“faceRect”在当前上下文中不存在,但是在检查我的代码时我不知道我错在哪里,我对 dlibdotnet 都是新手和 emgu.cv

以下是协同工作的 3 个功能:

图像采集卡:

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

是LiveFunction:

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()
    { }
}

登录退出:

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();
                }
            }
        }
    }
}

我试过了

DlibDotNet.Rectangle faceRect = GetFaceRectangleFromCurrentFrame();

但是,这将导致我创建一个新函数,甚至不能保证它会解决问题。

这就是我的想法:

Facedect > Blink
headmovement >
如果都为 true,则登录,否则显示提示消息。

我仍然不知道是否还会有更多关于功能的问题,如果你认识一些并告诉我,那将是一个很大的帮助。

谢谢!

c# .net emgucv dlib
1个回答
0
投票

我将所有代码从 emgu.cv 2.0 翻译到最新的 4.8,现在一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.