7db95e6af7b7ca7cec50d664502f2349f6c330f5
[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 DeviationScanner deviationScanner = new DeviationScanner();
55
56   public LEA() {
57     // this.imageProcessor = new
58     // ImageProcessor(this.captureDevice.getFrameRate());
59     implementation = new LEAImplementation();
60   }
61
62   /**
63    * Clears the internal movement buffer. If you just capture some of the eye movements you should
64    * call this every time you start recording the movements. Otherwise you may get notified for
65    * movements that took place BEFORE you started recording.
66    */
67   public void clear() {
68     // this.imageProcessor.clearDeviationScanner();
69   }
70
71   /**
72    * @METHOD To test LEA with the first capture device from the <code>Java Media Framework</code>
73    *         just start from here.
74    * 
75    * @param args
76    * @throws Exception
77    */
78   public static void main(String[] args) throws Exception {
79     LEA lea = new LEA();
80     lea.doRun();
81   }
82
83   public void doRun() {
84
85     int i = 0;
86
87     SSJAVA: while (true) {
88       Image image = ImageReader.getImage();
89       if (image == null) {
90         break;
91       }
92       processImage(image);
93     }
94
95     System.out.println("Done.");
96
97   }
98
99   private void processImage(Image image) {
100     FaceAndEyePosition positions = implementation.getEyePosition(image);
101     EyePosition eye = positions.getEyePosition();
102     deviationScanner.addEyePosition(eye.getX(),eye.getY());
103     Rectangle2D face = positions.getFacePosition();
104     int deviation = deviationScanner.scanForDeviation(face);// positions.getEyePosition().getDeviation(lastPositions.getEyePosition());
105     if (deviation != DeviationScanner.NONE) {
106       System.out.println("deviation=" + deviationScanner.toStringDeviation(deviation));
107     }
108   }
109
110 }