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