Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in the motion detector cause false positives #250

Closed
sarxos opened this issue Aug 8, 2014 · 0 comments
Closed

Bug in the motion detector cause false positives #250

sarxos opened this issue Aug 8, 2014 · 0 comments
Assignees
Labels

Comments

@sarxos
Copy link
Owner

sarxos commented Aug 8, 2014

Motion detector should trigger the motion event and stay in "motion" state for the time when motion is in progress and the intertia time defined as one of the parameters. Due to bug in the implementation it caused false positive events about new motion started when previous motion was already in progress.

import java.io.IOException;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamMotionDetector;
import com.github.sarxos.webcam.WebcamResolution;


/**
 * The goal of this example is to demonstrate the idea behind detecting that
 * motion has stopped.
 * 
 * @author Bartosz Firyn (sarxos)
 */
public class DetectMotionEventsExample {

    Webcam webcam;
    WebcamMotionDetector detector;

    public DetectMotionEventsExample() {

        webcam = Webcam.getDefault();
        webcam.setViewSize(WebcamResolution.VGA.getSize());

        detector = new WebcamMotionDetector(webcam);
        detector.setInterval(200); // one check per 200 ms
        detector.setInertia(2000); // keep "motion" state for 2 seconds
        detector.start();

        Thread t = new Thread("motion-printer") {

            @Override
            public void run() {

                boolean motion = false;
                long now = 0;

                while (true) {
                    now = System.currentTimeMillis();
                    if (detector.isMotion()) {
                        if (!motion) {
                            motion = true;
                            System.out.println(now + " MOTION STARTED");
                        }
                    } else {
                        if (motion) {
                            motion = false;
                            System.out.println(now + " MOTION STOPPED");
                        }
                    }
                    try {
                        Thread.sleep(50); // must be smaller than interval
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        t.setDaemon(true);
        t.start();
    }

    public static void main(String[] args) throws IOException {
        new DetectMotionEventsExample();
        System.in.read(); // keep program open
    }
}
@sarxos sarxos added the bug label Aug 8, 2014
@sarxos sarxos self-assigned this Aug 8, 2014
@sarxos sarxos changed the title Bug in the motion detector cause false positives events Bug in the motion detector cause false positives Aug 8, 2014
@sarxos sarxos closed this as completed in 27c56f3 Aug 8, 2014
sarxos added a commit that referenced this issue Aug 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant