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

Listener for multiple IP Cams #267

Closed
mcicolella opened this issue Oct 10, 2014 · 12 comments
Closed

Listener for multiple IP Cams #267

mcicolella opened this issue Oct 10, 2014 · 12 comments
Labels

Comments

@mcicolella
Copy link

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

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");
        }
    }
@mcicolella
Copy link
Author

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");
        }
    }

@sarxos
Copy link
Owner

sarxos commented Oct 10, 2014

Hi @mcicolella,

Thank you. I'm glad you like it :)

In regards to your question:

Also is it possibile to retrieve the webcam id from WebcamMotionEvent?

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.

@mcicolella
Copy link
Author

Hi sarxos,
thanks for your help. I solved one of the two problems.
I uploaded my code on https://github.com/mcicolella/freedomotic/blob/camera/plugins/devices/camera-motion/src/main/java/com/freedomotic/plugins/devices/cameramotion/CameraMotion.java so anyone can take a look and contribute.
I'm waiting for your news about the other issue.
Thanks

@mcicolella
Copy link
Author

Just a screenshot showing our plugin in action
webcam-motion

@sarxos
Copy link
Owner

sarxos commented Oct 12, 2014

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 webcam.open() or indirectly via detector.start()). In the synchronous mode when there are multiple threads reading images from webcam (here we have two - one from panel, and other one from detector) when the first thread consume the image, the other one have to wait. Therefore, it may be that the panel has already consumed the image, and detector didn't process it, and so, was not able to detect motion. The solution should be to open webcam in asynchronous mode before both detector an panel are started (see example at the end of this post).

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:

number of detectors: 8
Detected motion Webcam Foscam
Detected motion Webcam Biblione Palace
Detected motion Webcam Foscam
Detected motion Webcam Foscam
Detected motion Webcam Foscam
Detected motion Webcam Foscam
Detected motion Webcam Foscam
Detected motion Webcam Foscam
Detected motion Webcam Foscam
Detected motion Webcam Foscam
Detected motion Webcam Biblione Palace
Detected motion Webcam Biblione Palace

Here is example I wrote for myself. This one uses asynchronous mode (webcam.open(true) the true argument causes webcam to work async).

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.

motion detected ---------------- 
webcam:  Webcam Foscam
area:    43.345052083333336%
cog:    [294.0,210.0]
motion detected ---------------- 
webcam:  Webcam Foscam
area:    33.619466145833336%
cog:    [301.0,200.0]
motion detected ---------------- 
webcam:  Webcam Foscam
area:    44.532552083333336%
cog:    [332.0,204.0]
motion detected ---------------- 
webcam:  Webcam Biblione Palace
area:    47.61186079545455%
cog:    [180.0,196.0]
motion detected ---------------- 
webcam:  Webcam Biblione Palace
area:    34.97573390151515%
cog:    [176.0,212.0]
motion detected ---------------- 
webcam:  Webcam Biblione Palace
area:    1.112689393939394%
cog:    [142.0,258.0]
motion detected ---------------- 
webcam:  Webcam Foscam
area:    48.9677734375%
cog:    [334.0,205.0]
motion detected ---------------- 
webcam:  Webcam Foscam
area:    53.771809895833336%
cog:    [301.0,210.0]

@sarxos
Copy link
Owner

sarxos commented Oct 12, 2014

Ah, one more thing. There was a bug #268 which caused that mode="push" (MJPEG) cannot be used in cameras descriptor XML. I already fixed it, but I did not release it to Maven. Please let me know of how important it is, so I can plan release. As a temporary w/a you can use SNAPSHOT version:

<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>

@mcicolella
Copy link
Author

Hi Bartosz
I added the SNAPSHOT (copy&past repository and dependency in pom.xml) but I have a compilation problem. Is the SNAPSHOT available?

COMPILATION ERROR : 
-------------------------------------------------------------
com/freedomotic/plugins/devices/cameramotion/CameraMotion.java:[29,1] package com.github.sarxos.webcam does not exist
com/freedomotic/plugins/devices/cameramotion/CameraMotion.java:[216,12] cannot find symbol
  symbol:   class Webcam
  location: class com.freedomotic.plugins.devices.cameramotion.CameraMotion
com/freedomotic/plugins/devices/cameramotion/CameraMotion.java:[154,42] cannot find symbol
  symbol:   class WebcamMotionListener
  location: class com.freedomotic.plugins.devices.cameramotion.CameraMotion
com/freedomotic/plugins/devices/cameramotion/CameraMotion.java:[156,14] cannot find symbol
  symbol:   class WebcamMotionDetector
  location: class com.freedomotic.plugins.devices.cameramotion.CameraMotion.DetectMotion
com/freedomotic/plugins/devices/cameramotion/CameraMotion.java:[188,36] cannot find symbol
  symbol:   class WebcamMotionEvent
  location: class com.freedomotic.plugins.devices.cameramotion.CameraMotion.DetectMotion
