more changes.
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTracking / Classifier.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 public class Classifier {
25
26   private ScanArea[] scanAreas;
27
28   // private float possibilityFace = 0f;
29   private float[] possibilities_FaceYes;
30   private float[] possibilities_FaceNo;
31   private int possibilityFaceYes = 0;
32   private int possibilityFaceNo = 0;
33
34   public static final long serialVersionUID = 5168971806943656945l;
35
36   public Classifier(int numScanAreas) {
37     this.scanAreas = new ScanArea[numScanAreas];
38     this.possibilities_FaceYes = new float[numScanAreas];
39     this.possibilities_FaceNo = new float[numScanAreas];
40   }
41
42   public void setScanArea(int idx, ScanArea area) {
43     scanAreas[idx] = area;
44   }
45
46   public void setPossibilitiesFaceYes(float[] arr) {
47     this.possibilities_FaceYes = arr;
48   }
49
50   public void setPossibilityFaceYes(int v) {
51     this.possibilityFaceYes = v;
52   }
53
54   public void setPossibilitiesFaceNo(float[] arr) {
55     this.possibilities_FaceNo = arr;
56   }
57
58   public void setPossibilityFaceNo(int v) {
59     this.possibilityFaceNo = v;
60   }
61
62   // public void learn(IntegralImageData image, boolean isFace) {
63   // long values[] = new long[this.scanAreas.length];
64   // //
65   // System.out.println("HERE:"+image.getIntegralAt(image.getDimension().width-1,
66   // // image.getDimension().height-1));
67   // // we assume the image is rectangular so we can simply use one side to
68   // // calculate
69   // // the scale factor
70   // float scaleFactor = image.getDimension().width / 100.0f;
71   //
72   // float avg = 0f;
73   // int avgItems = 0;
74   // for (int i = 0; i < this.scanAreas.length; ++i) {
75   // ScanArea scanArea = this.scanAreas[i];
76   // values[i] = 0l;
77   //
78   // values[i] += image.getIntegralAt(scanArea.getToX(scaleFactor),
79   // scanArea.getToY(scaleFactor));
80   // values[i] +=
81   // image.getIntegralAt(scanArea.getFromX(scaleFactor),
82   // scanArea.getFromY(scaleFactor));
83   //
84   // values[i] -=
85   // image.getIntegralAt(scanArea.getToX(scaleFactor),
86   // scanArea.getFromY(scaleFactor));
87   // values[i] -=
88   // image.getIntegralAt(scanArea.getFromX(scaleFactor),
89   // scanArea.getToY(scaleFactor));
90   //
91   // values[i] = (long) (values[i] / ((float) scanArea.getSize(scaleFactor)));
92   // avg = ((avgItems * avg) + values[i]) / (++avgItems);
93   // }
94   //
95   // if (isFace) {
96   // this.possibilityFaceYes++;
97   // } else {
98   // this.possibilityFaceNo++;
99   // }
100   // for (int i = 0; i < this.scanAreas.length; ++i) {
101   // boolean bright = (values[i] >= avg);
102   //
103   // if (isFace) {
104   // // here we change the possibility of P(Scanarea_N = (Bright | NotBright)
105   // // | Face=Yes)
106   // this.possibilities_FaceYes[i] =
107   // (((this.possibilityFaceYes - 1) * this.possibilities_FaceYes[i]) + (bright
108   // ? 0.999f
109   // : 0.001f)) / this.possibilityFaceYes;
110   // //
111   // System.out.println("P(Scannarea"+i+"=bright|Face=Yes) = "+this.possibilities_FaceYes[i]);
112   // //
113   // System.out.println("P(Scannarea"+i+"=dark|Face=Yes) = "+(1.0f-this.possibilities_FaceYes[i]));
114   // } else {
115   // // here we change the possibility of P(Scanarea_N = (Bright | NotBright)
116   // // | Face=No)
117   // this.possibilities_FaceNo[i] =
118   // (((this.possibilityFaceNo - 1) * this.possibilities_FaceNo[i]) + (bright ?
119   // 0.999f
120   // : 0.001f)) / this.possibilityFaceNo;
121   // //
122   // System.out.println("P(Scannarea"+i+"=bright|Face=No) = "+this.possibilities_FaceNo[i]);
123   // //
124   // System.out.println("P(Scannarea"+i+"=dark|Face=No) = "+(1.0f-this.possibilities_FaceNo[i]));
125   // }
126   //
127   // }
128   //
129   // // System.out.println("Average: "+avg);
130   // // System.out.println(this);
131   // }
132
133   /**
134    * Classifies an images region as face
135    * 
136    * @param image
137    * @param scaleFactor
138    *          please be aware of the fact that the scanareas are scaled for use
139    *          with 100x100 px images
140    * @param translationX
141    * @param translationY
142    * @return true if this region was classified as face, else false
143    */
144   public boolean classifyFace(IntegralImageData image, float scaleFactor, int translationX,
145       int translationY, float borderline) {
146
147     long values[] = new long[this.scanAreas.length];
148
149     float avg = 0f;
150     int avgItems = 0;
151     for (int i = 0; i < this.scanAreas.length; ++i) {
152       ScanArea scanArea = this.scanAreas[i];
153       // System.out.println("scanarea="+scanArea);
154       values[i] = 0l;
155
156       values[i] +=
157           image.getIntegralAt(translationX + scanArea.getToX(scaleFactor),
158               translationY + scanArea.getToY(scaleFactor));
159       values[i] +=
160           image.getIntegralAt(translationX + scanArea.getFromX(scaleFactor), translationY
161               + scanArea.getFromY(scaleFactor));
162
163       values[i] -=
164           image.getIntegralAt(translationX + scanArea.getToX(scaleFactor),
165               translationY + scanArea.getFromY(scaleFactor));
166       values[i] -=
167           image.getIntegralAt(translationX + scanArea.getFromX(scaleFactor), translationY
168               + scanArea.getToY(scaleFactor));
169
170       values[i] = (long) (values[i] / ((float) scanArea.getSize(scaleFactor)));
171       avg = ((avgItems * avg) + values[i]) / (++avgItems);
172     }
173 //     System.out.println("avg=" + avg);
174
175     // int amountYesNo = this.possibilityFaceNo + this.possibilityFaceYes;
176
177     // calculate the possibilites for face=yes and face=no with naive bayes
178     // P(Yes | M1 and ... and Mn) = P(Yes) * P(M1 | Yes) * ... * P(Mn | Yes) /xx
179     // P(No | M1 and ... and Mn) = P(No) * P(M1 | No) * ... * P(Mn | No) / xx
180     // as we just maximize the args we don't actually calculate the accurate
181     // possibility
182
183     float isFaceYes = 1.0f;// this.possibilityFaceYes / (float)amountYesNo;
184     float isFaceNo = 1.0f;// this.possibilityFaceNo / (float)amountYesNo;
185
186     for (int i = 0; i < this.scanAreas.length; ++i) {
187       boolean bright = (values[i] >= avg);
188       isFaceYes *= (bright ? this.possibilities_FaceYes[i] : 1 - this.possibilities_FaceYes[i]);
189       isFaceNo *= (bright ? this.possibilities_FaceNo[i] : 1 - this.possibilities_FaceNo[i]);
190     }
191 //    System.out.println("avg=" + avg + " yes=" + isFaceYes + " no=" + isFaceNo);
192
193     return (isFaceYes >= isFaceNo && (isFaceYes / (isFaceYes + isFaceNo)) > borderline);
194   }
195
196   public ScanArea[] getScanAreas() {
197     return this.scanAreas;
198   }
199
200   public int getLearnedFacesYes() {
201     return this.possibilityFaceYes;
202   }
203
204   public int getLearnedFacesNo() {
205     return this.possibilityFaceNo;
206   }
207
208   public float getPossibility(int scanAreaID, boolean faceYes, boolean bright) {
209     if (faceYes) {
210       return (bright ? this.possibilities_FaceYes[scanAreaID]
211           : 1 - this.possibilities_FaceYes[scanAreaID]);
212     } else {
213       return (bright ? this.possibilities_FaceNo[scanAreaID]
214           : 1 - this.possibilities_FaceNo[scanAreaID]);
215     }
216   }
217
218   public int compareTo(Classifier o) {
219     if (o.getScanAreas().length > this.getScanAreas().length) {
220       return -1;
221     } else if (o.getScanAreas().length < this.getScanAreas().length) {
222       return 1;
223     } else
224       return 0;
225   }
226
227   public String toString() {
228
229     String str = "";
230     for (int i = 0; i < scanAreas.length; i++) {
231       str += scanAreas[i].toString() + "\n";
232     }
233
234     return str;
235
236   }
237   // @Override
238   // public String toString() {
239   // StringBuilder sb = new StringBuilder();
240   // sb.append("Classifier [ScanAreas: " + this.scanAreas.length);
241   // int yesNo = this.possibilityFaceYes + this.possibilityFaceNo;
242   // sb.append(String.format("|Yes: %3.2f| No:%3.2f] (",
243   // (this.possibilityFaceYes / (float) yesNo) * 100.0f,
244   // (this.possibilityFaceNo / (float) yesNo) * 100.0f));
245   // for (int i = 0; i < this.scanAreas.length; ++i) {
246   // sb.append(String.format("[%3d|Yes: %3.2f| No: %3.2f], ", i + 1,
247   // (this.possibilities_FaceYes[i] * 100.0f), (this.possibilities_FaceNo[i] *
248   // 100.0f)));
249   // }
250   // sb.append(")");
251   //
252   // return sb.toString();
253   // }
254
255   /**
256    * Generates a new set of classifiers each with more ScanAreas than the last
257    * classifier. You can specifiy the amount of classifiers you want to generate
258    * 
259    * @param amount
260    *          amount of classifiers to create
261    * @param startAmountScanAreas
262    *          the start amount of scanAreas - if your first classifiers should
263    *          contain 3 items you should give 3 here
264    * @param incAmountScanAreas
265    *          the amount of which the scanAreas should increase - a simple 2
266    *          will increase them by 2 every step
267    * @return a List of classifiers
268    */
269   // public static List<Classifier> generateNewClassifiers(int amount, int
270   // startAmountScanAreas,
271   // float incAmountScanAreas) {
272   // List<Classifier> classifiers = new ArrayList<Classifier>();
273   //
274   // int maxDim = 40;
275   // Random random = new Random(System.currentTimeMillis());
276   // double maxSpace = 2 * Math.PI * Math.pow(50, 2);
277   //
278   // for (int i = 0; i < amount; ++i) {
279   // // we create an odd amount of ScanAreas starting with 1 (3, 5, 7, ...)
280   // int scanAreaAmount = startAmountScanAreas + (int)
281   // Math.pow(incAmountScanAreas, i);// +
282   // // ((i)*incAmountScanAreas+1);
283   //
284   // int scanAreaSize =
285   // randomInt(random, scanAreaAmount * 20, (int) Math.min(maxDim * maxDim,
286   // maxSpace))
287   // / scanAreaAmount;
288   // // System.out.println("scanAreaSize = "+scanAreaSize);
289   //
290   // List<ScanArea> scanAreas = new ArrayList<ScanArea>();
291   //
292   // for (int j = 0; j < scanAreaAmount; ++j) {
293   //
294   // int counter = 0;
295   // ScanArea scanArea = null;
296   // do {
297   // // new the width has the first choice
298   // int minWidth = (int) Math.ceil(scanAreaSize / (float) maxDim);
299   //
300   // int scanAreaWidth = randomInt(random, minWidth, Math.min(maxDim,
301   // scanAreaSize / 2));
302   // int scanAreaHeight = (int) Math.ceil(scanAreaSize / (float) scanAreaWidth);
303   //
304   // int radius =
305   // randomInt(random, 5, Math.min(50 - scanAreaHeight / 2, 50 - scanAreaWidth /
306   // 2));
307   // double angle = random.nextFloat() * 2 * Math.PI;
308   //
309   // int posX = (int) (50 + Math.cos(angle) * radius) - (scanAreaWidth / 2);
310   // int posY = (int) (50 + Math.sin(angle) * radius) - (scanAreaHeight / 2);
311   //
312   // // System.out.println("[Angle: "+(angle /
313   // // (Math.PI*2)*180)+" | radius: "+radius+"]");
314   // //
315   // System.out.println("Area"+j+" is "+posX+", "+posY+" ("+scanAreaWidth+" x "+scanAreaHeight+" = "+((scanAreaWidth*scanAreaHeight))+")");
316   //
317   // // now we get random position for this area
318   // scanArea = new ScanArea(posX, posY, scanAreaWidth, scanAreaHeight);
319   //
320   // counter++;
321   // } while (scanAreas.contains(scanArea) && counter < 30);
322   //
323   // if (counter == 30) {
324   // j -= 1;
325   // continue;
326   // }
327   //
328   // scanAreas.add(scanArea);
329   // }
330   //
331   // Classifier classifier = new Classifier(scanAreas.toArray(new ScanArea[0]));
332   // classifiers.add(classifier);
333   // }
334   //
335   // return classifiers;
336   // }
337
338   // private static int randomInt(Random random, int from, int to) {
339   // if (to - from <= 0)
340   // to = from + 1;
341   // return from + random.nextInt(to - from);
342   // }
343   //
344   // public static List<Classifier> getDefaultClassifier() {
345   // List<Classifier> classifier = new ArrayList<Classifier>();
346   //
347   // classifier.add(new Classifier(new ScanArea(30, 30, 30, 30), new
348   // ScanArea(15, 8, 15, 82),
349   // new ScanArea(75, 8, 15, 82)));
350   //
351   // return classifier;
352   // }
353
354 }