f73ef95b2aae42be59e2344f3e4edbaaaea9aeec
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTrackingInfer / 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
25
26 public class Classifier {
27
28   
29   private ScanArea[] scanAreas;
30
31   
32   private float[] possibilities_FaceYes;
33   
34   private float[] possibilities_FaceNo;
35   
36   private int possibilityFaceYes = 0;
37   
38   private int possibilityFaceNo = 0;
39
40   public Classifier(int numScanAreas) {
41     this.scanAreas = new ScanArea[numScanAreas];
42     this.possibilities_FaceYes = new float[numScanAreas];
43     this.possibilities_FaceNo = new float[numScanAreas];
44   }
45
46   public void setScanArea(int idx, ScanArea area) {
47     scanAreas[idx] = area;
48   }
49
50   public void setPossibilitiesFaceYes(@DELEGATE float[] arr) {
51     this.possibilities_FaceYes = arr;
52   }
53
54   public void setPossibilityFaceYes(int v) {
55     this.possibilityFaceYes = v;
56   }
57
58   public void setPossibilitiesFaceNo(@DELEGATE float[] arr) {
59     this.possibilities_FaceNo = arr;
60   }
61
62   public void setPossibilityFaceNo(int v) {
63     this.possibilityFaceNo = v;
64   }
65
66   /**
67    * Classifies an images region as face
68    * 
69    * @param image
70    * @param scaleFactor
71    *          please be aware of the fact that the scanareas are scaled for use
72    *          with 100x100 px images
73    * @param translationX
74    * @param translationY
75    * @return true if this region was classified as face, else false
76    */
77   
78   
79   public boolean classifyFace( IntegralImageData image,
80        float scaleFactor,  int translationX,
81        int translationY,  float borderline) {
82
83      long values[] = new long[scanAreas.length];
84
85      float avg = 0f;
86      int avgItems = 0;
87     for ( int i = 0; i < scanAreas.length; ++i) {
88       values[i] = 0l;
89
90       values[i] +=
91           image.getIntegralAt(translationX + scanAreas[i].getToX(scaleFactor), translationY
92               + scanAreas[i].getToY(scaleFactor));
93       values[i] +=
94           image.getIntegralAt(translationX + scanAreas[i].getFromX(scaleFactor), translationY
95               + scanAreas[i].getFromY(scaleFactor));
96
97       values[i] -=
98           image.getIntegralAt(translationX + scanAreas[i].getToX(scaleFactor), translationY
99               + scanAreas[i].getFromY(scaleFactor));
100       values[i] -=
101           image.getIntegralAt(translationX + scanAreas[i].getFromX(scaleFactor), translationY
102               + scanAreas[i].getToY(scaleFactor));
103
104       values[i] = (long) (values[i] / ((float) scanAreas[i].getSize(scaleFactor)));
105       avg = ((avgItems * avg) + values[i]) / (++avgItems);
106     }
107     // System.out.println("avg=" + avg);
108
109     // int amountYesNo = this.possibilityFaceNo + this.possibilityFaceYes;
110
111     // calculate the possibilites for face=yes and face=no with naive bayes
112     // P(Yes | M1 and ... and Mn) = P(Yes) * P(M1 | Yes) * ... * P(Mn | Yes) /xx
113     // P(No | M1 and ... and Mn) = P(No) * P(M1 | No) * ... * P(Mn | No) / xx
114     // as we just maximize the args we don't actually calculate the accurate
115     // possibility
116
117      float isFaceYes = 1.0f;// this.possibilityFaceYes /
118                                        // (float)amountYesNo;
119      float isFaceNo = 1.0f;// this.possibilityFaceNo /
120                                       // (float)amountYesNo;
121
122     for ( int i = 0; i < this.scanAreas.length; ++i) {
123        boolean bright = (values[i] >= avg);
124       isFaceYes *= (bright ? this.possibilities_FaceYes[i] : 1 - this.possibilities_FaceYes[i]);
125       isFaceNo *= (bright ? this.possibilities_FaceNo[i] : 1 - this.possibilities_FaceNo[i]);
126     }
127     // System.out.println("avg=" + avg + " yes=" + isFaceYes + " no=" +
128     // isFaceNo);
129
130     return (isFaceYes >= isFaceNo && (isFaceYes / (isFaceYes + isFaceNo)) > borderline);
131   }
132
133   public ScanArea[] getScanAreas() {
134     return this.scanAreas;
135   }
136
137   public int getLearnedFacesYes() {
138     return this.possibilityFaceYes;
139   }
140
141   public int getLearnedFacesNo() {
142     return this.possibilityFaceNo;
143   }
144
145   public float getPossibility(int scanAreaID, boolean faceYes, boolean bright) {
146     if (faceYes) {
147       return (bright ? this.possibilities_FaceYes[scanAreaID]
148           : 1 - this.possibilities_FaceYes[scanAreaID]);
149     } else {
150       return (bright ? this.possibilities_FaceNo[scanAreaID]
151           : 1 - this.possibilities_FaceNo[scanAreaID]);
152     }
153   }
154
155   public int compareTo(Classifier o) {
156     if (o.getScanAreas().length > this.getScanAreas().length) {
157       return -1;
158     } else if (o.getScanAreas().length < this.getScanAreas().length) {
159       return 1;
160     } else
161       return 0;
162   }
163
164   public String toString() {
165
166      String str = "";
167     for ( int i = 0; i < scanAreas.length; i++) {
168       str += scanAreas[i].toString() + "\n";
169     }
170
171     return str;
172
173   }
174
175 }