com/freedomotic/plugins/devices/cameramotion/CameraMotion.java:[53,9] cannot find symbol
  symbol:   variable Webcam
etc..

@sarxos
Copy link
Owner

sarxos commented Oct 14, 2014

Hi @mcicolella,

Yes, snapshot is available:

https://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture/0.3.11-SNAPSHOT/

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.

@mcicolella
Copy link
Author

Hi @sarxos
I have a problem with dependencies
Thanks

-----------------------------------------------
Building camera-motion 3.0
------------------------------------------------------------------------
Downloading: https://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/maven-metadata.xml
Downloading: http://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/maven-metadata.xml
Checksum validation failed, expected <html> but is 81ffbd1712afe8cdf138b570c0fc9934742c33c1 for http://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/maven-metadata.xml
Checksum validation failed, expected <html> but is 81ffbd1712afe8cdf138b570c0fc9934742c33c1 for http://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/maven-metadata.xml

Downloaded: http://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/maven-metadata.xml (178 B at 0.0 KB/sec)

Downloaded: https://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/maven-metadata.xml (797 B at 0.1 KB/sec)
The metadata /home/mauro/.m2/repository/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/maven-metadata-Sonatype OSS Snapshot Repository.xml is invalid: end tag name </body> must match start tag name <hr> from line 5 (position: TEXT seen ...</center>\r\n</body>... @6:8) 
Downloading: http://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/webcam-capture-driver-ipcam-0.3.11-20141012.210135-1.pom
Checksum validation failed, expected <html> but is 81ffbd1712afe8cdf138b570c0fc9934742c33c1 for http://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/webcam-capture-driver-ipcam-0.3.11-20141012.210135-1.pom
Checksum validation failed, expected <html> but is 81ffbd1712afe8cdf138b570c0fc9934742c33c1 for http://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/webcam-capture-driver-ipcam-0.3.11-20141012.210135-1.pom

Downloaded: http://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/webcam-capture-driver-ipcam-0.3.11-20141012.210135-1.pom (178 B at 0.2 KB/sec)
The POM for com.github.sarxos:webcam-capture-driver-ipcam:jar:0.3.11-20141012.210135-1 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
Downloading: https://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/webcam-capture-driver-ipcam-0.3.11-20141012.210135-1.jar

Downloaded: https://oss.sonatype.org/content/repositories/snapshots/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/webcam-capture-driver-ipcam-0.3.11-20141012.210135-1.jar (35 KB at 40.6 KB/sec)

[enforcer:enforce]

[resources:resources]
Using 'UTF-8' encoding to copy filtered resources.
Copying 4 resources

[compiler:compile]
Changes detected - recompiling the module!
Compiling 1 source file to /home/mauro/Scrivania/fork/freedomotic/plugins/devices/camera-motion/target/classes
-------------------------------------------------------------
COMPILATION WARNING : 
-------------------------------------------------------------
/home/mauro/Scrivania/fork/freedomotic/plugins/devices/camera-motion/src/main/java/com/freedomotic/plugins/devices/cameramotion/CameraMotion.java: /home/mauro/Scrivania/fork/freedomotic/plugins/devices/camera-motion/src/main/java/com/freedomotic/plugins/devices/cameramotion/CameraMotion.java uses or overrides a deprecated API.
/home/mauro/Scrivania/fork/freedomotic/plugins/devices/camera-motion/src/main/java/com/freedomotic/plugins/devices/cameramotion/CameraMotion.java: Recompile with -Xlint:deprecation for details.
2 warnings 
-------------------------------------------------------------
-------------------------------------------------------------
COMPILATION ERROR : 
-------------------------------------------------------------
com/freedomotic/plugins/devices/cameramotion/CameraMotion.java:[29,1] package com.github.sarxos.webcam does not exist
com/freedomotic/plugins/devices/cameramotion/CameraMotion.java:[216,12] cannot find symbol
  symbol:   class Webcam
  location: class com.freedomotic.plugins.devices.cameramotion.CameraMotion
com/freedomotic/plugins/devices/cameramotion/CameraMotion.java:[154,42] cannot find symbol
  symbol:   class WebcamMotionListener
  location: class com.freedomotic.plugins.devices.cameramotion.CameraMotion
com/freedomotic/plugins/devices/cameramotion/CameraMotion.java:[156,14] cannot find symbol

@sarxos
Copy link
Owner

sarxos commented Oct 15, 2014

The compilation errors are directly caused by this problem (because transitive dependencies are not available when Maven claims that POM is invalid):

The POM for com.github.sarxos:webcam-capture-driver-ipcam:jar:0.3.11-20141012.210135-1 is 
invalid, transitive dependencies (if any) will not be available, enable debug logging for more details

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:

