I'm using MATLAB 2017b, my face recognition code doesn't work, anyone can help to solve?
Show older comments
% FACE DETECTION:
clear ALL
clc
%Detect objects using Viola-Jones Algorithm
%To detect Face
FDetect = vision.CascadeObjectDetector;
%Read the input image
I = imread('/Users/paulling95/Desktop/Left Frames/frame25.jpg');
%Returns Bounding Box values based on number of objects
BB = step(FDetect,I);
figure,
imshow(I);
hold on
k=1;
for i = 1:size(BB,1) % draw rectanlge in row wise
rectangle('Position',BB(i,:),'LineWidth',2,'LineStyle','-','EdgeColor','g');
%start from first row then second and ....
end
% title('Face Detection');
hold off;
for i= 1:size(BB,1)
J= imcrop(I,BB(i,:));
p2=strcat('cropped faces/', int2str(k),'.jpg');
I2 = rgb2gray(J);
I2= imresize(I2, [200 150]);
k=k+1;
imwrite(I2,p2)
end
% Face Recognition
TrainDatabasePath= '/Users/paulling95/Desktop/Demo/face database';
TestDatabasePath= '/Users/paulling95/Desktop/Demo/Cropped faces';
srfile=dir('cropped faces/*.jpg');
for i=1:length(srfile)
prompt=strcat('cropped faces/',srfile(i).name);
im = imread(prompt);
T = CreateDatabase(TrainDatabasePath);
[m, A, Eigenfaces] = EigenfaceCore(T);
% S={'20.jpg'};
OutputName= Recognition(prompt, m, A, Eigenfaces);
SelectedImage = strcat(TrainDatabasePath,'\',OutputName);
SelectedImage = imread(SelectedImage);
figure,
subplot(2,2,1)
imshow(im)
title('Test Image');
% figure,
subplot(2,2,2);
imshow(SelectedImage)
title('Equivalent Image');
str = strcat('Matched image is : ',OutputName);
disp(str)
end
21 Comments
KSSV
on 2 May 2019
In URGENT need......we are not here for your urgency/ emergency.
my face recognition code doesn't work, why it doesn't work any error? Wrong result? What made you tell so...give complete problem. What is your input?
Paul Ling
on 2 May 2019
Sindhuja Chaduvula
on 6 Jun 2021
TrainDatabasePath = '/\Users\prasanth\Downloads\MATLAB code 3\database2\';
TestDatabasePath = '/\Users\prasanth\Downloads\MATLAB code 3\TestDatabase\';
%%%%%%%%%%%%%%%%%%%%%%%%%%% No. of test images
TrainFiles = dir('/\Users\prasanth\Downloads\MATLAB code 3\Testdatabase\');
Train_Number = 0;
for i = 1:size(TrainFiles,1)
if not(strcmp(TrainFiles(i).name,'.')|strcmp(TrainFiles(i).name,'..')|strcmp(TrainFiles(i).name,'Thumbs.db'))
Train_Number = Train_Number + 1; % Number of all images in the training database
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
v=Train_Number-1;
for j = 1:v
TestImage = num2str(j);
% display(TestImage);
s=strcat('a',TestImage);
% display(s);
TestImage = strcat(TestDatabasePath,char(TestImage),'.jpg');
im=imread(TestImage);
% display(TestImage);
T = CreateDatabase(TrainDatabasePath); % T is the 1D matrix of the traindatabase
[m, A, Eigenfaces] = EigenfaceCore(T);
[OutputName,Recognized_index] = Recognition(TestImage, m, A, Eigenfaces);
SelectedImage = strcat(TrainDatabasePath,'\', OutputName);
SelectedImage = imread(SelectedImage);
% axes(eval(['handles.axes', num2str(s)]));
imshow(SelectedImage);
str = strcat('Matched image is : ',OutputName);
disp(str)
end
Sindhuja Chaduvula
on 6 Jun 2021
Im not getting an output for this code in my mac cant u tell me wats wrong in it
Walter Roberson
on 6 Jun 2021
\ is not a directory separator except on Windows.
TrainDatabasePath = '/Users/prasanth/Downloads/MATLAB code 3/database2';
TestDatabasePath = '/Users/prasanth/Downloads/MATLAB code 3/TestDatabase';
%%%%%%%%%%%%%%%%%%%%%%%%%%% No. of test images
TestFiles = dir(TestDatabasePath);
Test_Number = 0;
TestFiles( [TestFiles.isfolder] | ismember({TestFiles.name}, {'Thumbs.db'}) ) = [];
TestFileNames = fullfile({TestFiles.folder}, {TestFiles.name});
Test_number = length(TestFileNames);
T = CreateDatabase(TrainDatabasePath); % T is the 1D matrix of the traindatabase
[m, A, Eigenfaces] = EigenfaceCore(T);
%%%%%%%%%%%%%%%%%%%%%%%%%%
for j = 1:Test_number
TestImage = TestFileNames{j};
[~, basename, ~] = fileparts(TestImage);
im = imread(TestImage);
[OutputName, Recognized_index] = Recognition(TestImage, m, A, Eigenfaces);
SelectedImageName = fullfile(TrainDatabasePath, OutputName);
SelectedImage = imread(SelectedImageName);
imshow(SelectedImage);
fprintf('Matched image for "%s" is: "%s"\n', basename, OutputName);
end
Sindhuja Chaduvula
on 7 Jun 2021
I am running the code on mac ..
so what path should i use
im not even getting any errors
Sindhuja Chaduvula
on 7 Jun 2021
>> main
>>
this is my output in command window
Sindhuja Chaduvula
on 7 Jun 2021
Unrecognized field name "isfolder".
Error in main2 (line 8)
TestFiles( [TestFiles.isfolder] | ismember({TestFiles.name}, {'Thumbs.db'}) ) = [];
>>
this is the output after running your code
Sindhuja Chaduvula
on 7 Jun 2021
can u please help me in solving this error
Walter Roberson
on 7 Jun 2021
Sorry should be isdir instead of isfolder
Sindhuja Chaduvula
on 7 Jun 2021
Edited: Walter Roberson
on 7 Jun 2021
function [OutputName,Recognized_index] = Recognition( TestImage, m, A, Eigenfaces)
% Recognizing step....
%
% Description: This function compares two faces by projecting the images into facespace and
% measuring the Euclidean distance between 'them.
%
% Argument: TestImage - Path of the input test image
%
% m - (M*Nx1) Mean of the training
% database, which is output of 'EigenfaceCore' function.
%
% Eigenfaces - (M*Nx(P-1)) Eigen vectors of the
% covariance matrix of the training
% database, which is output of 'EigenfaceCore' function.
%
% A - (M*NxP) Matrix of centered image
% vectors, which is output of 'EigenfaceCore' function.
%
% Returns: OutputName - Name of the recognized image in the training database.
%
% See also: RESHAPE, STRCAT
%%%%%%%%%%%%%%%%%%%%%%%% Projecting centered image vectors into facespace
% All centered images are projected into facespace by multiplying in
% Eigenface basis's. Projected vector of each face will be its corresponding
% feature vector.
ProjectedImages = [];
Train_Number = size(Eigenfaces,2);
for i = 1 : Train_Number
temp = Eigenfaces'*A(:,i); % Projection of centered images into facespace
ProjectedImages = [ProjectedImages temp];
end
%%%%%%%%%%%%%%%%%%%%%%%% Extracting the PCA features from test image
InputImage = imread(TestImage);
temp = InputImage(:,:,1);
[irow, icol] = size(temp);
InImage = reshape(temp',irow*icol,1);
Difference = double(InImage)-m; % Centered test image
ProjectedTestImage = Eigenfaces'*Difference; % Test image feature vector
%%%%%%%%%%%%%%%%%%%%%%%% Calculating Euclidean distances
% Euclidean distances between the projected test image and the projection
% of all centered training images are calculated. Test image is
% supposed to have minimum distance with its corresponding image in the
% training database.
Euc_dist = [];
for i = 1 : Train_Number
q = ProjectedImages(:,i);
temp = ( norm( ProjectedTestImage - q ) )^2;
Euc_dist = [Euc_dist temp];
end
[Euc_dist_min , Recognized_index] = min(Euc_dist);
Euc_dist_min;
OutputName = strcat(int2str(Recognized_index),'.jpg');
end
Sindhuja Chaduvula
on 7 Jun 2021
>> main2
Error using -
Arrays have incompatible sizes for this operation.
Error in Recognition (line 44)
Difference = double(InImage)-m; % Centered test image
Error in main2 (line 18)
[OutputName, Recognized_index] = Recognition(TestImage, m, A, Eigenfaces);
Sindhuja Chaduvula
on 7 Jun 2021
after changing it as isdir im getting the above error
and the above code is for recoginition
can u please help me in solving the error
Walter Roberson
on 7 Jun 2021
Could you remind us which MATLAB version you are using? I suspect R2015a or earlier.
Walter Roberson
on 7 Jun 2021
You could get that error if the test images are not the same size as the training images.
Sindhuja Chaduvula
on 7 Jun 2021
im using MATLAB R2021a
Sindhuja Chaduvula
on 7 Jun 2021
>> main2
Error using imread>get_format_info (line 545)
Unable to determine the file format.
Error in imread (line 391)
fmt_s = get_format_info(fullname);
Error in main2 (line 17)
im = imread(TestImage);
Sindhuja Chaduvula
on 7 Jun 2021
after giving same no. of images im gettinthe above error
Walter Roberson
on 7 Jun 2021
Edited: Walter Roberson
on 7 Jun 2021
Your directory contains something that is not . or .. or Thumbs.db or a known image type.
Sindhuja Chaduvula
on 7 Jun 2021
My test and train database each contains 108 images so there isnt is one not know as image type yet im getting the same error
Walter Roberson
on 7 Jun 2021
TrainDatabasePath = '/Users/prasanth/Downloads/MATLAB code 3/database2';
TestDatabasePath = '/Users/prasanth/Downloads/MATLAB code 3/TestDatabase';
%%%%%%%%%%%%%%%%%%%%%%%%%%% No. of test images
TestFiles = dir(TestDatabasePath);
Test_Number = 0;
TestFiles( [TestFiles.isfolder] | ismember({TestFiles.name}, {'Thumbs.db'}) ) = [];
TestFileNames = fullfile({TestFiles.folder}, {TestFiles.name});
Test_number = length(TestFileNames);
T = CreateDatabase(TrainDatabasePath); % T is the 1D matrix of the traindatabase
[m, A, Eigenfaces] = EigenfaceCore(T);
%%%%%%%%%%%%%%%%%%%%%%%%%%
for j = 1:Test_number
TestImage = TestFileNames{j};
[~, basename, ~] = fileparts(TestImage);
try
im = imread(TestImage);
catch
fprintf('Warning: "%s" does not appear to be a readable image file\n', TestImage);
continue
end
[OutputName, Recognized_index] = Recognition(TestImage, m, A, Eigenfaces);
SelectedImageName = fullfile(TrainDatabasePath, OutputName);
SelectedImage = imread(SelectedImageName);
imshow(SelectedImage);
fprintf('Matched image for "%s" is: "%s"\n', basename, OutputName);
end
Accepted Answer
More Answers (0)
Categories
Find more on Semantic Segmentation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!