2 * Copyright 2009 (c) Florian Frankenberger (darkblue.de)
4 * This file is part of LEA.
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
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
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/>.
26 public class Classifier {
29 private ScanArea[] scanAreas;
32 private float[] possibilities_FaceYes;
34 private float[] possibilities_FaceNo;
36 private int possibilityFaceYes = 0;
38 private int possibilityFaceNo = 0;
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];
46 public void setScanArea(int idx, ScanArea area) {
47 scanAreas[idx] = area;
50 public void setPossibilitiesFaceYes(@DELEGATE float[] arr) {
51 this.possibilities_FaceYes = arr;
54 public void setPossibilityFaceYes(int v) {
55 this.possibilityFaceYes = v;
58 public void setPossibilitiesFaceNo(@DELEGATE float[] arr) {
59 this.possibilities_FaceNo = arr;
62 public void setPossibilityFaceNo(int v) {
63 this.possibilityFaceNo = v;
67 * Classifies an images region as face
71 * please be aware of the fact that the scanareas are scaled for use
72 * with 100x100 px images
75 * @return true if this region was classified as face, else false
79 public boolean classifyFace( IntegralImageData image,
80 float scaleFactor, int translationX,
81 int translationY, float borderline) {
83 long values[] = new long[scanAreas.length];
87 for ( int i = 0; i < scanAreas.length; ++i) {
91 image.getIntegralAt(translationX + scanAreas[i].getToX(scaleFactor), translationY
92 + scanAreas[i].getToY(scaleFactor));
94 image.getIntegralAt(translationX + scanAreas[i].getFromX(scaleFactor), translationY
95 + scanAreas[i].getFromY(scaleFactor));
98 image.getIntegralAt(translationX + scanAreas[i].getToX(scaleFactor), translationY
99 + scanAreas[i].getFromY(scaleFactor));
101 image.getIntegralAt(translationX + scanAreas[i].getFromX(scaleFactor), translationY
102 + scanAreas[i].getToY(scaleFactor));
104 values[i] = (long) (values[i] / ((float) scanAreas[i].getSize(scaleFactor)));
105 avg = ((avgItems * avg) + values[i]) / (++avgItems);
107 // System.out.println("avg=" + avg);
109 // int amountYesNo = this.possibilityFaceNo + this.possibilityFaceYes;
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
117 float isFaceYes = 1.0f;// this.possibilityFaceYes /
118 // (float)amountYesNo;
119 float isFaceNo = 1.0f;// this.possibilityFaceNo /
120 // (float)amountYesNo;
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]);
127 // System.out.println("avg=" + avg + " yes=" + isFaceYes + " no=" +
130 return (isFaceYes >= isFaceNo && (isFaceYes / (isFaceYes + isFaceNo)) > borderline);
133 public ScanArea[] getScanAreas() {
134 return this.scanAreas;
137 public int getLearnedFacesYes() {
138 return this.possibilityFaceYes;
141 public int getLearnedFacesNo() {
142 return this.possibilityFaceNo;
145 public float getPossibility(int scanAreaID, boolean faceYes, boolean bright) {
147 return (bright ? this.possibilities_FaceYes[scanAreaID]
148 : 1 - this.possibilities_FaceYes[scanAreaID]);
150 return (bright ? this.possibilities_FaceNo[scanAreaID]
151 : 1 - this.possibilities_FaceNo[scanAreaID]);
155 public int compareTo(Classifier o) {
156 if (o.getScanAreas().length > this.getScanAreas().length) {
158 } else if (o.getScanAreas().length < this.getScanAreas().length) {
164 public String toString() {
167 for ( int i = 0; i < scanAreas.length; i++) {
168 str += scanAreas[i].toString() + "\n";