-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new motion detection algorithm (DNE), refs #524
The "Do No Engage" algorithm is simply a rectangle specified by the user in which the algorithm will ignore motion. The feature is implemented by allowing the user to pass a Rectangle into the algorithm, and then the algorithm only processes pixels that are not contained in the Rectangle. This commit is a squash of pull request #524 by Kevin Kanzelmeyer (@kkanzelmeyer). It was produced by cherry picking all changes into the master, squasing, removing all changes that relates to pom.xml files (these were unnecessary) and the ones related to GStramer 1.x driver and then finally reworking to address comments from review.
- Loading branch information
1 parent
2fd33d1
commit 9ee9d94
Showing
8 changed files
with
453 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ README.md~ | |
.idea/ | ||
*.ipr | ||
*.iws | ||
.DS_Store |
110 changes: 110 additions & 0 deletions
110
webcam-capture/src/example/java/DetectMotionDoNotEngageZoneExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import java.awt.BasicStroke; | ||
import java.awt.Color; | ||
import java.awt.Dimension; | ||
import java.awt.Graphics2D; | ||
import java.awt.Paint; | ||
import java.awt.Point; | ||
import java.awt.Rectangle; | ||
import java.awt.TexturePaint; | ||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
|
||
import javax.swing.JFrame; | ||
|
||
import com.github.sarxos.webcam.Webcam; | ||
import com.github.sarxos.webcam.WebcamMotionDetector; | ||
import com.github.sarxos.webcam.WebcamMotionDetectorAlgorithm; | ||
import com.github.sarxos.webcam.WebcamMotionDetectorDefaultAlgorithm; | ||
import com.github.sarxos.webcam.WebcamPanel; | ||
import com.github.sarxos.webcam.WebcamResolution; | ||
import com.github.sarxos.webcam.util.ImageUtils; | ||
|
||
|
||
/** | ||
* This example demonstrates how to use build-in motion detector with do-not-engage zones. The | ||
* do-not-engage zone is a rectangular area where motion is ignored and won't trigger motion | ||
* detector. | ||
* | ||
* @author Bartosz Firyn (sarxos) | ||
*/ | ||
public class DetectMotionDoNotEngageZoneExample extends JFrame implements WebcamPanel.Painter { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private static final BufferedImage TEXTURE = ImageUtils.readFromResource("stripes.png"); | ||
private static final Paint PAINT = new TexturePaint(TEXTURE, new Rectangle(0, 0, 10, 10)); | ||
|
||
private static final Dimension RESOLUTION = WebcamResolution.VGA.getSize(); | ||
private static final Rectangle DO_NOT_ENGAGE_ZONE = new Rectangle(0, 0, 320, 480); | ||
|
||
private final Webcam webcam; | ||
private final WebcamPanel panel; | ||
private final WebcamMotionDetector detector; | ||
|
||
public DetectMotionDoNotEngageZoneExample() { | ||
|
||
setTitle("Motion Detector Demo"); | ||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
|
||
webcam = Webcam.getDefault(); | ||
webcam.setViewSize(RESOLUTION); | ||
webcam.open(true); | ||
|
||
panel = new WebcamPanel(webcam, false); | ||
panel.setPainter(this); | ||
panel.start(); | ||
|
||
WebcamMotionDetectorAlgorithm algorithm = new WebcamMotionDetectorDefaultAlgorithm(); | ||
algorithm.setDoNotEngageZones(Arrays.asList(DO_NOT_ENGAGE_ZONE)); | ||
|
||
detector = new WebcamMotionDetector(webcam, algorithm, 500); | ||
detector.setPixelThreshold(20); | ||
detector.start(); | ||
|
||
add(panel); | ||
|
||
pack(); | ||
setVisible(true); | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
new DetectMotionDoNotEngageZoneExample(); | ||
} | ||
|
||
@Override | ||
public void paintPanel(WebcamPanel panel, Graphics2D g2) { | ||
panel.getDefaultPainter().paintPanel(panel, g2); | ||
} | ||
|
||
@Override | ||
public void paintImage(WebcamPanel panel, BufferedImage image, Graphics2D g2) { | ||
|
||
final double s = detector.getMotionArea(); | ||
final Point cog = detector.getMotionCog(); | ||
|
||
final int x = DO_NOT_ENGAGE_ZONE.x; | ||
final int y = DO_NOT_ENGAGE_ZONE.y; | ||
final int w = DO_NOT_ENGAGE_ZONE.width; | ||
final int h = DO_NOT_ENGAGE_ZONE.height; | ||
|
||
Graphics2D g = image.createGraphics(); | ||
g.setPaint(PAINT); | ||
g.fillRect(x, y, w, h); | ||
g.setColor(Color.WHITE); | ||
g.drawString(String.format("Area: %.2f%%", s), 10, 20); | ||
|
||
if (detector.isMotion()) { | ||
g.setStroke(new BasicStroke(2)); | ||
g.setColor(Color.RED); | ||
g.drawOval(cog.x - 5, cog.y - 5, 10, 10); | ||
} else { | ||
g.setColor(Color.GREEN); | ||
g.drawRect(cog.x - 5, cog.y - 5, 10, 10); | ||
} | ||
|
||
g.dispose(); | ||
|
||
panel.getDefaultPainter().paintImage(panel, image, g2); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.