/
main.cpp
356 lines (299 loc) · 8.31 KB
/
main.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include "Dataman.h"
#include "bpnet.h"
using namespace std;
using namespace cv;
#define PATTERN_COUNT 30
#define PATTERN_SIZE 625
#define NETWORK_INPUTNEURONS 626
#define NETWORK_OUTPUT 3
#define HIDDEN_LAYERS 1
Mat detectAndDisplay( Mat frame );
String face_cascade_name = "haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
int main(int ac, char** av)
{
double t = (double)getTickCount();
int hl[] = {10};
int train;
if (ac > 1)
{
train = 1;
cout << "training" << endl;
}
Dataman* data = new Dataman((int)NETWORK_INPUTNEURONS,(int)NETWORK_OUTPUT,hl[0]);
float pattern[PATTERN_COUNT][PATTERN_SIZE];
float desiredout[PATTERN_COUNT][NETWORK_OUTPUT];
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade\n"); return -1; };
Mat frame, dface;
bpnet net;
int i,j;
float error;
net.create(PATTERN_SIZE,NETWORK_INPUTNEURONS,NETWORK_OUTPUT,hl,HIDDEN_LAYERS);
if (train == 1)
{
frame = imread("faces/s4/1.pgm", CV_LOAD_IMAGE_COLOR);
dface = detectAndDisplay( frame );
dface = dface > 128;
uchar* p;
for(int ia = 0; ia < dface.rows; ++ia)
{
p = dface.ptr<uchar>(ia);
for (int ja = 0; ja < dface.cols*dface.channels(); ++ja)
{
pattern[PATTERN_COUNT-1][ja+ia*25] = (int)p[ja]/255.0;
//~ for (int k = 0; k < NETWORK_OUTPUT; ++k)
//~ {
//~ desiredout[PATTERN_COUNT-1][k] = 0;
//~ }
//~ desiredout[PATTERN_COUNT-1][PATTERN_COUNT-1] = 1;
desiredout[PATTERN_COUNT-1][0] = 1;
desiredout[PATTERN_COUNT-1][1] = 1;
}
}
for (int iz = 0; iz < PATTERN_COUNT-1; iz++)
{
cout << "load gambar dr database " << iz+1 << endl;
char tfoto[32];
sprintf(tfoto,"faces/s4/1.pgm",iz+1);
frame = imread(tfoto, CV_LOAD_IMAGE_COLOR);
if( frame.empty() )
{
printf(" --(!) No captured frame -- Break!");
break;
}
//-- 3. Apply the classifier to the frame
dface = detectAndDisplay( frame );
dface = dface > 128;
uchar* p;
for(int ia = 0; ia < dface.rows; ++ia)
{
p = dface.ptr<uchar>(ia);
for (int ja = 0; ja < dface.cols*dface.channels(); ++ja)
{
pattern[iz][ja+ia*25] = (int)p[ja]/255.0;
}
}
desiredout[iz][0] = 0;
desiredout[iz][1] = 0;
//~ desiredout[iz][2] = 0;
//~ desiredout[iz][3] = 0;
int ks = iz;
if (ks > 8)
{
desiredout[iz][3] = 1;
ks = ks -8;
}
if (ks > 4)
{
desiredout[iz][2] = 1;
ks = ks -4;
}
if (ks > 2)
{
desiredout[iz][1] = 1;
ks = ks-2;
}
desiredout[iz][0] = ks;
int c = waitKey(10);
if( (char)c == 27 ) { break; } // escape
}
cout << "start training " << endl;
error = 1;
int isd = 0;
while(error > 0.001)
{
isd++;
cout << "ITER: " << isd << " " << error << endl;
error=0;
for(j=0;j<PATTERN_COUNT;j++)
{
error+=net.train(desiredout[j],pattern[j],0.2f,0.1f);
}
error/=PATTERN_COUNT;
int c = waitKey(10);
if( (char)c == 27 ) { break; } // escape
}
for(i=0;i<net.m_outputlayer.neuroncount;i++)
{
for(j=0;j<net.m_outputlayer.inputcount;j++)
{
data->out_weight[i][j] = net.m_outputlayer.neurons[i]->weights[j];
}
data->out_wgain[i] = net.m_outputlayer.neurons[i]->wgain;
}
for(i=0;i<net.m_hiddenlayers[HIDDEN_LAYERS-1]->neuroncount;i++)
{
for(j=0;j<net.m_hiddenlayers[HIDDEN_LAYERS-1]->inputcount;j++)
{
data->hid_weight[i][j] = net.m_hiddenlayers[HIDDEN_LAYERS-1]->neurons[i]->weights[j];
}
data->hid_wgain[i] = net.m_hiddenlayers[HIDDEN_LAYERS-1]->neurons[i]->wgain;
}
for(i=0;i<net.m_inputlayer.neuroncount;i++)
{
for(j=0;j<net.m_inputlayer.inputcount;j++)
{
data->in_weight[i][j] = net.m_inputlayer.neurons[i]->weights[j];
}
data->in_wgain[i] = net.m_inputlayer.neurons[i]->wgain;
}
data->write();
}
else
{
data->read();
for(i=0;i<net.m_outputlayer.neuroncount;i++)
{
for(j=0;j<net.m_outputlayer.inputcount;j++)
{
net.m_outputlayer.neurons[i]->weights[j]= data->out_weight[i][j];
}
net.m_outputlayer.neurons[i]->wgain = data->out_wgain[i];
}
for(i=0;i<net.m_hiddenlayers[HIDDEN_LAYERS-1]->neuroncount;i++)
{
for(j=0;j<net.m_hiddenlayers[HIDDEN_LAYERS-1]->inputcount;j++)
{
net.m_hiddenlayers[HIDDEN_LAYERS-1]->neurons[i]->weights[j]= data->hid_weight[i][j];
}
net.m_hiddenlayers[HIDDEN_LAYERS-1]->neurons[i]->wgain = data->hid_wgain[i];
}
for(i=0;i<net.m_inputlayer.neuroncount;i++)
{
for(j=0;j<net.m_inputlayer.inputcount;j++)
{
net.m_inputlayer.neurons[i]->weights[j]= data->in_weight[i][j];
}
net.m_inputlayer.neurons[i]->wgain = data->in_wgain[i];
}
for (int iz = 0; iz < PATTERN_COUNT-20; iz++)
{
char tfoto[32];
sprintf(tfoto,"faces/s1/%d.pgm",iz+1);
frame = imread(tfoto, CV_LOAD_IMAGE_COLOR);
if( frame.empty() )
{
printf(" --(!) No captured frame -- Break!\n");
break;
}
//-- 3. Apply the classifier to the frame
dface = detectAndDisplay( frame );
dface = dface > 128;
uchar* p;
int ss = 0;
for(int ia = 0; ia < dface.rows; ++ia)
{
p = dface.ptr<uchar>(ia);
for (int ja = 0; ja < dface.cols*dface.channels(); ++ja)
{
if (ja > 26 and ss == 0)
{
cout << "false detect: " << tfoto << endl;
ss = 1;
}
pattern[iz][ja+ia*25] = (int)p[ja]/255.0;
}
}
}
for (int iz = 0; iz < PATTERN_COUNT-20; iz++)
{
char tfoto[32];
sprintf(tfoto,"faces/s5/%d.pgm",iz+1);
frame = imread(tfoto, CV_LOAD_IMAGE_COLOR);
if( frame.empty() )
{
printf(" --(!) No captured frame -- Break!\n");
break;
}
//-- 3. Apply the classifier to the frame
dface = detectAndDisplay( frame );
dface = dface > 128;
uchar* p;
int ss = 0;
for(int ia = 0; ia < dface.rows; ++ia)
{
p = dface.ptr<uchar>(ia);
for (int ja = 0; ja < dface.cols*dface.channels(); ++ja)
{
if (ja > 26 and ss == 0)
{
cout << "false detect: " << tfoto << endl;
ss = 1;
}
pattern[iz+10][ja+ia*25] = (int)p[ja]/255.0;
}
}
int c = waitKey(10);
if( (char)c == 27 ) { break; } // escape
}
for (int iz = 0; iz < PATTERN_COUNT-20; iz++)
{
char tfoto[32];
sprintf(tfoto,"faces/s6/%d.pgm",iz+1);
frame = imread(tfoto, CV_LOAD_IMAGE_COLOR);
if( frame.empty() )
{
printf(" --(!) No captured frame -- Break!\n");
break;
}
//-- 3. Apply the classifier to the frame
dface = detectAndDisplay( frame );
dface = dface > 128;
uchar* p;
int ss = 0;
for(int ia = 0; ia < dface.rows; ++ia)
{
p = dface.ptr<uchar>(ia);
for (int ja = 0; ja < dface.cols*dface.channels(); ++ja)
{
if (ja > 26 and ss == 0)
{
cout << "false detect: " << tfoto << endl;
ss = 1;
}
pattern[iz+20][ja+ia*25] = (int)p[ja]/255.0;
}
}
int c = waitKey(10);
if( (char)c == 27 ) { break; } // escape
}
}
for(i=0;i<PATTERN_COUNT;i++)
{
net.propagate(pattern[i]);
//display result
cout << "TESTED PATTERN " << i+1 << " NET RESULT: "<< " " << net.getOutput().neurons[0]->output << " " << net.getOutput().neurons[1]->output << endl;
if ((i+1)%10 == 0) cout << endl;
//cout << "at ITER:" << isd << endl;
}
t = ((double)getTickCount() - t)/getTickFrequency();
cout << "Times passed in seconds: " << t << endl;
return 0;
}
Mat detectAndDisplay( Mat frame )
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
Mat ff(25, 25, CV_8UC3);
//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );
for ( size_t i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );
ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
Mat faceROI = frame_gray( faces[i] );
resize(faceROI, ff, ff.size(), 0, 0, INTER_CUBIC);
//imshow( "face_es", ff );
}
//-- Show what you got
//imshow( window_name, frame );
return ff;
}