ce997210b14abd7cd16dc9c3214c20258d91756d
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTracking / LEAImplementation.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  * No description given.
22  * 
23  * @author Florian Frankenberger
24  */
25 public class LEAImplementation {
26
27   private ClassifierTree classifierTree;
28
29   private Rectangle2D lastRectangle;
30
31   public LEAImplementation() {
32     this.loadFaceData();
33   }
34
35   public FaceAndEyePosition getEyePosition(Image image) {
36     if (image == null)
37       return null;
38
39     Rectangle2D faceRect = classifierTree.locateFaceRadial(image, lastRectangle);
40     System.out.println("FACE RECT=" + faceRect);
41     EyePosition eyePosition = null;
42     if (faceRect != null) {
43
44       lastRectangle = faceRect;
45       Point point = readEyes(image, faceRect);
46       if (point != null) {
47         eyePosition = new EyePosition(point, faceRect);
48       }
49     }
50
51     return new FaceAndEyePosition(faceRect, eyePosition);
52   }
53
54   private Point readEyes(Image image, Rectangle2D rect) {
55     EyeDetector ed = new EyeDetector(image, rect);
56     return ed.detectEye();
57   }
58
59   public boolean needsCalibration() {
60     return false;
61   }
62
63   /**
64    * This method loads the faceData from a file called facedata.dat which should
65    * be within the jar-file
66    */
67   private void loadFaceData() {
68
69     FileInputStream inputFile = new FileInputStream("facedata.dat");
70
71     classifierTree = new ClassifierTree();
72
73     int numClassifier = Integer.parseInt(inputFile.readLine());
74     System.out.println("numClassifier=" + numClassifier);
75     for (int c = 0; c < numClassifier; c++) {
76
77       int numArea = Integer.parseInt(inputFile.readLine());
78       Classifier classifier = new Classifier(numArea);
79       // parsing areas
80       for (int idx = 0; idx < numArea; idx++) {
81         // 54,54,91,62,296.0
82         Point fromPoint = new Point();
83         Point toPoint = new Point();
84         fromPoint.x = Integer.parseInt(inputFile.readLine());
85         fromPoint.y = Integer.parseInt(inputFile.readLine());
86         toPoint.x = Integer.parseInt(inputFile.readLine());
87         toPoint.y = Integer.parseInt(inputFile.readLine());
88         float size = Float.parseFloat(inputFile.readLine());
89         ScanArea area = new ScanArea(fromPoint, toPoint, size);
90         classifier.setScanArea(idx, area);
91       }
92
93       // parsing possibilities face yes
94       float array[] = new float[numArea];
95       for (int idx = 0; idx < numArea; idx++) {
96         array[idx] = Float.parseFloat(inputFile.readLine());
97       }
98       classifier.setPossibilitiesFaceYes(array);
99
100       // parsing possibilities face no
101       for (int idx = 0; idx < numArea; idx++) {
102         array[idx] = Float.parseFloat(inputFile.readLine());
103       }
104       classifier.setPossibilitiesFaceNo(array);
105       classifier.setPossibilityFaceYes(Integer.parseInt(inputFile.readLine()));
106       classifier.setPossibilityFaceNo(Integer.parseInt(inputFile.readLine()));
107
108       classifierTree.addClassifier(classifier);
109     }
110   }
111   // private Point readEyes(BufferedImage image, Rectangle2D rect) {
112   //
113   // // now we cluster the black image points and try to find the inner eye
114   // /*
115   // * BlackHoleDetector bhd = new BlackHoleDetector(image, rect);
116   // * bhd.detect(20);
117   // *
118   // * return bhd.getPosition();
119   // */
120   //
121   // EyeDetector ed = new EyeDetector(image, rect);
122   // return ed.detectEye();
123   // }
124
125 }