Skip to content

Commit

Permalink
Bug in the motion detector cause false positives, fixes #250
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Aug 8, 2014
1 parent 0b826cf commit 27c56f3
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public class WebcamMotionDetector {
public static final int DEFAULT_PIXEL_THREASHOLD = 25;

/**
* Default check interval (in milliseconds, set to 1 second).
* Default check interval, in milliseconds, set to 500 ms.
*/
public static final int DEFAULT_INTERVAL = 1000;
public static final int DEFAULT_INTERVAL = 500;

/**
* Default percentage image area fraction threshold (set to 0.2%).
Expand Down Expand Up @@ -120,7 +120,7 @@ public void run() {
delay = inertia != -1 ? inertia : 2 * interval;

if (lastMotionTimestamp + delay < System.currentTimeMillis()) {
motion.set(false);
motion = false;
}
}
}
Expand All @@ -144,7 +144,7 @@ public void run() {
/**
* Is motion?
*/
private final AtomicBoolean motion = new AtomicBoolean(false);
private volatile boolean motion = false;

/**
* Previously captured image.
Expand Down Expand Up @@ -272,8 +272,18 @@ public void stop() {

protected void detect() {

if (!webcam.isOpen()) {
motion = false;
return;
}

BufferedImage current = webcam.getImage();

if (current == null) {
motion = false;
return;
}

current = blur.filter(current, null);
current = gray.filter(current, null);

Expand Down Expand Up @@ -308,9 +318,8 @@ protected void detect() {

cog = new Point(cogX / p, cogY / p);

if (motion.compareAndSet(false, true)) {
lastMotionTimestamp = System.currentTimeMillis();
}
motion = true;
lastMotionTimestamp = System.currentTimeMillis();

notifyMotionListeners();

Expand Down Expand Up @@ -457,7 +466,7 @@ public boolean isMotion() {
if (!running.get()) {
LOG.warn("Motion cannot be detected when detector is not running!");
}
return motion.get();
return motion;
}

/**
Expand Down

0 comments on commit 27c56f3

Please sign in to comment.