-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Listener for multiple IP Cams #267
Comments
Sorry, there was an error in copying the class public class DetectMotion implements WebcamMotionListener {
List<WebcamMotionDetector> detectors = new ArrayList<WebcamMotionDetector>();
public DetectMotion() {
for (Webcam webcam : Webcam.getWebcams()) {
WebcamMotionDetector detector = new WebcamMotionDetector(webcam);
detector.setInterval(100); // one check per 100 ms (10 FPS)
detector.addMotionListener(this);
detectors.add(detector);
}
for (WebcamMotionDetector detector : detectors) {
detector.start();
}
System.out.println(detectors.size());
}
@Override
public void motionDetected(WebcamMotionEvent wme) {
System.out.println("Detected motion " + wme.getSource().toString() + ", alarm turn on you have");
}
} |
Hi @mcicolella, Thank you. I'm glad you like it :) In regards to your question:
Yes, there is: @Override
public void motionDetected(WebcamMotionEvent wme) {
Webcam webcam = ((WebcamMotionDetector) wme.getSource()).getWebcam();
IpCamDevice device = (IpCamDevice) webcam.getDevice(); // in case whe IP camera driver is used
System.out.println("Detected motion I, alarm turn on you have");
System.out.println("Webcam name: " + webcam.getName());
System.out.println("Webcam URL: " + device.getURL());
} But in regards to your prime issue, which is motion detected for the first webcam only, I will need to verify it. Will come back to you later today or tomorrow. |
Hi sarxos, |
Hi @mcicolella, I'm impressed of how many various devices are support in Freedomotic project. This is very good piece of cool stuff! I'm sure I will keep an eye on it :) But in regards to the root of your problem, I think it may be caused by the fact that camera is open in synchronous mode (default one when you call it as Dasding Studio was already closed so I found two cameras I can manipulate to simulate motion detection:
This is my XML cameras descriptor for test: <?xml version="1.0" encoding="UTF-8" ?>
<storage>
<ipcam name="Dasding 01" url="http://www.dasding.de/ext/webcam/webcam770.php?cam=1" />
<ipcam name="Dasding 02" url="http://www.dasding.de/ext/webcam/webcam770.php?cam=2" />
<ipcam name="Dasding 04" url="http://www.dasding.de/ext/webcam/webcam770.php?cam=4" />
<ipcam name="Dasding 06" url="http://www.dasding.de/ext/webcam/webcam770.php?cam=6" />
<ipcam name="Dasding 07" url="http://www.dasding.de/ext/webcam/webcam770.php?cam=7" />
<ipcam name="Dasding 10" url="http://www.dasding.de/ext/webcam/webcam770.php?cam=10" />
<ipcam name="Biblione Palace" url="http://webcam.hotelbibionepalace.it/mjpg/video.mjpg" mode="push" />
<ipcam name="Foscam" url="http://50.197.211.181:8910/videostream.cgi" mode="push">
<auth user="user" password="foscam" />
</ipcam>
</storage> With your code, a little bit modified: import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamMotionDetector;
import com.github.sarxos.webcam.WebcamMotionEvent;
import com.github.sarxos.webcam.WebcamMotionListener;
import com.github.sarxos.webcam.ds.ipcam.IpCamDriver;
import com.github.sarxos.webcam.ds.ipcam.IpCamStorage;
public class DetectMotion implements WebcamMotionListener {
static {
Webcam.setDriver(new IpCamDriver(new IpCamStorage("src/examples/resources/cameras.xml")));
}
List<WebcamMotionDetector> detectors = new ArrayList<WebcamMotionDetector>();
public DetectMotion() {
for (Webcam webcam : Webcam.getWebcams()) {
WebcamMotionDetector detector = new WebcamMotionDetector(webcam);
detector.setInterval(100); // one check per 100 ms (10 FPS)
detector.addMotionListener(this);
detectors.add(detector);
}
for (WebcamMotionDetector detector : detectors) {
detector.start();
}
System.out.println("number of detectors: " + detectors.size());
}
@Override
public void motionDetected(WebcamMotionEvent wme) {
System.out.println("Detected motion " + ((WebcamMotionDetector) wme.getSource()).getWebcam());
}
public static void main(String[] args) throws IOException {
new DetectMotion();
System.in.read(); // wait for key pressed
}
} I get this output:
Here is example I wrote for myself. This one uses asynchronous mode ( import java.awt.Dimension;
import java.awt.GridLayout;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamMotionDetector;
import com.github.sarxos.webcam.WebcamMotionEvent;
import com.github.sarxos.webcam.WebcamMotionListener;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.ds.ipcam.IpCamDriver;
import com.github.sarxos.webcam.ds.ipcam.IpCamStorage;
public class DetectMotionFromIpCamerasExample {
static {
Webcam.setDriver(new IpCamDriver(new IpCamStorage("src/examples/resources/cameras.xml")));
}
public static void main(String[] args) throws MalformedURLException {
JFrame f = new JFrame("Detect Motion Multiple Cameras");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(0, 3, 1, 1));
WebcamMotionListener listener = new WebcamMotionListener() {
@Override
public void motionDetected(WebcamMotionEvent wme) {
synchronized (this) {
System.out.println("motion detected ---------------- ");
System.out.println("webcam: " + ((WebcamMotionDetector) wme.getSource()).getWebcam());
System.out.println("area: " + wme.getArea() + "%");
System.out.println("cog: [" + wme.getCog().getX() + "," + wme.getCog().getY() + "]");
}
}
};
final List<Webcam> webcams = new ArrayList<Webcam>();
final List<WebcamPanel> panels = new ArrayList<WebcamPanel>();
final List<WebcamMotionDetector> detectors = new ArrayList<WebcamMotionDetector>();
for (Webcam webcam : Webcam.getWebcams()) {
webcams.add(webcam);
WebcamPanel panel = new WebcamPanel(webcam, new Dimension(256, 144), false);
panel.setFitArea(true);
panel.setFPSLimited(true);
panel.setFPSLimit(0.5); // 0.5 FPS = 1 frame per 2 seconds
panel.setBorder(BorderFactory.createEmptyBorder());
f.add(panel);
panels.add(panel);
WebcamMotionDetector detector = new WebcamMotionDetector(webcam);
detector.addMotionListener(listener);
detector.setInterval(100); // one motion check per 2 seconds
detectors.add(detector);
}
f.pack();
f.setVisible(true);
for (int i = 0; i < webcams.size(); i++) {
final int x = i;
Thread t = new Thread() {
@Override
public void run() {
webcams.get(x).open(); // open in asynchronous mode
panels.get(x).start();
detectors.get(x).start();
}
};
t.setDaemon(true);
t.start();
}
}
} And the output from tests. As you can see, motion from both cameras I manipulated (Foscam and Biblione Palace) has been detected.
|
Ah, one more thing. There was a bug #268 which caused that <repository>
<id>Sonatype OSS Snapshot Repository</id>
<url>http://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture-driver-ipcam</artifactId>
<version>0.3.11-SNAPSHOT</version>
</dependency> |
Hi Bartosz
|
Hi @mcicolella, Yes, snapshot is available: I created yet another pull request to fix a bug I accidentally dropped in your code and to add dependency/repository into camera-motion device plugin. |
Hi @sarxos
|
The compilation errors are directly caused by this problem (because transitive dependencies are not available when Maven claims that POM is invalid):
However I tested camera-motion plugin build on other machine, with clean (removed) Maven repository and didn't observe such problem. I checked the POM and it's valid. The problem may be caused by previous issue:
It seems to be some strange Maven problem cause by the fact that the above file contains some weird HTML instead of valid XML metadata. Please
And verify if it contains the following (or similar) content:
If not, then remove from your local Maven repo everything under
I had similar issue in the past when I was behind http proxy and my PC configuration was incorrect. Maven was trying to download files from repository, but instead it saved error page (html files) from the proxy itself instead of repository server... In all such cases, after incorrect files are removed from local repo and configuration and/or repository is back online, the problem should disappear with next build. |
Hi, 1 sometimes there are error messages but then the camera works
2 I'd to deallocate correctly all resources when I call onStop() method. I can start/stop my plugin without stopped Freedomotic instance. For example I can stop the plugin, modify camera.xml and restart it when Freedomotic is running. But disposing and deallocation are executed only at exit.
How can I manage that correctly? 3 what do cog and area represent exactly? Thanks again for your support. |
Hi @mcicolella, I'm glad to her it. Maven can be very frustrating sometimes, but I cannot imagine to work without it ;P In regards to your questions:
These are caused by the fact that camera has been closed (or not yet open) and
Just use: onStop() {
for (WebcamMotionDetexctor detector : detectors) {
detector.stop();
}
for (WebcamPanel panel : panels) {
panel.stop();
}
for (Webcam webcam : webcams) {
webcam.close()
}
} This will close the webcam and release allocated resources. It should also stop rendering on panes attached to this webcam and stop motion detector (on next motion detection tick). Webcam can be in three states:
Please take a look on javadocs in WebcamMotionDetector class:
|
Hi,
first of all congratulations for your great work!
I'm trying to create a plugin for our domotic system to detect motion from multiple ip cams.
I can view all sources in different panels, but it detects motion only for the first one.
I'm using listener approach. Also is it possibile to retrieve the webcam id from WebcamMotionEvent?
Thanks for help
In attachment my class
The text was updated successfully, but these errors were encountered: