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