b3fda528ca22f3fbb7132eed91fb559a90b00939
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTrackingInfer / 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 image(s). Then it
24  * searches for the eye in a region where it most likely located and traces its position relative to
25  * the face and to the last known position. The movements are estimated by comparing more than one
26  * movement. If a movement is distinctly pointing to a direction it is recognized and all listeners
27  * get notified.
28  * <p>
29  * The notification is designed as observer pattern. You simply call
30  * <code>addEyeMovementListener(IEyeMovementListener)</code> to add an implementation of
31  * <code>IEyeMovementListener</code> to LEA. When a face is recognized/lost or whenever an eye
32  * movement is detected LEA will call the appropriate methods of the listener
33  * <p>
34  * LEA also needs an image source implementing the <code>ICaptureDevice</code>. One image source
35  * proxy to the <code>Java Media Framework</code> is included ( <code>JMFCaptureDevice</code>).
36  * <p>
37  * Example (for using LEA with <code>Java Media Framework</code>):
38  * <p>
39  * <code>
40  * LEA lea = new LEA(new JMFCaptureDevice(), true);
41  * </code>
42  * <p>
43  * This will start LEA with the first available JMF datasource with an extra status window showing
44  * if face/eye has been detected successfully. Please note that face detection needs about 2 seconds
45  * to find a face. After detection the following face detection is much faster.
46  * 
47  * @author Florian Frankenberger
48  */
49
50 public class LEA {
51
52   private LEAImplementation implementation;
53
54   private FaceAndEyePosition lastPositions = new FaceAndEyePosition(null, null);
55
56   private DeviationScanner deviationScanner = new DeviationScanner();
57
58   public LEA() {
59     // this.imageProcessor = new
60     // ImageProcessor(this.captureDevice.getFrameRate());
61     implementation = new LEAImplementation();
62   }
63
64   /**
65    * Clears the internal movement buffer. If you just capture some of the eye movements you should
66    * call this every time you start recording the movements. Otherwise you may get notified for
67    * movements that took place BEFORE you started recording.
68    */
69   public void clear() {
70     // this.imageProcessor.clearDeviationScanner();
71   }
72
73   /**
74    * @METHOD To test LEA with the first capture device from the <code>Java Media Framework</code>
75    *         just start from here.
76    * 
77    * @param args
78    * @throws Exception
79    */
80   public static void main(String[] args) throws Exception {
81     LEA lea = new LEA();
82     lea.doRun();
83   }
84
85   public void doRun() {
86
87     int i = 0;
88
89     SSJAVA: while (true) {
90       Image image = ImageReader.getImage();
91       if (image == null) {
92         break;
93       }
94       processImage(image);
95     }
96
97     System.out.println("Done.");
98
99   }
100
101   private void processImage(Image image) {
102     FaceAndEyePosition positions = implementation.getEyePosition(image);
103     // if (positions.getEyePosition() != null) {
104     deviationScanner.addEyePosition(positions.getEyePosition());
105     int deviation = deviationScanner.scanForDeviation(positions.getFacePosition());// positions.getEyePosition().getDeviation(lastPositions.getEyePosition());
106     if (deviation != DeviationScanner.NONE) {
107       System.out.println("deviation=" + deviationScanner.toStringDeviation(deviation));
108       // notifyEyeMovementListenerEyeMoved(deviation);
109     }
110     // }
111     lastPositions = positions;
112   }
113
114 }