The metadata /home/mauro/.m2/repository/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT/maven-metadata-Sonatype OSS Snapshot Repository.xml
is invalid: end tag name </body> must match start tag name <hr> from line 5 (position: TEXT seen ...</center>\r\n</body>... @6:8) 

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 cat it:

$ cd /home/mauro/.m2/repository/com/github/sarxos/webcam-capture-driver-ipcam/0.3.11-SNAPSHOT
$ cat "maven-metadata-Sonatype OSS Snapshot Repository.xml"

And verify if it contains the following (or similar) content:

<?xml version="1.0" encoding="UTF-8"?>
<metadata modelVersion="1.1.0">
  <groupId>com.github.sarxos</groupId>
  <artifactId>webcam-capture-driver-ipcam</artifactId>
  <version>0.3.11-SNAPSHOT</version>
  <versioning>
    <snapshot>
      <timestamp>20141012.210135</timestamp>
      <buildNumber>1</buildNumber>
    </snapshot>
    <lastUpdated>20141012210135</lastUpdated>
    <snapshotVersions>
      <snapshotVersion>
        <extension>jar</extension>
        <value>0.3.11-20141012.210135-1</value>
        <updated>20141012210135</updated>
      </snapshotVersion>
      <snapshotVersion>
        <extension>pom</extension>
        <value>0.3.11-20141012.210135-1</value>
        <updated>20141012210135</updated>
      </snapshotVersion>
    </snapshotVersions>
  </versioning>
</metadata>

If not, then remove from your local Maven repo everything under com/github/sarxos (this will force Maven to download these dependencies again, hopefully without any problems), and then build project again.

$ cd /home/mauro/.m2/repository
$ rm -rf com/github/sarxos
$ cd /home/mauro/Scrivania/fork/freedomotic/plugins/devices/camera-motion
$ mvn clean install

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.

@mcicolella
Copy link
Author

Hi,
first of all thanks a lot for your help!
I solved with a complete reset of my maven repository.
I integrated your code and works well.
I have some questions:

1 sometimes there are error messages but then the camera works

INFO  <Thread-68> [WebcamOpenTask] Opening webcam Biblione Palace
ERROR <webcam-panel-scheduled-executor-2> [WebcamExceptionHandler] Exception in
thread webcam-panel-scheduled-executor-2
com.github.sarxos.webcam.WebcamException: IpCam device not open
        at com.github.sarxos.webcam.ds.ipcam.IpCamDevice.getImage(IpCamDevice.ja
va:309)
        at com.github.sarxos.webcam.ds.cgt.WebcamGetImageTask.handle(WebcamGetIm
ageTask.java:43)
        at com.github.sarxos.webcam.WebcamTask.process(WebcamTask.java:48)
        at com.github.sarxos.webcam.ds.cgt.WebcamGetImageTask.getImage(WebcamGet
ImageTask.java:26)
        at com.github.sarxos.webcam.Webcam.getImage(Webcam.java:627)
        at com.github.sarxos.webcam.WebcamPanel$ImageUpdater.update(WebcamPanel.
java:568)
        at com.github.sarxos.webcam.WebcamPanel$ImageUpdater.run(WebcamPanel.jav
a:547)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.
access$301(Unknown Source)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.
run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

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.

INFO  <shutdown-hook-1> [Webcam] Disposing webcam Dasding 10
INFO  <shutdown-hook-3> [WebcamShutdownHook] Automatic Dasding 02 deallocation

How can I manage that correctly?

3 what do cog and area represent exactly?

Thanks again for your support.

@sarxos
Copy link
Owner

sarxos commented Oct 17, 2014

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:

sometimes there are error messages but then the camera works

com.github.sarxos.webcam.WebcamException: IpCam device not open
        at com.github.sarxos.webcam.ds.ipcam.IpCamDevice.getImage(IpCamDevice.java:309)
        at com.github.sarxos.webcam.ds.cgt.WebcamGetImageTask.handle(WebcamGetImageTask.java:43)
        at com.github.sarxos.webcam.WebcamTask.process(WebcamTask.java:48)

These are caused by the fact that camera has been closed (or not yet open) and getImage() method is invoked. In case of such situation, the IP camera device should rather return null instead of throwing exception. I think I will change this behaviour.

I'd to deallocate correctly all resources when I call onStop() method

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:

  • closed - you can open it,
  • open - you can close it,
  • disposed - it this state it cannot be open any more because everything is prepared to close the application. You can call webcam.dispose() but please don't do that. This is automatically managed by JVM. Instead you should use webcam.close() which also releases resources allocated under the hood of webcam instance.

what do cog and area represent exactly?

Please take a look on javadocs in WebcamMotionDetector class:

  • cog = center-of-gravity (the center of motion area),
  • area = percentage of complete image pixels area that has been changed between two consecutive images.

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

2 participants