add source code that does not have location annotations.
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTracking / ClassifierTree.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  * 
22  * @author Florian
23  */
24 @LATTICE("CS<C,C*")
25 @METHODDEFAULT("OUT<THIS,THIS<IN,THISLOC=THIS,RETURNLOC=OUT")
26 public class ClassifierTree {
27
28   @LOC("CS")
29   private Classifier classifiers[];
30
31   public ClassifierTree(int size) {
32     classifiers = new Classifier[size];
33   }
34
35   public void addClassifier(@LOC("IN") int idx, @LOC("IN") Classifier c) {
36     classifiers[idx] = c;
37   }
38
39   /**
40    * Locates a face by searching radial starting at the last known position. If
41    * lastCoordinates are null we simply start in the center of the image.
42    * <p>
43    * TODO: This method could quite possible be tweaked so that face recognition
44    * would be much faster
45    * 
46    * @param image
47    *          the image to process
48    * @param lastCoordinates
49    *          the last known coordinates or null if unknown
50    * @return an rectangle representing the actual face position on success or
51    *         null if no face could be detected
52    */
53   @LATTICE("OUT<CXY,CXY<THIS,THIS<V,V<IMG,IMG<C,C<IN,C*,V*,FACTOR*,CXY*,THISLOC=THIS,RETURNLOC=OUT,GLOBALLOC=IN")
54   public Rectangle2D locateFaceRadial(@LOC("IN") Image smallImage,
55       @LOC("THIS,ClassifierTree.C") Rectangle2D lastCoordinates) {
56
57     @LOC("IMG") IntegralImageData imageData = new IntegralImageData(smallImage);
58     @LOC("IN") float originalImageFactor = 1;
59
60     if (lastCoordinates == null) {
61       // if we don't have a last coordinate we just begin in the center
62       @LOC("THIS,ClassifierTree.C") int smallImageMaxDimension =
63           Math.min(smallImage.getWidth(), smallImage.getHeight());
64       lastCoordinates =
65           new Rectangle2D((smallImage.getWidth() - smallImageMaxDimension) / 2.0,
66               (smallImage.getHeight() - smallImageMaxDimension) / 2.0, smallImageMaxDimension,
67               smallImageMaxDimension);
68       // System.out.println("lastCoordinates=" + lastCoordinates);
69     } else {
70       // first we have to scale the last coodinates back relative to the resized
71       // image
72       lastCoordinates =
73           new Rectangle2D((lastCoordinates.getX() * (1 / originalImageFactor)),
74               (lastCoordinates.getY() * (1 / originalImageFactor)),
75               (lastCoordinates.getWidth() * (1 / originalImageFactor)),
76               (lastCoordinates.getHeight() * (1 / originalImageFactor)));
77     }
78
79     @LOC("THIS,ClassifierTree.C") float startFactor = (float) (lastCoordinates.getWidth() / 100.0f);
80
81     // first we calculate the maximum scale factor for our 200x200 image
82     @LOC("THIS,ClassifierTree.C") float maxScaleFactor =
83         Math.min(imageData.getWidth() / 100f, imageData.getHeight() / 100f);
84     // maxScaleFactor = 1.0f;
85
86     // we simply won't recognize faces that are smaller than 40x40 px
87     @LOC("THIS,ClassifierTree.C") float minScaleFactor = 0.5f;
88
89     @LOC("THIS,ClassifierTree.C") float maxScaleDifference =
90         Math.max(Math.abs(maxScaleFactor - startFactor), Math.abs(minScaleFactor - startFactor));
91
92     // border for faceYes-possibility must be greater that that
93     @LOC("THIS,ClassifierTree.C") float maxBorder = 0.999f;
94
95     @LOC("THIS,ClassifierTree.C") int startPosX = (int) lastCoordinates.getX();
96     @LOC("THIS,ClassifierTree.C") int startPosY = (int) lastCoordinates.getX();
97
98     @LOC("THIS,ClassifierTree.C") int loopidx = 0;
99     TERMINATE: for (@LOC("THIS,ClassifierTree.C") float factorDiff = 0.0f; Math.abs(factorDiff) <= maxScaleDifference; factorDiff =
100         (factorDiff + sgn(factorDiff) * 0.1f) * -1 // we alternate between
101                                                    // negative and positiv
102                                                    // factors
103     ) {
104
105       if (++loopidx > 1000) {
106         return null;
107       }
108
109       @LOC("THIS,ClassifierTree.C") float factor = startFactor + factorDiff;
110       if (factor > maxScaleFactor || factor < minScaleFactor)
111         continue;
112
113       // now we calculate the actualDimmension
114       @LOC("THIS,ClassifierTree.C") int actualDimmension = (int) (100 * factor);
115       @LOC("THIS,ClassifierTree.C") int maxX = imageData.getWidth() - actualDimmension;
116       @LOC("THIS,ClassifierTree.C") int maxY = imageData.getHeight() - actualDimmension;
117
118       @LOC("THIS,ClassifierTree.C") int maxDiffX = Math.max(Math.abs(startPosX - maxX), startPosX);
119       @LOC("THIS,ClassifierTree.C") int maxDiffY = Math.max(Math.abs(startPosY - maxY), startPosY);
120
121       @LOC("CXY") int xidx = 0;
122       TERMINATE: for (@LOC("CXY") float xDiff = 0.1f; Math.abs(xDiff) <= maxDiffX; xDiff =
123           (xDiff + sgn(xDiff) * 0.5f) * -1) {
124
125         if (++xidx > 1000) {
126           return null;
127         }
128
129         @LOC("CXY") int xPos = Math.round((float) (startPosX + xDiff));
130
131         if (xPos < 0 || xPos > maxX)
132           continue;
133
134         @LOC("CXY") int yidx = 0;
135         // yLines:
136         TERMINATE: for (@LOC("CXY") float yDiff = 0.1f; Math.abs(yDiff) <= maxDiffY; yDiff =
137             (yDiff + sgn(yDiff) * 0.5f) * -1) {
138
139           if (++yidx > 1000) {
140             return null;
141           }
142
143           @LOC("CXY") int yPos = Math.round(startPosY + yDiff);
144           if (yPos < 0 || yPos > maxY)
145             continue;
146
147           // by now we should have a valid coordinate to process which we should
148           // do now
149           @LOC("CXY") boolean backToYLines = false;
150           for (@LOC("CXY") int idx = 0; idx < classifiers.length; ++idx) {
151             @LOC("CXY") float borderline =
152                 0.8f + (idx / (classifiers.length - 1)) * (maxBorder - 0.8f);
153             if (!classifiers[idx].classifyFace(imageData, factor, xPos, yPos, borderline)) {
154               backToYLines = true;
155               break;
156               // continue yLines;
157             }
158           }
159
160           // if we reach here we have a face recognized because our image went
161           // through all
162           // classifiers
163
164           if (backToYLines) {
165             continue;
166           }
167           @LOC("OUT") Rectangle2D faceRect =
168               new Rectangle2D(xPos * originalImageFactor, yPos * originalImageFactor,
169                   actualDimmension * originalImageFactor, actualDimmension * originalImageFactor);
170
171           return faceRect;
172
173         }
174
175       }
176
177     }
178
179     // System.out.println("Time: "+(System.currentTimeMillis()-timeStart)+"ms");
180     return null;
181
182   }
183
184   @LATTICE("OUT<IN,OUT<THIS,THISLOC=THIS,RETURNLOC=OUT")
185   private static int sgn(@LOC("IN") float value) {
186     return (value < 0 ? -1 : (value > 0 ? +1 : 1));
187   }
188
189 }