-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattendance_system.m
233 lines (177 loc) · 7.4 KB
/
attendance_system.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
%% main
% Load the pre-trained VGGFace model
net = vgg16();
% Read and preprocess the input image
%classImage = imread('./dataset2/IMG_9263.jpg');
% classImage = imread('./dataset2/class2.jpg');
% classImage = imread('./dataset1/first.jpg');
classImage =imread('./d2/class1.jpg');
% classImage = imread('adipclass.jpeg');
% Detect faces in the input image
boundingBoxObject = detectFaces(classImage);
% Initialize a flag to check if any face was recognized in image
recognized = false;
%process referenceimages of our dataset
referenceImages = preprocessReferenceImages();
% Create or open the attendance file
attendanceFile = fopen('attendance.txt', 'w');
% Create a cell array to store names
maxFaces=30;
presentNames = {};
count_present_students = 1;
% Process each detected face
for i = 1:size(boundingBoxObject, 1)
% Extract the face region using imcrop
faceBoundingBox = boundingBoxObject(i, :);
face = imcrop(classImage, faceBoundingBox);
% filename = sprintf('face%d.jpg',i);
% % Save the cropped face image
% imwrite(face, filename);
%process the recognized face
recognizedFace = preprocessImage(face);
%find the person recognized
[recognizedPerson, maxSimilarity] = RecognizePerson(net, recognizedFace, referenceImages);
% Set a similarity threshold to determine if it's a match
similarityThreshold = 0.83;
% Check if the maximum similarity is above the threshold
if maxSimilarity >= similarityThreshold
disp(['Face ', num2str(i), ' Recognized as ' recognizedPerson]);
disp(maxSimilarity);
recognized = true;
% Write attendance to the text file
%fprintf(attendanceFile, '%s Present\n', recognizedPerson);
% Append the recognized name to the cell array
presentNames{count_present_students} = recognizedPerson;
count_present_students = count_present_students+1;
else
disp(['Face ', num2str(i), ' Not recognized as any person']);
end
end
if ~recognized
disp('No recognized faces in the image');
end
my_class_names = {'Safi','Sarah','Shayan', 'Anoosha', 'Areeb', 'Zainab Raza', 'Siqandar', 'Youshay', 'Zainab Haider', 'Mustufa', 'Murtuza'};
% Create or open the attendance file
attendanceFile = fopen('attendance.txt', 'w');
%Iterate over the names array
for i = 1:numel(my_class_names)
% Check if the name exists in presentNames
if ~ismember(my_class_names{i}, presentNames)
% Write the name and 'Absent' to the text file
fprintf(attendanceFile, '%s Absent\n', my_class_names{i});
else
fprintf(attendanceFile, '%s Present\n', my_class_names{i});
end
end
% Close the attendance file
fclose(attendanceFile);
%% process reference images
function processedReferenceImages = preprocessReferenceImages()
% Define a cell array of reference image file paths
referenceImagePaths = {
% './dataset2/test1.jpg',
% './dataset2/test2.jpg',
% './dataset2/test3.jpg',
% './dataset2/test4.jpg',
% 'test01.jpg'
'./d2/new1.jpg'%areeb
'./d2/n1.jpg'%shayan
'./d2/n2.jpg'%youshay
'./d2/new3.jpg'%safi
'./d2/new5.jpg' %zainab
'./d2/new7.jpg'%bushra
'./d2/new8.jpg'%sarah
'./d2/new9.jpg'%mustufa
'./d2/new10.jpg'%murtuza
'./d2/new11.jpg'%zainab raza
};
processedReferenceImages = cell(1, numel(referenceImagePaths));
for i = 1:numel(referenceImagePaths)
% Load the reference image
referenceImage = imread(referenceImagePaths{i});
%process the reference image
referenceImage = preprocessImage(referenceImage);
% Store the processed reference image in the cell array
processedReferenceImages{i} = referenceImage;
end
end
%% preprocess image
function resultImage = preprocessImage(image)
inputSize = [224, 224];
meanImageNetRGB = [123.68, 116.78, 103.94];
%resize image to inputSize of VGG model
image = imresize(image, inputSize);
%convert image to single precision
recognizedFace = single(image);
% Subtract the mean RGB value from each channel
% meanImageNetRGB value of VGG model
recognizedFace(:, :, 1) = recognizedFace(:, :, 1) - meanImageNetRGB(1);
recognizedFace(:, :, 2) = recognizedFace(:, :, 2) - meanImageNetRGB(2);
recognizedFace(:, :, 3) = recognizedFace(:, :, 3) - meanImageNetRGB(3);
% Rearrange the dimensions of the image to match the expected format
% [height, width, channels] of vgg
resultImage = permute(recognizedFace, [2, 1, 3]);
end
%% detect faces
function bbox = detectFaces(currentImage)
% Load the cascade object detector
faceDetection = vision.CascadeObjectDetector('ClassificationModel','FrontalFaceCART');
faceDetection.MergeThreshold = 10;
faceDetection.MinSize = [100, 100];
% Convert the image to grayscale for face detection
% grayImage = rgb2gray(currentImage);
% Detect faces
% bbox = step(faceDetection, currentImage);
bbox = faceDetection(currentImage);
% Define annotation properties
boxColor = [0, 255, 0];
% Annotate and display the image
labeledImage = currentImage; % Create a copy of the original image
for i = 1:size(bbox, 1)
% Draw a bounding box around the face with label
label = ['Face ' num2str(i)];
labeledImage = insertObjectAnnotation(labeledImage, 'rectangle', bbox(i, :), label, 'Color', boxColor, 'FontSize', 14, 'LineWidth',5);
end
figure, imshow(labeledImage);
end
%% face recognition
function [recognizedPerson, maxSimilarity] = RecognizePerson(net, recognizedFace, referenceImages)
% Initialize variables for similarity and recognized person
maxSimilarity = 0.8;
recognizedPerson = 'Unknown';
%extract features using VGG 16
embeddingRecognized = activations(net, recognizedFace, 'fc7', 'OutputAs', 'rows');
% Calculate similarity with reference faces and find the maximum
for j = 1:numel(referenceImages)
embeddingReference = activations(net, referenceImages{j}, 'fc7', 'OutputAs', 'rows');
%find cosine similarity
similarity = dot(embeddingRecognized, embeddingReference) / (norm(embeddingRecognized) * norm(embeddingReference));
if similarity >= maxSimilarity
maxSimilarity = similarity;
% Customize recognized person's name based on index
if(j==1)
recognizedPerson ='Areeb';
elseif(j==2)
recognizedPerson = 'Shayan';
elseif(j==3)
recognizedPerson = 'Youshay';
elseif(j==4)
recognizedPerson = 'Safi';
elseif(j==5)
recognizedPerson = 'Zainab';
elseif(j==6)
recognizedPerson = 'Bushra';
elseif(j==7)
recognizedPerson = 'Sarah';
elseif(j==8)
recognizedPerson = 'Mustufa';
elseif(j==9)
recognizedPerson = 'Murtuza';
elseif(j==10)
recognizedPerson = 'Zainab Raza';
else
recognizedPerson = ['Person ' num2str(j)];
end
end
end
end