-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestTogetherForever.m
357 lines (298 loc) · 11.7 KB
/
TestTogetherForever.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
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
357
%% Together Forever
% TestTogetherForever in order to mix both (TestHoGG & TestMoodPCA), this will recognize you
% from a new snap and identify your "mood" from that one.
% Uncomment to do this test apart
% clc
% clear
% close all
% name = []
%% WebCam setup
% This is commented because it was chosen at GUI_Menu
% 1 para la webCam normal
% 2 para la webCam de mi tablet
% CamType=1;
%% Name Verification
name;
if isempty(name) == 1
name = inputdlg('Introduzca su nombre por favor: ','Bienvenido');
end
%% See if its necessary to do new snaps
Snap_folder =0;
% Init mood labels
mood6 = {'Happy', 'Bored', 'Crazy', 'Angry', 'Sleepy', 'Sad'};
mood9 = {'Happy', 'Bored', 'Crazy', 'Angry', 'Sleepy', 'Sad', 'Smart' , 'Thankful' , 'Weird'};
answer1 = BoxMenu ('Menu', 'Do you want new snaps?');
if answer1 == 1
choice = questdlg('Number of New Photos?', ...
'New Snap Pool','6','9', 'fix');
% Handle response
switch choice
case '6'
disp([choice ' snaps chosen!'])
num = 6;
case '9'
disp([choice ' snaps chosen'])
num = 9;
end
rmdir('Together4Ever/BagOfSnaps/You','s');
mkdir('Together4Ever/BagOfSnaps/You');
Snap_folder = 'Together4Ever/BagOfSnaps/You';
if num == 6
Img.mood = mood6;
elseif num == 9
Img.mood = mood9;
end
for i=1:num
uiwait(msgbox({'Let`s' Img.mood{i} 'snap'}, 'Be Smart plz','warn','modal'));
[~, IFaces, bboxes] = Snapshot (CamType);
I = HOGFeatures(IFaces,bboxes);
I=ReSize(I);
imwrite(I,['Foto_', num2str(i),'.jpg']);
movefile(strcat('Foto_', num2str(i),'.jpg'),Snap_folder);
end
end
if answer1 == 2
rmdir('Together4Ever/BagOfSnaps/You','s');
mkdir('Together4Ever/BagOfSnaps/You');
% Select a folder with 6 or 9 photos to train them
uiwait(msgbox('Choose "Together4Ever/Training" folder PLZ', 'Be Smart plz','warn','modal'));
disp('Selección de Repositorio de tus fotos, sugerencia "Together4Ever/Training"');
folder_name = uigetdir('Together4Ever/Training','selecciona el repositorio');
Snap_folder = 'Together4Ever/BagOfSnaps/You';
end
% Se sale del programa
if answer1 == 0
GUI_Menu
return;
end
close all
%% Create the imageSet and copying
if answer1 == 2
MeDatabase = imageSet(folder_name,'recursive');
% Convertimos todas las fotos al mismo tamaño y creamos una copia en el
% directorio que se ha elegido
for i=1:MeDatabase.Count
% Lectura de la imagen
I = read(MeDatabase(1),i);
I = ReSize (I);
imwrite(I,['Foto_', num2str(i),'.jpg']);
movefile(strcat('Foto_', num2str(i),'.jpg'),Snap_folder);
end
end
%% Show a montage of the training photos
FaceDatabase = imageSet(Snap_folder, 'recursive');
figure;
montage(FaceDatabase.ImageLocation);
title('Images of YOU');
%% Sacamos las HOGFeatures para entrenarlas
TrainingDatabase = imageSet('Together4Ever/BagOfSnaps','recursive');
featureCount =1;
for i=1:size(TrainingDatabase,2) % Para contar los 41 Folders
for j = 1:TrainingDatabase(i).Count
trainingFeatures(featureCount,:) = extractHOGFeatures(read(TrainingDatabase(i),j));
trainingLabel{featureCount} = TrainingDatabase(i).Description;
featureCount = featureCount + 1;
end
personIndex{i} = TrainingDatabase(i).Description;
end
%% Creating class using fitcecoc
faceClassifier = fitcecoc(trainingFeatures,trainingLabel);
%% Start MoodPCA Part
dim= size(read(FaceDatabase(1),1),1); % 224 ; % image size
M=FaceDatabase.Count; %9 o 6; % number faces
if M == 6
Img.mood = mood6;
elseif M == 9
Img.mood = mood9;
end
for i=1:M
Img.data{i} = read(FaceDatabase(1),i); %Creating an structure with pixel data
end
save classFile Img;
load classFile;
% ScreenSize to plot
scrsz = get(0,'ScreenSize');
ancho = scrsz(3);
alto = scrsz (4);
ancho = round (ancho/3);
alto = round (alto/2)-40;
num=FaceDatabase.Count;
dim=size(read(FaceDatabase(1),1),1);
%% Create z vector with data info
if num==9
z= [ Img.data{1} Img.data{2} Img.data{3}; Img.data{4} Img.data{5} Img.data{6};...
Img.data{7} Img.data{8} Img.data{9}];
figure(1),imshow(z,'Initialmagnification','fit');title('Faces')
end
if num==6
z= [ Img.data{1} Img.data{2} Img.data{3}; Img.data{4} Img.data{5} Img.data{6}];
figure(1),imshow(z,'Initialmagnification','fit');title('Faces')
end
%% Compute Mean
averageImg=zeros(dim);
for i=1:num
Img.data{i} = im2single(Img.data{i});
averageImg = averageImg + (1/num)*Img.data{i};
end
% Uncommen to show the figure
%figure(2),imshow(averageImg,'Initialmagnification','fit');title('average')
%% Normalize (removing mean)
for i=1:num
Img.dataAvg{i} = Img.data{i} -averageImg;
end
if num == 9
z = [ Img.dataAvg{1} Img.dataAvg{2} Img.dataAvg{3} ; Img.dataAvg{4} Img.dataAvg{5} Img.dataAvg{6};...
Img.dataAvg{7} Img.dataAvg{8} Img.dataAvg{9}];
% Uncommen to show the figure
% figure(3),imshow(z,'Initialmagnification','fit');title('z average')
end
if num == 6
z = [ Img.dataAvg{1} Img.dataAvg{2} Img.dataAvg{3} ; Img.dataAvg{4} Img.dataAvg{5} Img.dataAvg{6}];
% Uncommen to show the figure
% figure(3),imshow(z,'Initialmagnification','fit');title('z average')
end
%% Calc Eigenvectors
% Cenerate A = [ img1(:) img2(:) ... imgM(:) ]; calculo de los autovectores
A = zeros(dim*dim,num);% (N*N)*num 2500*4
for i=1:num
A(:,i) = Img.dataAvg{i}(:);
end
% Covariance matrix small dimension (transposed)
Cov_mat = A'*A;
% Uncommen to show the figure
% figure(4),imagesc(Cov_mat);title('covariance')
%% Eigenvectros in small dimension
[ Eigvec_V, Eigval_D ] = eig(Cov_mat);% v num*num e num*num only diagonal 4 eigen values
% eigan face in large dimension A*Eigvec_V is eigen vector of Clarge
Large_V = A*Eigvec_V;% 2500*num*num*num =2500 *num
% reshape to eigen face
Eigenfaces=[];
for i=1:num
c_arr = Large_V(:,i);
Eigenfaces{i} = reshape(c_arr,dim,dim);
end
diag_eigval=diag(Eigval_D);
[xc,xci]=sort(diag_eigval,'descend');% largest eigenval
if num == 6
z = [ Eigenfaces{xci(1)} Eigenfaces{xci(2)} Eigenfaces{xci(3)} ; Eigenfaces{xci(4)} Eigenfaces{xci(5)} Eigenfaces{xci(6)}];
% Uncommen to show the figure
% figure(5),imshow(z,'Initialmagnification','fit');title('Eigenfaces')
end
if num == 9
z = [ Eigenfaces{xci(1)} Eigenfaces{xci(2)} Eigenfaces{xci(3)} ; Eigenfaces{xci(4)} Eigenfaces{xci(5)} Eigenfaces{xci(6)};...
Eigenfaces{xci(7)} Eigenfaces{xci(8)} Eigenfaces{xci(9)} ];
% Uncommen to show the figure
% figure(5),imshow(z,'Initialmagnification','fit');title('Eigenfaces')
end
%% Weights
nsel=M% select eigen faces
for img_num=1:num % image number
for i=1:nsel % eigen face for coeff number
img_weight(img_num,i) = sum(A(:,img_num).* Eigenfaces{xci(i)}(:)) ;
end
end
% Para el final
%% Classify new IMG
NotFinished = false;
while ~NotFinished
close (GUI_TogetherForever)
answer2 = BoxMenu ('Let´s trying to find Yourself','Let''s take a new picture to compare?');
switch answer2
case 1
[I, IFaces, bboxes] = Snapshot (CamType);
% Get HOGFeatures of the photo and cropp
[Icrop, hogFeature, visualization] = HOGFeatures (IFaces, bboxes);
testFace = Icrop;
case 2
uiwait(msgbox('Then, choose one to compare :)', 'He he he','warn','modal'));
[file, location] = uigetfile ('*jpg') ;
I = imread(strcat(location,file));
% Image proccesing
faceDetector = vision.CascadeObjectDetector;
bboxes = step(faceDetector, I);
IFaces = insertObjectAnnotation(I, 'rectangle', bboxes, 'Face');
% Get HOGFeatures of the photo and cropp
[Icrop, hogFeature, visualization] = HOGFeatures(IFaces,bboxes);
testFace = Icrop;
case 0
close all
uiwait(msgbox('Bye byeeeeee', 'Haleluyaaa','warn','modal'));
close (GUI_TogetherForever)
GUI_Menu
return;
end
%% EigenFaces
testFace = imresize(testFace,[dim dim]);
testFace = im2single(testFace);
% Uncommen to show the image
% figure(6), imshow(testFace,'Initialmagnification','fit'); title('Test Face')
Average_face = testFace(:)-averageImg(:); % normilized face
for(i=1:nsel)
face_weight(i) = sum(Average_face.* Eigenfaces{xci(i)}(:)) ;
end
% compute distance
for img_num=1:num
face_sumcur=0;
for(i=1:nsel)
face_sumcur = face_sumcur + (face_weight(i) -img_weight(img_num,i)).^2;
end
diffWeights(img_num) = sqrt( face_sumcur);
end
% Commen to show figures
close all
% Difference of EigenFaces
[val in]=min(diffWeights);
diffWeights = diffWeights.';
[min2, pos] = sort(diffWeights(:));
val2 = min2(2);
in2 = pos(2);
diff2 = val2 - val;
%% Probamos si funciona el clasificador
personLabel = predict (faceClassifier, hogFeature);
value = char(personLabel);
%% Load EmojiDatabase
EmojiDatabase = imageSet('Emojis','recursive');
for i=1:EmojiDatabase.Count
Img.emoji{i} = read(EmojiDatabase(1),i);
end
%% Print Results
% Uncommen to show results
% switch value
% case 'You'
% % Eigen Results
% if diff2 < 100
% figure('Position', [2*ancho 10 ancho alto]);
% imshow([Img.data{in},Img.data{in2}]);
% title(['Your mood is...' Img.mood{in} ' and ' Img.mood{in2}]);
% else
% figure('Position', [2*ancho 10 ancho alto]), imshow(Img.data{in}),title(['Your mood is...' Img.mood{in}]);
% end
% %HOGG Results
% h= msgbox(personLabel,'And the result is...');
% set(h, 'position', [100 440 400 100]); %makes box bigger
% ah = get( h, 'CurrentAxes' );
% ch = get( ah, 'Children' );
% set( ch, 'FontSize', 14 ); %makes text bigger
%
% case 'NotYou'
% % Eigen Results
% if diff2 < 100
% % Modificar
% figure('Position', [2*ancho 10 ancho alto]);
% imshow([Img.emoji{in},Img.emoji{in2}]);
% title(['Your mood is...' Img.mood{in} ' and ' Img.mood{in2}]);
% else
% figure('Position', [2*ancho 10 ancho alto]), imshow(Img.emoji{in}),title(['Your mood is...' Img.mood{in}]);
% end
% %HOGG Results
% h= msgbox(personLabel,'And the result is...');
% set(h, 'position', [100 440 400 100]); %makes box bigger
% ah = get( h, 'CurrentAxes' );
% ch = get( ah, 'Children' );
% set( ch, 'FontSize', 14 ); %makes text bigger
%
% end
% Show results with GUI
run GUI_TogetherForever.m
pause (10)
end