changes: now Inference engine works fine with the EyeTracking benchmark.
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTracking / LEA.java
1 import Benchmarks.SSJava.EyeTrackingInfer.EyePosition;
2
3 /*
4  * Copyright 2009 (c) Florian Frankenberger (darkblue.de)
5  * 
6  * This file is part of LEA.
7  * 
8  * LEA is free software: you can redistribute it and/or modify it under the
9  * terms of the GNU Lesser General Public License as published by the Free
10  * Software Foundation, either version 3 of the License, or (at your option) any
11  * later version.
12  * 
13  * LEA is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with LEA. If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 /**
23  * This is the main class of LEA.
24  * <p>
25  * It uses a face detection algorithm to find an a face within the provided image(s). Then it
26  * searches for the eye in a region where it most likely located and traces its position relative to
27  * the face and to the last known position. The movements are estimated by comparing more than one
28  * movement. If a movement is distinctly pointing to a direction it is recognized and all listeners
29  * get notified.
30  * <p>
31  * The notification is designed as observer pattern. You simply call
32  * <code>addEyeMovementListener(IEyeMovementListener)</code> to add an implementation of
33  * <code>IEyeMovementListener</code> to LEA. When a face is recognized/lost or whenever an eye
34  * movement is detected LEA will call the appropriate methods of the listener
35  * <p>
36  * LEA also needs an image source implementing the <code>ICaptureDevice</code>. One image source
37  * proxy to the <code>Java Media Framework</code> is included ( <code>JMFCaptureDevice</code>).
38  * <p>
39  * Example (for using LEA with <code>Java Media Framework</code>):
40  * <p>
41  * <code>
42  * LEA lea = new LEA(new JMFCaptureDevice(), true);
43  * </code>
44  * <p>
45  * This will start LEA with the first available JMF datasource with an extra status window showing
46  * if face/eye has been detected successfully. Please note that face detection needs about 2 seconds
47  * to find a face. After detection the following face detection is much faster.
48  * 
49  * @author Florian Frankenberger
50  */
51 @LATTICE("LAST<DEV,DEV<E,E<POS,POS<IMPL")
52 @METHODDEFAULT("OUT<THIS,THIS<IN,THISLOC=THIS,RETURNLOC=OUT")
53 public class LEA {
54
55   @LOC("IMPL")
56   private LEAImplementation implementation;
57   @LOC("DEV")
58   private DeviationScanner deviationScanner = new DeviationScanner();
59
60   public LEA() {
61     // this.imageProcessor = new
62     // ImageProcessor(this.captureDevice.getFrameRate());
63     implementation = new LEAImplementation();
64   }
65
66   /**
67    * Clears the internal movement buffer. If you just capture some of the eye movements you should
68    * call this every time you start recording the movements. Otherwise you may get notified for
69    * movements that took place BEFORE you started recording.
70    */
71   public void clear() {
72     // this.imageProcessor.clearDeviationScanner();
73   }
74
75   /**
76    * @METHOD To test LEA with the first capture device from the <code>Java Media Framework</code>
77    *         just start from here.
78    * 
79    * @param args
80    * @throws Exception
81    */
82   public static void main(String[] args) throws Exception {
83     LEA lea = new LEA();
84     lea.doRun();
85   }
86
87   @LATTICE("THIS<IMG,IMG<C,C*,THISLOC=THIS")
88   public void doRun() {
89
90     @LOC("C") int i = 0;
91
92     SSJAVA: while (true) {
93       @LOC("IMG") Image image = ImageReader.getImage();
94       if (image == null) {
95         break;
96       }
97       processImage(image);
98     }
99
100     System.out.println("Done.");
101
102   }
103
104   private void processImage(@LOC("IN") Image image) {
105     @LOC("THIS,LEA.POS") FaceAndEyePosition positions = implementation.getEyePosition(image);
106     deviationScanner.addEyePosition(positions.getEyePosition().getX(), positions.getEyePosition()
107         .getY());
108     @LOC("THIS,LEA.DEV,DeviationScanner.DEV") int deviation =
109         deviationScanner.scanForDeviation(positions.getFacePosition());// positions.getEyePosition().getDeviation(lastPositions.getEyePosition());
110     if (deviation != DeviationScanner.NONE) {
111       System.out.println("deviation=" + deviationScanner.toStringDeviation(deviation));
112       // notifyEyeMovementListenerEyeMoved(deviation);
113     }
114     // }
115   }
116
117 }