001package org.openimaj.demos.sandbox.tldcpp.videotld;
002
003import java.io.File;
004import java.io.FileNotFoundException;
005import java.io.IOException;
006import java.io.PrintStream;
007
008import javax.swing.JFrame;
009import javax.swing.SwingUtilities;
010
011import org.openimaj.demos.sandbox.tldcpp.TLD;
012import org.openimaj.demos.sandbox.tldcpp.tracker.RectangleSelectionListener;
013import org.openimaj.image.MBFImage;
014import org.openimaj.math.geometry.shape.Rectangle;
015import org.openimaj.video.Video;
016import org.openimaj.video.VideoDisplay;
017import org.openimaj.video.VideoDisplay.Mode;
018import org.openimaj.video.capture.VideoCapture;
019
020public class TLDMain {
021        public Video<MBFImage> source;
022        TLD tld;
023//      Gui * gui;
024        boolean showOutput;
025        File printResults;
026        File saveDir;
027        double threshold = 0.5;
028        boolean showForeground = false;
029        boolean showNotConfident;
030        boolean selectManually;
031        Rectangle initialBB;
032        boolean reinit;
033        boolean exportModelAfterRun;
034        boolean loadModel;
035        File modelPath;
036        File modelExportFile;
037        int seed;
038        public PrintStream resultsFile;
039        public VideoDisplay<MBFImage> disp;
040        public RectangleSelectionListener selector;
041        public Command command;
042        boolean markerMode = false;
043        public TLDMain(Video<MBFImage> imageSource){
044                source = imageSource;
045                tld = new TLD(imageSource.getWidth(),imageSource.getHeight());
046        }
047        void doWork() throws FileNotFoundException {
048                if(printResults != null) {
049                        resultsFile = new PrintStream(printResults);
050                }
051                
052                // Start a video processor
053                selector = new RectangleSelectionListener(this);
054                disp = VideoDisplay.createVideoDisplay(source);
055                disp.addVideoListener(new TLDVideoListener(this));
056                
057                JFrame rootScreen = (JFrame) SwingUtilities.windowForComponent(disp.getScreen());
058                rootScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
059                rootScreen.addKeyListener(new TLDKeyListener(this));
060                disp.getScreen().addMouseListener(selector);
061                disp.getScreen().addMouseMotionListener(selector);
062        }
063        public void initiateObjectSelect() {
064                command = Command.SELECT;
065        }
066        public void selectObject(Rectangle r) throws Exception{
067                this.tld.selectObject(this.source.getCurrentFrame().flatten(),r);
068                this.disp.setMode(Mode.PLAY);
069                
070        }
071        
072        public static void main(String[] args) throws IOException {
073                TLDMain tldMain = new TLDMain(new VideoCapture(320,240));
074                TLDConfig.tldConfig(tldMain);
075                tldMain.doWork();
076        }
077        public enum Command{
078                NONE,CLEAR,LEARNING,ALTERNATING,SELECT;
079        }
080        public void queueClear() {
081                command = Command.CLEAR;
082        }
083        public void toggleLearning() {
084                command = Command.LEARNING;
085                
086        }
087        public void toggleAlternating() {
088                command = Command.ALTERNATING;
089                
090        }
091        public void toggleMarkerMode() {
092                this.markerMode  = !markerMode;
093        }
094}