-
Notifications
You must be signed in to change notification settings - Fork 0
/
mortonJ_sonJ_finalProject.m
104 lines (85 loc) · 3.05 KB
/
mortonJ_sonJ_finalProject.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
% Jonathan Morton and Jun Son
% Run this script to train your data
% Clear memory and console
close all
clear
clc
rootFolder = 'att_faces';
image_dims = [112, 92];
imageFolders = dir(rootFolder);
imageFolders = {imageFolders.name};
imageFolders = imageFolders(startsWith(imageFolders(:), 's'));
images = [];
subjectIds = {};
for i = 1:size(imageFolders, 2)
folder = imageFolders{i};
subjectPath = strcat(rootFolder, '/',folder);
images = dir(subjectPath);
images = {images.name};
images = images(or(endsWith(images(:), 'pgm'), endsWith(images(:), 'png')));
subjectId = folder;
subjectIds = unique([subjectIds subjectId]);
faces(i).id = subjectId;
idNumber = subjectId(2:end);
faces(i).idNumber = str2num(idNumber);
faces(i).pictures = {};
faces(i).vectors = [];
for j = 1:size(images, 2)
fullImagePath = strcat(subjectPath, '/', images{j});
image = imread(fullImagePath);
image = double(image);
imageIndex = images{j};
imageIndex = imageIndex(1:end-4);
imageIndex = str2num(imageIndex);
faces(i).pictures{imageIndex} = image;
% read and convert images to column vector
currentPicture = faces(i).pictures{imageIndex};
[irow icol] = size(currentPicture);
faceVector = reshape(currentPicture',irow*icol,1);
faces(i).vectors = [faces(i).vectors faceVector];
end
% Reconstructed average face
% avgFace = reshape(mean_face, 112, 92);
% figure; imshow(avgFace);
end
faceFields = fieldnames(faces);
faceCell = struct2cell(faces);
faceSize = size(faceCell);
% Convert to a matrix
faceCell = reshape(faceCell, faceSize(1), []); % Px(MxN)
% Make each field a column
faceCell = faceCell'; % (MxN)xP
% Sort by first field "idNumber"
faceCell = sortrows(faceCell, 2);
faceCell = reshape(faceCell', faceSize);
% Convert to Struct
faces = cell2struct(faceCell, faceFields, 1);
% split into training and testing data with 1:9 split ratio
% our faces are in test_data(:,n) where n = [71,74]
train_data = [];
test_data = [];
test_data_id = [];
train_data_id = [];
test_index = [1];
for i = 1:size(faces,2)
for j = 1:size(faces(i).vectors, 2)
faceVector = faces(i).vectors(:,j);
if ismember(j, test_index)
test_data = [test_data, faceVector];
test_data_id = [test_data_id getNumberId(faces(i).id)];
else
train_data = [train_data, faceVector];
train_data_id = [train_data_id getNumberId(faces(i).id)];
end
end
end
mean_face = mean(train_data, 2);
avgFace = reshape(mean_face, 92, 112);
figure; imshow(uint8(avgFace'));
Class_number = (size(faces, 2)); % Number of classes (or persons)
Class_population = size(faces(1).vectors,2) - size(test_index,2); % Number of images in each class
P = Class_population * Class_number; % Total number of training images
shifted_images = train_data - repmat(mean_face, 1, P);
run('face_recognition_training.m')
run('face_recognition_test.m')
run('face_recognition_evaluation.m')