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