sarxos / webcam-capture

The goal of this project is to allow integrated or USB-connected webcams to be accessed directly from Java. Using provided libraries users are able to read camera images and detect motion. Main project consist of several sub projects - the root one, which contains required classes, build-in webcam driver compatible with Windows, Linux and Mac OS, which can stream images as fast as your camera can serve them (up to 50 FPS). Main project can be used standalone, but user is able to replace build-in driver with different one - such as OpenIMAJ, GStreamer, V4L4j, JMF, LTI-CIVIL, FMJ, etc.
http://webcam-capture.sarxos.pl
MIT License
2.27k stars 1.11k forks source link

Raspberry Pi 3 - getImage is null - plenty of info attached #495

Open marcuscraske opened 8 years ago

marcuscraske commented 8 years ago

webcam.getImage() is returning null after some time, looks to be timing out.

This is using a Raspberry Pi 3 with Debian Jesse (minimum), with libjpeg8-dev and libv4l-dev dependencies installed. Webcam is a Logitech C170, which is definitely working, since e.g. "fswebcam test.jpg" is using /dev/video0.

The program works fine on Ubuntu 14.04.

Current version:

pi@raspberrypi:~ $ uname -r 4.4.13-v7+

pi@raspberrypi:~ $ java -version openjdk version "1.8.0_40-internal" OpenJDK Runtime Environment (build 1.8.0_40-internal-b04) OpenJDK Zero VM (build 25.40-b08, interpreted mode)

POM dependencies:

<!-- Webcam API -->
        <dependency>
            <groupId>com.github.sarxos</groupId>
            <artifactId>webcam-capture</artifactId>
            <version>0.3.11</version>
        </dependency>

        <!-- Support for Pi -->
        <dependency>
            <groupId>com.nativelibs4java</groupId>
            <artifactId>bridj</artifactId>
            <version>0.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.sarxos</groupId>
            <artifactId>webcam-capture-driver-v4l4j</artifactId>
            <version>0.3.11</version>
        </dependency>
        <!-- https://github.com/sarxos/webcam-capture/wiki/How-To-Configure-Raspberry-Pi -->
        <dependency>
            <groupId>com.github.sarxos</groupId>
            <artifactId>v4l4j</artifactId>
            <version>0.9.1-r507</version>
        </dependency>

        <!-- webcam-capture log libs -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.0.9</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.9</version>
        </dependency>

Code:

static
    {
        System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug");
        System.setProperty("org.slf4j.simpleLogger.log.com.github.sarxos.webcam.ds.v4l4j", "trace");

        WebcamLogConfigurator.configure("logback.xml");

        Webcam.setDriver(new V4l4jDriver());
    }

    public static void main(String[] args) throws Exception
    {
        System.out.println("starting execution...");

        Webcam webcam = Webcam.getDefault();

        if (webcam == null)
        {
            System.out.println("no webcam found, exiting");
            return;
        }
        else
        {
            System.out.println("webcam found... - " + webcam.getName() + " / " + webcam.getFPS());
        }

        webcam.setViewSize(new Dimension(640, 480));
        webcam.open(true);
        System.out.println("webcam open");

        try
        {
            BufferedImage img = null;
            int attempt = 0;
            while (img == null)
            {
                try
                {
                    System.out.println("attempt #" + attempt++ + "...");
                    img = webcam.getImage();
                }
                catch (Exception e)
                {
                    e.printStackTrace(System.err);
                }
            }

            System.out.println("writing to file...");
            ImageIO.write(img, "PNG", new File("test.png"));
        }
        finally
        {
            webcam.close();
        }

        System.out.println("done...");
    }
 }

Example output from execution:

pi@raspberrypi:~ $ sudo java -jar main-1.0-jar-with-dependencies.jar 21:46:43,243 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] 21:46:43,244 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] 21:46:43,247 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/home/pi/main-1.0-jar-with-dependencies.jar!/logback.xml] 21:46:43,460 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@bfe836 - URL [jar:file:/home/pi/main-1.0-jar-with-dependencies.jar!/logback.xml] is not of type file 21:46:44,274 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set 21:46:44,371 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 30 seconds 21:46:44,371 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[]] every 30 seconds. 21:46:44,371 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter 21:46:44,402 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender] 21:46:44,441 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT] 21:46:45,501 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - This appender no longer admits a layout as a sub-component, set an encoder instead. 21:46:45,501 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder. 21:46:45,501 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details 21:46:45,504 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG 21:46:45,504 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT] 21:46:45,508 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 21:46:45,516 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@1f5659e - Registering current configuration as safe fallback point

