bug fixes + annotations
[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 @LATTICE("R<CT")
26 @METHODDEFAULT("OUT<THIS,THIS<IN,THISLOC=THIS,RETURNLOC=OUT")
27 public class LEAImplementation {
28
29   @LOC("CT")
30   private ClassifierTree classifierTree;
31
32   @LOC("R")
33   private Rectangle2D lastRectangle;
34
35   public LEAImplementation() {
36     this.loadFaceData();
37   }
38
39   @LATTICE("OUT<V,V<THIS,THIS<IN,THISLOC=THIS,RETURNLOC=OUT")
40   public FaceAndEyePosition getEyePosition(@LOC("IN") Image image) {
41     if (image == null)
42       return null;
43
44     @LOC("V") Rectangle2D faceRect = classifierTree.locateFaceRadial(image, lastRectangle);
45     @LOC("V") EyePosition eyePosition = null;
46     if (faceRect != null) {
47       lastRectangle = faceRect;
48       faceRect = null;
49       @LOC("V") Point point = readEyes(image, lastRectangle);
50       if (point != null) {
51         eyePosition = new EyePosition(point, lastRectangle);
52       }
53     }
54     System.out.println("eyePosition=" + eyePosition);
55
56     return new FaceAndEyePosition(lastRectangle, eyePosition);
57   }
58
59   private Point readEyes(@LOC("IN") Image image, @LOC("IN") Rectangle2D rect) {
60     @LOC("THIS") 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
70    * be within the 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 }