-
Notifications
You must be signed in to change notification settings - Fork 7
/
big_test.m
86 lines (69 loc) · 2.36 KB
/
big_test.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
%----------------------------------------------------------------
% File: big_test.m
%----------------------------------------------------------------
%
% Author: Marek Rychlik ([email protected])
% Date: Tue Dec 10 18:19:58 2024
% Copying: (C) Marek Rychlik, 2020. All rights reserved.
%
%----------------------------------------------------------------
% A test program for a large data (2500 images) facial recognition
location = fullfile('lfw');
disp('Creating image datastore...');
% We read images in their original format
imds0 = imageDatastore(location,...
'IncludeSubfolders',true,...
'LabelSource','foldernames');
load('big_model',["persons"])
[lia, locb] = ismember(imds0.Labels, persons);
idx = find(lia);
my_idx = randperm(numel(idx));
my_idx = my_idx(1:min(numel(idx),64));
idx = idx(my_idx);
imds = subset(imds0, idx);
imds.reset;
RGB = readall(imds);
Y = imds.Labels;
YPred = i_recognize_faces(RGB);
correct = Y == YPred;
% Burn labels into the images
RGBannotated = {};
for j=1:numel(RGB)
if correct(j)
color = 'yellow';
else
color = 'red';
end
RGBannotated{j} = insertObjectAnnotation(RGB{j}, ...
'rectangle', [10,10,100,20], ...
YPred(j),...
'AnnotationColor',color);
end
montage(RGBannotated);
Accuracy = numel(find(Y==YPred))/numel(Y);
disp(['Percentage of correctly labeled images: ', num2str(100*Accuracy),'%']);
function YPred = i_recognize_faces(RGB)
% RECOGNIZE_FACES - map images of faces to people names
% YPred = recognize_faces(RGB) accepts a cell array RGB of images, which
% should be RGB images. YPred returns a categorical array of image
% labels.
;
% Load precomputed model from a MAT file. For a format
% of the file, see the M-file main_fitcecoc.m.
load('big_model.mat');
num_images = numel(RGB,3);
% Get grayscale images of the desired size
Grayscale = cellfun(@(I)imresize(im2gray(I),targetSize),...
RGB, 'uni',false);
B = cat(3,Grayscale{:});
D = prod(targetSize);
B = reshape(B,D,[]);
% Normalizing data...';
B = single(B)./256;
B = (B - C) ./ SD;
% Extract features
W = U' * B;
% Predict faces
X = W';
YPred = predict(Mdl, X);
end