Skip to content

Commit

Permalink
rhino eye from git project
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://svn.forge.ecs.soton.ac.uk/projects/ecsrhino/rhinoeye@55 408bdfbd-0bd4-0410-88bf-c0b946b28f18
  • Loading branch information
ss committed May 24, 2013
0 parents commit 6ae60a4
Show file tree
Hide file tree
Showing 27 changed files with 1,936 additions and 0 deletions.
149 changes: 149 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rhino</groupId>
<artifactId>rhinoeye</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Rhino Eye</name>
<url>http://www.openimaj.org</url>
<properties>
<openimaj.version>1.1.1-SNAPSHOT</openimaj.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.openimaj</groupId>
<artifactId>core-video-capture</artifactId>
<version>${openimaj.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.openimaj</groupId>
<artifactId>video-processing</artifactId>
<version>${openimaj.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.openimaj</groupId>
<artifactId>faces</artifactId>
<version>${openimaj.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.openimaj</groupId>
<artifactId>image-feature-extraction</artifactId>
<version>${openimaj.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.openimaj</groupId>
<artifactId>core</artifactId>
<version>${openimaj.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.json</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.simple</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>openimaj-maven</id>
<url>http://maven.openimaj.org/</url>
</repository>
<repository>
<id>openimaj-snapshots</id>
<url>http://snapshots.openimaj.org/</url>
</repository>
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.org</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/deps</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>rhino.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.6</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>
</project>
60 changes: 60 additions & 0 deletions src/main/java/rhino/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package rhino;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.openimaj.image.DisplayUtilities;
import org.openimaj.image.FImage;
import org.openimaj.math.geometry.point.Point2d;
import org.openimaj.math.geometry.point.Point2dImpl;
import org.openimaj.time.Timer;
import org.openimaj.video.capture.VideoCapture;
import org.openimaj.video.processing.motion.GridMotionEstimator;
import org.openimaj.video.processing.motion.MotionEstimator;
import org.openimaj.video.processing.motion.MotionEstimatorAlgorithm;
import org.openimaj.video.translator.MBFImageToFImageVideoTranslator;

/**
* OpenIMAJ Hello world!
*
*/
public class App {
public static void main(String[] args) throws Exception {

int w = 320;
int h = 240;
if(args.length == 2){
w = Integer.parseInt(args[0]);
h = Integer.parseInt(args[1]);
}
final VideoCapture cap = new VideoCapture(w, h);
MotionEstimator e = new GridMotionEstimator(new MBFImageToFImageVideoTranslator(cap),
new MotionEstimatorAlgorithm.TEMPLATE_MATCH(),
30, 30,true);
System.out.println("Was given w,h: "+ e.getWidth() + "," + e.getHeight());
for (Iterator<FImage> iterator = e.iterator(); iterator.hasNext();) {
Timer t = Timer.timer();
FImage frame = iterator.next();
Point2dImpl meanMotion = new Point2dImpl(0,0);
Map<Point2d, Point2d> analysis = e.getMotionVectors();
for (Entry<Point2d, Point2d> line : analysis.entrySet()) {
Point2d to = line.getKey().copy();
to.translate(line.getValue());
frame.drawLine(line.getKey(), to, 1f);
meanMotion.x += line.getValue().getX();
}
DisplayUtilities.displayName(frame, "frame");
meanMotion.x /= analysis.size();
meanMotion.y /= analysis.size();
System.out.println("took: " + t.duration() + "ms, mean motion x: " + meanMotion.x);
// ImageUtilities.write(frame, new File("test" + i + ".png"));

try {
Thread.sleep(500);
} catch (final InterruptedException er) {
er.printStackTrace();
}
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/rhino/AppTypedResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package rhino;

import org.restlet.Application;
import org.restlet.resource.ServerResource;

public class AppTypedResource<T extends Application> extends ServerResource {
protected T app;

@SuppressWarnings("unchecked")
public AppTypedResource() {
this.app = (T) this.getApplication();
}
}
78 changes: 78 additions & 0 deletions src/main/java/rhino/EyeFaceApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package rhino;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openimaj.image.FImage;
import org.openimaj.image.ImageUtilities;
import org.openimaj.image.MBFImage;
import org.openimaj.image.processing.face.detection.DetectedFace;
import org.openimaj.image.processing.face.detection.HaarCascadeDetector;
import org.openimaj.image.processing.resize.ResizeProcessor;
import org.openimaj.math.geometry.shape.Rectangle;
import org.openimaj.util.function.Operation;
import org.openimaj.video.capture.VideoCaptureException;
import org.restlet.Restlet;
import org.restlet.resource.ClientResource;

import rhino.util.RestletUtil;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

public class EyeFaceApp extends EyeWatchingApp {

public EyeFaceApp() throws VideoCaptureException {
super();
}
private final class FaceOperation implements Operation<MBFImage> {
private HaarCascadeDetector det;
private ResizeProcessor resize;
private int width;
private int height;
public FaceOperation() {
this.width = Integer.parseInt(System.getProperty("eye.face.width", "160"));
this.height = Integer.parseInt(System.getProperty("eye.face.height", "120"));
int search = Integer.parseInt(System.getProperty("eye.face.search", "20"));
this.resize = new ResizeProcessor(width, height);
this.det = new HaarCascadeDetector(search);
}
@Override
public void perform(MBFImage frame) {
if(isActive){
float xRatio = frame.getWidth() / (float)width;
float yRatio = frame.getHeight() / (float)height;
FImage grey = null;
if(xRatio == 1 && yRatio == 1)
grey = frame.flatten();
else
grey = frame.process(resize).flatten();
List<DetectedFace> rects = det.detectFaces(grey);
Map<Object,Object> ret = new HashMap<Object, Object>();
ArrayList<Map<Object, Object>> faces = new ArrayList<Map<Object,Object>>();
ret.put("faces", faces);
for (DetectedFace detectedFace : rects) {
Map<Object,Object> face = new HashMap<Object,Object>();
Rectangle rec = detectedFace.getBounds();
face.put("x", rec.x * xRatio);
face.put("y", rec.y * yRatio);
face.put("width", rec.width * xRatio);
face.put("height", rec.height * yRatio);
logger.debug("Detected face: " +face);
}
}
}
}
@Override
public Operation<MBFImage> getOperation() {
return new FaceOperation();
}

}
52 changes: 52 additions & 0 deletions src/main/java/rhino/EyeImageApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package rhino;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.openimaj.image.ImageUtilities;
import org.openimaj.image.MBFImage;
import org.openimaj.util.stream.Stream;
import org.openimaj.video.capture.VideoCaptureException;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.representation.InputRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.routing.Router;

import rhino.camera.VideoStreamFactory;

public class EyeImageApp extends Application {
static Logger logger = Logger.getLogger(EyeImageApp.class);
public static class CurrentImage extends AppTypedResource<EyeImageApp>{
@Get
public Representation image() throws IOException{
logger.debug("Asking for next image from stream");
MBFImage image = this.app.videoStream.next();
logger.debug("Rendering image...");
ByteArrayOutputStream imageOutput = new ByteArrayOutputStream();
ImageUtilities.write(image, "jpeg", imageOutput);

InputRepresentation ret = new InputRepresentation(
new ByteArrayInputStream(imageOutput.toByteArray()),
MediaType.IMAGE_JPEG
);
return ret;
}
}
private Stream<MBFImage> videoStream;

public EyeImageApp() throws VideoCaptureException {
this.videoStream = VideoStreamFactory.getInstance().createStream();
}

@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/current", CurrentImage.class);
return router;
}
}
Loading

0 comments on commit 6ae60a4

Please sign in to comment.