AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

Motion Detection

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

Motion Detection

Postby abazuu » Sun Jul 22, 2018 10:05 pm

Hi,

I have problems using a motion detection or setting an even on motion detected.. i dont know why but i can't get information when motion is detected. I have motion detector set to motionareahighlightning and it's working. but can't solve the float..

Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Vision.Motion;
using AForge.Video;
using AForge.Video.DirectShow;
using Accord.Video.FFMPEG;
using System.Globalization;
using AForge.Controls;
using System.Threading;

namespace AnotherAForge
{
    public partial class Form1 : Form
    {
        MotionDetector Detector = new MotionDetector(new TwoFramesDifferenceDetector(), new MotionAreaHighlighting());
        int frames;
        private MJPEGStream stream = new MJPEGStream("http://192.65.213.243/mjpg/video.mjpg?camera=1&timestamp=1531221316316");
        int uporabimotiondetection = 0;
        delegate void SetTextCallback(string text);
        float f;
       


        public Form1()
        {
            InitializeComponent();
           
        }

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

                f = Detector.ProcessFrame(image);
                label8.Text = String.Format("{0:00.0000}",f);
                label9.Text = f.ToString();




            if (Detector.ProcessFrame(image) > 0.02)
            {
                label10.Text = "0";
            }
            else
            {
                label10.Text = "1";
            }


       


        }
}



what am i doing wrong?
abazuu
 
Posts: 4
Joined: Sun Jul 22, 2018 10:01 pm

Re: Motion Detection

Postby andrew.kirillov » Mon Jul 23, 2018 7:04 am

Hello,

abazuu wrote:I have motion detector set to motionareahighlightning and it's working.

Nice to hear it does work for you. So it is all fine then.

abazuu wrote:but can't solve the float..

Not sure what you mean by that.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3453
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: Motion Detection

Postby abazuu » Mon Jul 23, 2018 10:31 am

Thanks for quick reply.

what i am having trouble with is:

I manage to get float out of motion detector. I've managed also to get this float to label (which shows how much motion is).

But this:

Code: Select all
  if (Detector.ProcessFrame(image) > 0.02)
            {
                label10.Text = "0";
            }
            else
            {
                label10.Text = "1";
            }


doesnt work... label10.text stays at 1, no matter what.. if there's motion or not...

no matter what (i've tried literally 30 ways) this doesnt work. I can't set alarm, or beep a sound when moving is detected or anything :(
abazuu
 
Posts: 4
Joined: Sun Jul 22, 2018 10:01 pm

Re: Motion Detection

Postby andrew.kirillov » Mon Jul 23, 2018 11:15 am

Two things:
1) Check the value of motion level (the one returned by ProcessFrame()). Output it into debug console and see what are the values.

2) Make sure you update label correctly. The NewFrame event is fired from a different thread. So updating UI in NewFrame event handler will cause exception. See here on how to do thread safe UI update.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3453
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: Motion Detection

Postby abazuu » Mon Jul 23, 2018 11:34 am

Since I am just a beginner, is it possible to help me with invoke method for label10?

thank you very much for your help!

Or, would this work?

Code: Select all
float motion = Detector.ProcessFrame(image); // running on worker thread
this.Invoke((MethodInvoker)delegate {
    label9.Text = motion.ToString(); // runs on UI thread

    if( motion > 0.02  ){
        label10.Text = "0";
    else {
       label10.Text = "1";
}}
});
abazuu
 
Posts: 4
Joined: Sun Jul 22, 2018 10:01 pm

Re: Motion Detection

Postby abazuu » Mon Jul 23, 2018 5:55 pm

I have now put this invoke and multiplied float * 100 and now label10 is reponsible, but it doesnt work like it should be working...

Code: Select all
        private void VideoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
        {

            float motion = Detector.ProcessFrame(image); // running on worker thread

            float motion2 = motion * 100;
            this.Invoke((MethodInvoker)delegate
            {
                label9.Text = motion2.ToString(); // runs on UI thread

                if ( motion2 > 0.02)
                {
                    label10.Text = "0";
                }
                else
                {
                    label10.Text = "1";
                }
            });
               
           

        }
abazuu
 
Posts: 4
Joined: Sun Jul 22, 2018 10:01 pm

Re: Motion Detection

Postby andrew.kirillov » Tue Jul 24, 2018 6:51 am

abazuu wrote:but it doesnt work like it should be working...

No one really knows how it should be working except you ...
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3453
Joined: Fri Jan 23, 2009 9:12 am
Location: UK




Return to AForge.NET Framework

cron