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/>.
25 public class Classifier {
27 private ScanArea[] scanAreas;
29 private float[] possibilities_FaceYes;
31 private float[] possibilities_FaceNo;
33 private int possibilityFaceYes = 0;
35 private int possibilityFaceNo = 0;
37 public Classifier(int numScanAreas) {
38 this.scanAreas = new ScanArea[numScanAreas];
39 this.possibilities_FaceYes = new float[numScanAreas];
40 this.possibilities_FaceNo = new float[numScanAreas];
43 public void setScanArea(int idx, ScanArea area) {
44 scanAreas[idx] = area;
47 public void setPossibilitiesFaceYes(@DELEGATE float[] arr) {
48 this.possibilities_FaceYes = arr;
51 public void setPossibilityFaceYes(int v) {
52 this.possibilityFaceYes = v;
55 public void setPossibilitiesFaceNo(@DELEGATE float[] arr) {
56 this.possibilities_FaceNo = arr;
59 public void setPossibilityFaceNo(int v) {
60 this.possibilityFaceNo = v;
64 * Classifies an images region as face
68 * please be aware of the fact that the scanareas are scaled for use with 100x100 px
72 * @return true if this region was classified as face, else false
75 public boolean classifyFace(IntegralImageData image, float scaleFactor, int translationX, int translationY, float borderline) {
77 long[] values = new long[scanAreas.length];
81 for (int i = 0; i < scanAreas.length; ++i) {
84 values[i] += image.getIntegralAt(translationX + scanAreas[i].getToX(scaleFactor), translationY + scanAreas[i].getToY(scaleFactor));
85 values[i] += image.getIntegralAt(translationX + scanAreas[i].getFromX(scaleFactor), translationY + scanAreas[i].getFromY(scaleFactor));
87 values[i] -= image.getIntegralAt(translationX + scanAreas[i].getToX(scaleFactor), translationY + scanAreas[i].getFromY(scaleFactor));
88 values[i] -= image.getIntegralAt(translationX + scanAreas[i].getFromX(scaleFactor), translationY + scanAreas[i].getToY(scaleFactor));
90 values[i] = (long) (values[i] / ((float) scanAreas[i].getSize(scaleFactor)));
91 avg = ((avgItems * avg) + values[i]) / (++avgItems);
93 // System.out.println("avg=" + avg);
95 // int amountYesNo = this.possibilityFaceNo + this.possibilityFaceYes;
97 // calculate the possibilites for face=yes and face=no with naive bayes
98 // P(Yes | M1 and ... and Mn) = P(Yes) * P(M1 | Yes) * ... * P(Mn | Yes) /xx
99 // P(No | M1 and ... and Mn) = P(No) * P(M1 | No) * ... * P(Mn | No) / xx
100 // as we just maximize the args we don't actually calculate the accurate
103 float isFaceYes = 1.0f;// this.possibilityFaceYes /
104 // (float)amountYesNo;
105 float isFaceNo = 1.0f;// this.possibilityFaceNo /
106 // (float)amountYesNo;
108 for (int i = 0; i < this.scanAreas.length; ++i) {
109 boolean bright = (values[i] >= avg);
110 isFaceYes *= (bright ? this.possibilities_FaceYes[i] : 1 - this.possibilities_FaceYes[i]);
111 isFaceNo *= (bright ? this.possibilities_FaceNo[i] : 1 - this.possibilities_FaceNo[i]);
113 // System.out.println("avg=" + avg + " yes=" + isFaceYes + " no=" +
116 return (isFaceYes >= isFaceNo && (isFaceYes / (isFaceYes + isFaceNo)) > borderline);
119 public ScanArea[] getScanAreas() {
120 return this.scanAreas;
123 public int getLearnedFacesYes() {
124 return this.possibilityFaceYes;
127 public int getLearnedFacesNo() {
128 return this.possibilityFaceNo;
131 public float getPossibility(int scanAreaID, boolean faceYes, boolean bright) {
133 return (bright ? this.possibilities_FaceYes[scanAreaID]
134 : 1 - this.possibilities_FaceYes[scanAreaID]);
136 return (bright ? this.possibilities_FaceNo[scanAreaID]
137 : 1 - this.possibilities_FaceNo[scanAreaID]);
141 public int compareTo(Classifier o) {
142 if (o.getScanAreas().length > this.getScanAreas().length) {
144 } else if (o.getScanAreas().length < this.getScanAreas().length) {
150 public String toString() {
153 for (int i = 0; i < scanAreas.length; i++) {
154 str += scanAreas[i].toString() + "\n";