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