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