21:46:45.620 [main] ERROR c.g.s.w.log.WebcamLogConfigurator - File not found logback.xml java.io.FileNotFoundException: logback.xml (No such file or directory) at java.io.FileInputStream.open0(Native Method) ~[na:1.8.0_40-internal] at java.io.FileInputStream.open(FileInputStream.java:195) ~[na:1.8.0_40-internal] at java.io.FileInputStream.(FileInputStream.java:138) ~[na:1.8.0_40-internal] at com.github.sarxos.webcam.log.WebcamLogConfigurator.configure(WebcamLogConfigurator.java:67) [main-1.0-jar-with-dependencies.jar:na] at com.github.sarxos.webcam.log.WebcamLogConfigurator.configure(WebcamLogConfigurator.java:90) [main-1.0-jar-with-dependencies.jar:na] at com.limpygnome.cctv.Main.(Main.java:37) [main-1.0-jar-with-dependencies.jar:na] java.io.FileNotFoundException: logback.xml (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.(FileInputStream.java:138) at com.github.sarxos.webcam.log.WebcamLogConfigurator.configure(WebcamLogConfigurator.java:67) at com.github.sarxos.webcam.log.WebcamLogConfigurator.configure(WebcamLogConfigurator.java:90) at com.limpygnome.cctv.Main.(Main.java:37) 21:46:46.063 [main] DEBUG com.github.sarxos.webcam.Webcam - Setting new capture driver V4l4jDriver starting execution... 21:46:46.125 [webcam-discovery-service] DEBUG c.g.s.webcam.ds.v4l4j.V4l4jDriver - Video file detected /dev/video0 21:46:46.168 [webcam-discovery-service] DEBUG c.g.s.webcam.ds.v4l4j.V4l4jDevice - Creating V4L4J device from file /dev/video0 [ libvideo.c:68 ] Using libvideo version 0.9-UNKNOWN 21:46:46.689 [webcam-discovery-service] DEBUG c.g.s.webcam.ds.v4l4j.V4l4jDevice - Best image format match MJPEG - 48 21:46:46.782 [main] INFO c.g.s.webcam.WebcamDiscoveryService - Discovery will not run - driver V4l4jDriver does not support this feature webcam found... - /dev/video0 / 0.0 21:46:46.803 [main] DEBUG com.github.sarxos.webcam.WebcamLock - Lock Webcam /dev/video0 21:46:46.856 [atomic-processor-1] INFO c.g.s.webcam.ds.cgt.WebcamOpenTask - Opening webcam /dev/video0 21:46:46.859 [atomic-processor-1] DEBUG c.g.s.webcam.ds.v4l4j.V4l4jDevice - Opening V4L4J device /dev/video0 21:46:46.860 [atomic-processor-1] DEBUG c.g.s.webcam.ds.v4l4j.V4l4jDevice - Constructing V4L4J frame grabber 21:46:46.995 [atomic-processor-1] DEBUG c.g.s.webcam.ds.v4l4j.V4l4jDevice - Starting V4L4J frame grabber 21:46:47.009 [atomic-processor-1] DEBUG c.g.s.webcam.ds.v4l4j.V4l4jDevice - Webcam V4L4J is now open 21:46:47.011 [main] DEBUG com.github.sarxos.webcam.Webcam - Webcam is now open /dev/video0 21:46:50.036 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:46:50.047 [main] DEBUG c.github.sarxos.webcam.WebcamUpdater - Webcam updater has been started webcam open attempt #0... 21:46:53.048 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:46:56.052 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:46:59.055 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:00.272 [main] ERROR c.github.sarxos.webcam.WebcamUpdater - Image has not been found for more than 10 seconds attempt #1... 21:47:02.058 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:05.061 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:08.064 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:10.485 [main] ERROR c.github.sarxos.webcam.WebcamUpdater - Image has not been found for more than 10 seconds attempt #2... 21:47:11.066 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:14.069 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:17.072 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:20.075 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:20.697 [main] ERROR c.github.sarxos.webcam.WebcamUpdater - Image has not been found for more than 10 seconds attempt #3... 21:47:23.078 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:26.080 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:29.083 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:30.909 [main] ERROR c.github.sarxos.webcam.WebcamUpdater - Image has not been found for more than 10 seconds attempt #4... 21:47:32.086 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:35.089 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:38.092 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:41.095 [atomic-processor-1] ERROR c.g.s.webcam.ds.v4l4j.V4l4jDevice - UNable to get image in {} seconds timeout 21:47:41.122 [main] ERROR c.github.sarxos.webcam.WebcamUpdater - Image has not been found for more than 10 seconds

marcuscraske commented 8 years ago

Seems to be an issue with OpenJDK, since this works with oracle-jdk-8.

ghost commented 8 years ago

I was experiencing a similar issue where it was working on my desktop but couldn't get it to work with the raspberry pi. After removing openjdk8 and installing oracle-java8-jdk problem is solved. Maybe this should be stated on how to configure raspberry pi wiki.