-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.m
59 lines (45 loc) · 1.88 KB
/
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
% Load the pre-trained Yonatan model
load('model.mat', 'modelNet');
% Prompt the user to select an image file
[filename, pathname] = uigetfile({'*.jpg;*.png;*.bmp', 'Image files (*.jpg, *.png, *.bmp)'}, 'Select an image file');
% Check if the user selected a file
if isequal(filename, 0) || isequal(pathname, 0)
disp('User canceled the operation. Exiting...');
return;
end
% Construct the full path to the selected image
fullImagePath = fullfile(pathname, filename);
% Read the selected image
img = imread(fullImagePath);
% Detect faces in the image
faceDetector = vision.CascadeObjectDetector();
bbox = step(faceDetector, img);
% Check if any faces are detected
if ~isempty(bbox)
% Initialize cell array to store predicted labels for each face
predictedLabels = cell(size(bbox, 1), 1);
% Create a single figure for displaying all faces
figure;
imshow(img);
hold on;
% Process each detected face
for i = 1:size(bbox, 1)
% Crop and resize the detected face
face = imresize(imcrop(img, bbox(i, :)), [224, 224]);
% Classify the face using the trained model
predictedLabels{i} = char(classify(modelNet, face));
% Draw the bounding box around the detected face
rectangle('Position', bbox(i, :), 'EdgeColor', 'r', 'LineWidth', 2);
% Display the classification result for each face
text(bbox(i, 1), bbox(i, 2) - 10, ['Face ', num2str(i), ': ', predictedLabels{i}], 'Color', 'g', 'FontSize', 20);
end
hold off;
% Optionally, display a summary of all detected faces and their labels
disp('Summary of Detected Faces:');
for i = 1:length(predictedLabels)
disp(['Face ', num2str(i), ': ', predictedLabels{i}]);
end
else
% If no faces are detected, display a message
disp('No face detected in the selected image.');
end