-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalcBestThresh.m
executable file
·89 lines (85 loc) · 3.51 KB
/
CalcBestThresh.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function [] = CalcBestThresh(x,y,winWidth,winLength,classifier)
PosImgSize = 50;
NegImgSize = 10;
totalSize = PosImgSize + NegImgSize;
threshPos = zeros(1,PosImgSize);
threshNeg = zeros(1,NegImgSize);
% open a weights mat file that contains current weights of Ada boost for
% each image
weightsFile = 'D:\computervision\faceDetect\vars\weights.mat';
adaWeights = open(weightsFile);
adaWeights = adaWeights.adaWeights;
% for every face image, calculate the haar feature (over 51000 haar
% features for a 19 x 19 window)
disp('Evaluating images using CalcBestThresh');
srcFiles = dir('training_faces\*.bmp'); % the folder in which ur images exists
for i=1:PosImgSize
filename = strcat('training_faces\',srcFiles(i).name);
img=imread(filename);
threshPos(i) = HaarFeatureCalc(img,x,y,winWidth,winLength,classifier);
end
% get mean and std of the face images to use as our classifier
posMean = mean(threshPos);
posStd = std(threshPos);
posMax = max(threshPos);
posMin = min(threshPos);
% for every non face image, calculate the haar feature
srcFiles = dir('training_nonfaces\*.jpg'); % the folder in which ur images exists
%changing to 10 negimgsize
for i=1:NegImgSize
filename = strcat('training_nonfaces\',srcFiles(i).name);
img=imread(filename);
threshNeg(i) = HaarFeatureCalc(img,x,y,winWidth,winLength,classifier);
end
% look for best threshold of points between mean and std of face images for
% each classifier
disp('Finished evaluating all images at specified classifier');
disp('Looking for best threshold, thresholds are:');
thresh = [threshPos threshNeg];
for j = 1:50
% calculate false positives ( WE REALLY DONT WANT THESE)
pos = 0;
% calculate false positives (NOT SO IMPORTANT, but must be below 50%)
neg = 0;
C = ones(1,totalSize);
% Increase the bandwitdh of our gaussian distribution to accept more
% recognition outside the mean to get a probability with almost 100%
% face detection with a low (65% or less) non face recognition and a
% total error lower than 50%. THIS IS A WEAK CLASSIFIER
for i = 1:PosImgSize
if (thresh(i) <=posMean +(abs(posMax -posMean)/50)*j && thresh(i) >= posMean -(abs(posMean -posMin)/50)*j)
pos = pos+1;
C(i) = 0;
end
end
for i = 1:NegImgSize
if (thresh(i) <=posMean +(abs(posMax -posMean)/50)*j && thresh(i) >= posMean -(abs(posMean -posMin)/50)*j)
else
neg = neg+1;
C(i+PosImgSize) = 0;
end
end
i = 1:totalSize;
totalErr = sum(adaWeights(i)*C(i)');
posErr = sum(adaWeights(1:PosImgSize)*C(1:PosImgSize)');
negErr = sum(adaWeights(PosImgSize+1:totalSize)*C(PosImgSize+1:totalSize)');
fprintf('Total Error: %e\n',totalErr);
fprintf('Positive Error: %e\n',posErr);
fprintf('Negative Error: %e\n',negErr);
if (posErr <=0.05)
if (totalErr<0.5)
disp('Found weak classifier');
fprintf('Total error is: %e\n',totalErr);
fprintf('False Negative error: %e\n',posErr);
fprintf('False positive error: %e\n',negErr);
% pass to adaboost to save our weak classifier and update our
% weights
AdaBoost(x,y,winWidth,winLength,classifier,posMean,posStd,j,totalErr,adaWeights,C,posMax,posMin,posErr,negErr);
break;
end
end
end
fprintf('Mean: %e\n',posMean);
fprintf('Std: %e\n',posStd);
fprintf('Max: %e\n',posMax);
fprintf('Min: %e\n',posMin);