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