From 12dcb465db7a62f7f2eb986e21ce312e670e9ec0 Mon Sep 17 00:00:00 2001 From: Bartosz Firyn Date: Tue, 10 Jul 2012 23:25:27 +0200 Subject: [PATCH] Add OpenIMAJ data source --- .gitignore | 2 + README.md | 25 ++ pom.xml | 340 ++++++++---------- webcam-capture-civil-ds/pom.xml | 4 +- .../webcam/ds/civil/CivilDataSource.java | 9 + webcam-capture-jmf-ds/pom.xml | 11 +- .../{ => webcam}/ds/jmf/JMFDataSource.java | 18 +- .../sarxos/{ => webcam}/ds/jmf/JMFDevice.java | 2 +- webcam-capture-openimaj-ds/.classpath | 7 + webcam-capture-openimaj-ds/.project | 23 ++ webcam-capture-openimaj-ds/pom.xml | 62 ++++ .../ds/openimaj/OpenImajDataSource.java | 46 +++ .../ds/openimaj/OpenImajWebcamDevice.java | 115 ++++++ webcam-capture/pom.xml | 6 +- .../java/com/github/sarxos/webcam/Webcam.java | 4 + 15 files changed, 453 insertions(+), 221 deletions(-) rename webcam-capture-jmf-ds/src/main/java/com/github/sarxos/{ => webcam}/ds/jmf/JMFDataSource.java (63%) rename webcam-capture-jmf-ds/src/main/java/com/github/sarxos/{ => webcam}/ds/jmf/JMFDevice.java (95%) create mode 100644 webcam-capture-openimaj-ds/.classpath create mode 100644 webcam-capture-openimaj-ds/.project create mode 100644 webcam-capture-openimaj-ds/pom.xml create mode 100644 webcam-capture-openimaj-ds/src/main/java/com/github/sarxos/webcam/ds/openimaj/OpenImajDataSource.java create mode 100644 webcam-capture-openimaj-ds/src/main/java/com/github/sarxos/webcam/ds/openimaj/OpenImajWebcamDevice.java diff --git a/.gitignore b/.gitignore index 6d59a8f4..96da8653 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,6 @@ webcam-capture-jmf-ds/.settings webcam-capture-jmf-ds/target webcam-capture/.settings webcam-capture/target +webcam-capture-openimaj-ds/.settings +webcam-capture-openimaj-ds/target diff --git a/README.md b/README.md index ea615170..6df6977a 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,31 @@ registered before you start using webcam. Webcam.setDataSource(new CivilDataSource()); ``` +### OpenIMAJ + +OpenIMAJ is a collection of libraries for multimedia analysis written in the Java +programming language. + +To use it as a data source you can add this dependency in your pom: + +```xml + + com.sarxos.webcam + webcam-capture-openimaj-ds + 0.3 + +``` + +*NOTE!* I found that video capturing (via webcam) is not yet stable in OpenIMAJ, so +I would not recommend it for production systems right now. However whole library seems +to be growing and we can find it to be very usable piece of code in the near future. + +To make ```Webcam``` using this data source: + +```java +Webcam.setDataSource(new OpenImajDataSource()); +``` + ### JMF (Java Media Framework) Supported by Oracle (previously Sun), it is really old, first version is from 1998 and 2.0 diff --git a/pom.xml b/pom.xml index f71976db..aeffe568 100644 --- a/pom.xml +++ b/pom.xml @@ -1,198 +1,144 @@ - - -4.0.0 - -com.github.sarxos -webcam-capture-parent -0.3 - -Webcam Capture - Root POM -This is Webcam Capture root POM -pom - - -SarXos Design -http://www.sarxos.pl - - - - -sarxos-ftp-repo -ftp://ftp.sarxos.pl/maven2 - - - - - - -org.apache.maven.plugins -maven-compiler-plugin - -1.5 -1.5 - - - - - - - -webcam-capture -webcam-capture-civil-ds -webcam-capture-jmf-ds - - - - -notest - - - -org.apache.maven.plugins -maven-surefire-plugin - -true - - - - - - - - - -org.slf4j -slf4j-api -1.6.1 -jar -compile - - -ch.qos.logback -logback-core -0.9.28 -jar -compile - - -ch.qos.logback -logback-classic -0.9.28 -jar -compile - - -commons-io -commons-io -2.0.1 -jar -compile - - -commons-io -commons-io -2.0.1 -jar -sources -compile - - -junit -junit -4.8.2 -jar -test - - -org.easymock -easymock -3.0 -jar -test - - -org.easymock -easymockclassextension -3.0 -jar -test - - - - - -org.apache.maven.plugins -maven-javadoc-plugin -2.8 - - -aggregate - -aggregate - - - - - - -org.apache.maven.plugins -maven-jxr-plugin -2.1 - -true -true - - - -org.apache.maven.plugins -maven-pmd-plugin -2.4 - -true -utf-8 -100 -1.6 -true - - - -org.apache.maven.plugins -maven-project-info-reports-plugin -2.1.2 - - - -index -dependencies - - - - - -org.codehaus.mojo -taglist-maven-plugin -2.4 - - -TODO -@todo -FIXME -XXX - -true - - - -com.googlecode.maven-overview-plugin -maven-overview-plugin -1.6 -false - -com.sarxos.medusa - - - - - + + + + 4.0.0 + + com.github.sarxos + webcam-capture-parent + 0.3 + + Webcam Capture - Root POM + This is Webcam Capture root POM + pom + + + SarXos Design + http://www.sarxos.pl + + + + + sarxos-ftp-repo + ftp://ftp.sarxos.pl/maven2 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + + + + + webcam-capture + webcam-capture-civil-ds + webcam-capture-jmf-ds + webcam-capture-openimaj-ds + + + + + notest + + + + org.apache.maven.plugins + maven-surefire-plugin + + true + + + + + + + + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.8 + + + aggregate + + aggregate + + + + + + + org.apache.maven.plugins + maven-jxr-plugin + 2.1 + + true + true + + + + org.apache.maven.plugins + maven-pmd-plugin + 2.4 + + true + utf-8 + 100 + 1.6 + true + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 2.1.2 + + + + index + dependencies + + + + + + org.codehaus.mojo + taglist-maven-plugin + 2.4 + + + TODO + @todo + FIXME + XXX + + true + + + + com.googlecode.maven-overview-plugin + maven-overview-plugin + 1.6 + false + + com.github.sarxos.webcam + + + + + \ No newline at end of file diff --git a/webcam-capture-civil-ds/pom.xml b/webcam-capture-civil-ds/pom.xml index c4f093bc..45e4bded 100644 --- a/webcam-capture-civil-ds/pom.xml +++ b/webcam-capture-civil-ds/pom.xml @@ -4,7 +4,7 @@ com.github.sarxos.webcam-capture webcam-capture-civil-ds - 0.3 + 0.3-SNAPSHOT Webcam Capture - LTI CIVIL Data Source This is LTI Civil implementation of webcam data source. It allows you to capture webcam media without JMF installed. @@ -27,7 +27,7 @@ com.github.sarxos webcam-capture - 0.3 + 0.3-SNAPSHOT org.lti-civil diff --git a/webcam-capture-civil-ds/src/main/java/com/github/sarxos/webcam/ds/civil/CivilDataSource.java b/webcam-capture-civil-ds/src/main/java/com/github/sarxos/webcam/ds/civil/CivilDataSource.java index 809a66ab..b8e3cd33 100644 --- a/webcam-capture-civil-ds/src/main/java/com/github/sarxos/webcam/ds/civil/CivilDataSource.java +++ b/webcam-capture-civil-ds/src/main/java/com/github/sarxos/webcam/ds/civil/CivilDataSource.java @@ -1,5 +1,6 @@ package com.github.sarxos.webcam.ds.civil; +import java.awt.Dimension; import java.util.ArrayList; import java.util.List; @@ -64,4 +65,12 @@ public List getDevices() { return devices; } + + public static void main(String[] args) { + WebcamDevice device = new CivilDataSource().getDevices().get(0); + for (Dimension d : device.getSizes()) { + System.out.println(d); + } + + } } diff --git a/webcam-capture-jmf-ds/pom.xml b/webcam-capture-jmf-ds/pom.xml index 0e9c0b02..f75c6c9b 100644 --- a/webcam-capture-jmf-ds/pom.xml +++ b/webcam-capture-jmf-ds/pom.xml @@ -4,7 +4,7 @@ com.github.sarxos.webcam-capture webcam-capture-jmf-ds - 0.3 + 0.3-SNAPSHOT Webcam Capture - JMF Data Source This is JMF implementation of webcam data source. It allows you to capture webcam media with JMF or FMJ installed. @@ -16,11 +16,18 @@ + + + sarxos-repo + http://repo.sarxos.pl/maven2 + + + com.github.sarxos webcam-capture - 0.3 + 0.3-SNAPSHOT javax.media diff --git a/webcam-capture-jmf-ds/src/main/java/com/github/sarxos/ds/jmf/JMFDataSource.java b/webcam-capture-jmf-ds/src/main/java/com/github/sarxos/webcam/ds/jmf/JMFDataSource.java similarity index 63% rename from webcam-capture-jmf-ds/src/main/java/com/github/sarxos/ds/jmf/JMFDataSource.java rename to webcam-capture-jmf-ds/src/main/java/com/github/sarxos/webcam/ds/jmf/JMFDataSource.java index 7a69ddbb..fcab6d09 100644 --- a/webcam-capture-jmf-ds/src/main/java/com/github/sarxos/ds/jmf/JMFDataSource.java +++ b/webcam-capture-jmf-ds/src/main/java/com/github/sarxos/webcam/ds/jmf/JMFDataSource.java @@ -1,14 +1,10 @@ -package com.github.sarxos.ds.jmf; +package com.github.sarxos.webcam.ds.jmf; -import java.awt.Dimension; -import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Vector; -import javax.imageio.ImageIO; import javax.media.CaptureDeviceInfo; import javax.media.CaptureDeviceManager; import javax.media.Format; @@ -39,16 +35,4 @@ public List getDevices() { return devices; } - - public static void main(String[] args) throws IOException { - - WebcamDevice device = new JMFDataSource().getDevices().get(0); - device.setSize(new Dimension(176, 144)); - device.open(); - - for (int i = 0; i < 10; i++) { - ImageIO.write(device.getImage(), "JPG", new File(i + ".jpg")); - } - - } } diff --git a/webcam-capture-jmf-ds/src/main/java/com/github/sarxos/ds/jmf/JMFDevice.java b/webcam-capture-jmf-ds/src/main/java/com/github/sarxos/webcam/ds/jmf/JMFDevice.java similarity index 95% rename from webcam-capture-jmf-ds/src/main/java/com/github/sarxos/ds/jmf/JMFDevice.java rename to webcam-capture-jmf-ds/src/main/java/com/github/sarxos/webcam/ds/jmf/JMFDevice.java index 68d6484a..0cbd0866 100644 --- a/webcam-capture-jmf-ds/src/main/java/com/github/sarxos/ds/jmf/JMFDevice.java +++ b/webcam-capture-jmf-ds/src/main/java/com/github/sarxos/webcam/ds/jmf/JMFDevice.java @@ -1,4 +1,4 @@ -package com.github.sarxos.ds.jmf; +package com.github.sarxos.webcam.ds.jmf; import java.awt.Dimension; import java.awt.Graphics2D; diff --git a/webcam-capture-openimaj-ds/.classpath b/webcam-capture-openimaj-ds/.classpath new file mode 100644 index 00000000..756245a6 --- /dev/null +++ b/webcam-capture-openimaj-ds/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/webcam-capture-openimaj-ds/.project b/webcam-capture-openimaj-ds/.project new file mode 100644 index 00000000..2cc8e37a --- /dev/null +++ b/webcam-capture-openimaj-ds/.project @@ -0,0 +1,23 @@ + + + webcam-capture-openimaj-ds + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/webcam-capture-openimaj-ds/pom.xml b/webcam-capture-openimaj-ds/pom.xml new file mode 100644 index 00000000..34e96192 --- /dev/null +++ b/webcam-capture-openimaj-ds/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + com.sarxos.webcam + webcam-capture-openimaj-ds + 0.3-SNAPSHOT + + Webcam Capture - OpenIMAJ Data Source + Webcam Capture data source using OpenIMAJ libraries + + + + OpenIMAJ maven repository + http://maven.openimaj.org/ + + + Octopussy thirdparty maven repository + http://octopussy.ecs.soton.ac.uk/m2/thirdparty/ + + + sarxos-repo + http://repo.sarxos.pl/maven2 + + + + + + + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + maven-assembly-plugin + 2.2.1 + + + jar-with-dependencies + + + + + + + + + com.github.sarxos + webcam-capture + 0.3-SNAPSHOT + + + org.openimaj + core-video-capture + 1.0.5 + + + + diff --git a/webcam-capture-openimaj-ds/src/main/java/com/github/sarxos/webcam/ds/openimaj/OpenImajDataSource.java b/webcam-capture-openimaj-ds/src/main/java/com/github/sarxos/webcam/ds/openimaj/OpenImajDataSource.java new file mode 100644 index 00000000..620d7843 --- /dev/null +++ b/webcam-capture-openimaj-ds/src/main/java/com/github/sarxos/webcam/ds/openimaj/OpenImajDataSource.java @@ -0,0 +1,46 @@ +package com.github.sarxos.webcam.ds.openimaj; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.imageio.ImageIO; + +import org.openimaj.video.capture.Device; +import org.openimaj.video.capture.VideoCapture; + +import com.github.sarxos.webcam.WebcamDataSource; +import com.github.sarxos.webcam.WebcamDevice; + + +public class OpenImajDataSource implements WebcamDataSource { + + List webcamDevices = null; + + @Override + public List getDevices() { + + if (webcamDevices == null) { + webcamDevices = new ArrayList(); + List devices = VideoCapture.getVideoDevices(); + for (Device device : devices) { + webcamDevices.add(new OpenImajWebcamDevice(device)); + } + } + + return webcamDevices; + } + + public static void main(String[] args) throws IOException, InterruptedException { + WebcamDevice d = new OpenImajDataSource().getDevices().get(0); + d.setSize(d.getSizes()[0]); + d.open(); + for (int i = 0; i < 5; i++) { + ImageIO.write(d.getImage(), "JPG", new File(System.currentTimeMillis() + ".jpg")); + Thread.sleep(1000); + } + + d.close(); + } +} diff --git a/webcam-capture-openimaj-ds/src/main/java/com/github/sarxos/webcam/ds/openimaj/OpenImajWebcamDevice.java b/webcam-capture-openimaj-ds/src/main/java/com/github/sarxos/webcam/ds/openimaj/OpenImajWebcamDevice.java new file mode 100644 index 00000000..880d88f9 --- /dev/null +++ b/webcam-capture-openimaj-ds/src/main/java/com/github/sarxos/webcam/ds/openimaj/OpenImajWebcamDevice.java @@ -0,0 +1,115 @@ +package com.github.sarxos.webcam.ds.openimaj; + +import java.awt.Dimension; +import java.awt.image.BufferedImage; + +import org.openimaj.image.ImageUtilities; +import org.openimaj.video.capture.Device; +import org.openimaj.video.capture.VideoCapture; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.github.sarxos.webcam.WebcamDevice; + + +public class OpenImajWebcamDevice implements WebcamDevice { + + private static final Logger LOG = LoggerFactory.getLogger(OpenImajDataSource.class); + + /** + * Artificial view sizes. I'm really not sure if will fit into other webcams + * but hope that OpenIMAJ can handle this. + */ + private final static Dimension[] dimensions = new Dimension[] { + new Dimension(176, 144), + new Dimension(320, 240), + new Dimension(352, 288), + new Dimension(640, 400), + new Dimension(640, 480), + new Dimension(1280, 720), + }; + + private Device device = null; + private VideoCapture capture = null; + private Dimension size = null; + private boolean open = false; + + public OpenImajWebcamDevice(Device device) { + this.device = device; + } + + @Override + public String getName() { + return device.getNameStr(); + } + + @Override + public Dimension[] getSizes() { + return dimensions; + } + + @Override + public Dimension getSize() { + return size; + } + + @Override + public void setSize(Dimension size) { + if (open) { + throw new RuntimeException("Cannot set new size when device is open, please close it first"); + } + this.size = size; + } + + @Override + public BufferedImage getImage() { + + if (!open) { + throw new RuntimeException("Cannot get image from closed device"); + } + + // TODO scale to dimension if not equal + return ImageUtilities.createBufferedImage(capture.getNextFrame()); + } + + @Override + public void open() { + + if (open) { + return; + } + + capture = new VideoCapture(size.width, size.height, device); + open = true; + + // what the hell is that something below? that's ugly w/a for blue + // images at the very capture beginning, if you have some other idea of + // how to remove them, please share or fix + + int i = 0; + do { + // capture.getNextFrame(); + // try { + // Thread.sleep(1000); + // } catch (InterruptedException e) { + // } + } while (i++ < 5); + + LOG.info("OpenIMAJ webcam device has been initialized"); + } + + @Override + public void close() { + + if (!open) { + return; + } + + capture.stopCapture(); + + open = false; + + LOG.info("OpenIMAJ webcam device has been closed"); + } + +} diff --git a/webcam-capture/pom.xml b/webcam-capture/pom.xml index 4d9888b2..59086ba5 100644 --- a/webcam-capture/pom.xml +++ b/webcam-capture/pom.xml @@ -4,7 +4,7 @@ com.github.sarxos webcam-capture - 0.3 + 0.3-SNAPSHOT Use your webcam directly from Java @@ -22,7 +22,9 @@ - https://github.com/sarxos/webcam-capture + scm:git:git@github.com:sarxos/webcam-capture.git + scm:git:git@github.com:sarxos/webcam-capture.git + git@github.com:sarxos/webcam-capture.git diff --git a/webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java b/webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java index e2431e0e..e66d892f 100644 --- a/webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java +++ b/webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java @@ -38,6 +38,10 @@ public Webcam(WebcamDevice device) { */ public synchronized void open() { + if (device.getSize() == null) { + device.setSize(device.getSizes()[0]); + } + device.open(); open = true;