001package org.openimaj.demos.sandbox; 002 003/** 004 * Copyright (c) 2011, The University of Southampton and the individual contributors. 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without modification, 008 * are permitted provided that the following conditions are met: 009 * 010 * * Redistributions of source code must retain the above copyright notice, 011 * this list of conditions and the following disclaimer. 012 * 013 * * Redistributions in binary form must reproduce the above copyright notice, 014 * this list of conditions and the following disclaimer in the documentation 015 * and/or other materials provided with the distribution. 016 * 017 * * Neither the name of the University of Southampton nor the names of its 018 * contributors may be used to endorse or promote products derived from this 019 * software without specific prior written permission. 020 * 021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 022 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 023 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 024 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 025 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 026 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 027 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 028 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 029 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 031 */ 032import java.awt.event.KeyAdapter; 033import java.awt.event.KeyEvent; 034import java.io.DataInputStream; 035import java.io.FileInputStream; 036import java.io.FileNotFoundException; 037import java.io.IOException; 038import java.util.Map; 039import java.util.Map.Entry; 040 041import javax.swing.JFrame; 042 043import org.openimaj.image.DisplayUtilities; 044import org.openimaj.image.FImage; 045import org.openimaj.io.IOUtils; 046import org.openimaj.math.geometry.point.Point2d; 047import org.openimaj.math.geometry.point.Point2dImpl; 048import org.openimaj.ml.annotation.bayes.NaiveBayesAnnotator; 049import org.openimaj.video.Video; 050import org.openimaj.video.capture.VideoCapture; 051import org.openimaj.video.processing.motion.GridMotionEstimator; 052import org.openimaj.video.processing.motion.MotionEstimator; 053import org.openimaj.video.processing.motion.MotionEstimatorAlgorithm; 054import org.openimaj.video.translator.MBFImageToFImageVideoTranslator; 055 056public class OpticFlowPlayground { 057 public static Direction direction = Direction.NONE; 058 059 public static void main(String[] args) throws FileNotFoundException, IOException { 060 // Video<FImage> capture = new MBFImageToFImageVideoTranslator(new 061 // VideoCapture(160,120)); 062 final Video<FImage> capture = new MBFImageToFImageVideoTranslator(new VideoCapture(640, 480)); 063 final MotionEstimatorAlgorithm.TEMPLATE_MATCH alg = new MotionEstimatorAlgorithm.TEMPLATE_MATCH(); 064 final MotionEstimator e = new GridMotionEstimator(capture, 065 // new MotionEstimatorAlgorithm.PHASE_CORRELATION(), 066 alg, 067 30, 30, true); 068 final NaiveBayesAnnotator<Double, Direction> dirAnn = IOUtils.read(new DataInputStream(new FileInputStream( 069 "/Users/ss/.rhino/opticflowann"))); 070 071 boolean first = true; 072 for (final FImage fImage : e) { 073 if (first) { 074 first = false; 075 continue; 076 } 077 final Point2dImpl meanMotion = new Point2dImpl(0, 0); 078 final Map<Point2d, Point2d> analysis = e.getMotionVectors(); 079 for (final Entry<Point2d, Point2d> line : analysis.entrySet()) { 080 final Point2d to = line.getKey().copy(); 081 to.translate(line.getValue()); 082 fImage.drawLine(line.getKey(), to, 1f); 083 fImage.drawPoint(line.getKey(), 1f, 3); 084 meanMotion.x += line.getValue().getX(); 085 } 086 meanMotion.x /= analysis.size(); 087 meanMotion.y /= analysis.size(); 088 final JFrame f = DisplayUtilities.displayName(fImage, "frame"); 089 090 f.addKeyListener(new KeyAdapter() { 091 @Override 092 public void keyPressed(KeyEvent e) { 093 if (e.getKeyChar() == 'l') { 094 direction = Direction.LEFT; 095 } 096 else if (e.getKeyChar() == 'r') { 097 direction = Direction.RIGHT; 098 } 099 else if (e.getKeyCode() == KeyEvent.VK_SPACE) { 100 direction = Direction.MIDDLE; 101 } 102 } 103 104 @Override 105 public void keyReleased(KeyEvent e) { 106 direction = Direction.NONE; 107 } 108 }); 109 if (!(direction == Direction.NONE)) { 110 System.out.println(String.format("x: %2.2f,%s", meanMotion.x, direction)); 111 dirAnn.train(new DirectionScore(meanMotion.x, direction)); 112 } else { 113 // final Iterator<Direction> iterator = dirAnn.classify((double) 114 // meanMotion.x).getPredictedClasses() 115 // .iterator(); 116 // // if (iterator.hasNext()) { 117 // final Direction dir = iterator.next(); 118 // // System.out.println("Current flow: " + dir); 119 // } 120 } 121 } 122 } 123}