1
votes

I am using code for motion detection webcam using Aforge. When running the program, it can run perfectly, but when edit libel1 "motion Detection" and libel2 "no motion Detection" it cannot running. Why?

The code I'm using for motion detection:

    public Form1()
    {
        InitializeComponent();
    }

    FilterInfoCollection fic;
    VideoCaptureDevice Device;
    MotionDetector motionDetector;
    float f;

    private void Form1_Load(object sender, EventArgs e)
    {
        motionDetector = new MotionDetector(new TwoFramesDifferenceDetector(), new MotionAreaHighlighting());
        fic = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        foreach (FilterInfo item in fic)
        {
            comboBoxDevice.Items.Add(item.Name);
        }
        comboBoxDevice.SelectedIndex = 0;
    }

    private void BtnStart_Click(object sender, EventArgs e)
    {
        Device = new VideoCaptureDevice(fic[comboBoxDevice.SelectedIndex].MonikerString);

        videoSourcePlayer1.VideoSource = Device;
        videoSourcePlayer1.Start();
    }

    private void BtnStop_Click(object sender, EventArgs e)
    {
        videoSourcePlayer1.Stop();
    }

    private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
    {

       if (motionDetector == null) return;

         f = motionDetector.ProcessFrame(image);
       if (f >0)
       {
           label1.ForeColor = Color.Red;
             label1.Text = "Motion Detected";

       }
       else
       {
           label1.ForeColor = Color.Green;
           label1.Text = "No Motion Detected";
       }
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        label3.Text = "Value: " + f.ToString();
    }
}
}
1

1 Answers

0
votes

I think you want to change a label in your UI when you detect a motion. If you want to change a label in UI, you can't run the change function in the same thread. So, you can change it by defining another thread. Just make sure you prevent race conditions.

// Changing UI Labels (Make thread)
   Label1.Invoke((MethodInvoker)delegate {
          // What do you want to change?
          Label1.Text = "Detecting Motions";
    });