I'm using MATLAB 2017b, my face recognition code doesn't work, anyone can help to solve?

% 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

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?
Error using imread>get_full_filename (line 516)
File "/Users/paulling95/Desktop/Demo/face database/Users/paulling95/Desktop/Demo/face database\1.jpg" does
not exist.
Error in imread (line 340)
fullname = get_full_filename(filename);
Error in CreateDatabase (line 40)
img = imread( fullfile('/Users/paulling95/Desktop/Demo/face database', str));
Error in main (line 43)
T = CreateDatabase(TrainDatabasePath);
These are the error.
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
Im not getting an output for this code in my mac cant u tell me wats wrong in it
\ 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
I am running the code on mac ..
so what path should i use
im not even getting any errors
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
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
>> 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);
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
Could you remind us which MATLAB version you are using? I suspect R2015a or earlier.
You could get that error if the test images are not the same size as the training images.
>> 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);
after giving same no. of images im gettinthe above error
Your directory contains something that is not . or .. or Thumbs.db or a known image type.
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
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

Sign in to comment.

 Accepted Answer

You are on Mac or Linux, and you are trying to use code that assumes Windows.
In the place that SelectedImage has a \ added into it, that should be a / for Linux or Mac. / also works for Windows.
Each place that a / or \ directory separator is added into the path should ideally be replaced with a call to fullfile()

2 Comments

Yeah, I'm Mac user.
Can i ask how to solve it using fullfile()? I'm still having error below.
function T = CreateDatabase(TrainDatabasePath)
% Align a set of face images (the training set T1, T2, ... , TM )
%
% Description: This function reshapes all 2D images of the training database
% into 1D column vectors. Then, it puts these 1D column vectors in a row to
% construct 2D matrix 'T'.
%
%
% Argument: TrainDatabasePath - Path of the training database
%
% Returns: T - A 2D matrix, containing all 1D image vectors.
% Suppose all P images in the training database
% have the same size of MxN. So the length of 1D
% column vectors is MN and 'T' will be a MNxP 2D matrix.
%
% See also: STRCMP, STRCAT, RESHAPE
% Original version by Amir Hossein Omidvarnia, October 2007
% Email: aomidvar@ece.ut.ac.ir
%%%%%%%%%%%%%%%%%%%%%%%% File management
TrainFiles = dir(TrainDatabasePath);
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
%%%%%%%%%%%%%%%%%%%%%%%% Construction of 2D matrix from 1D image vectors
T = [];
for i = 1 : Train_Number
% I have chosen the name of each image in databases as a corresponding
% number. However, it is not mandatory!
str = int2str(i);
str = strcat('/',str,'.jpg');
str = strcat(TrainDatabasePath,str);
img = imread(str);
%img = rgb2gray(img);
[irow icol] = size(img);
temp = reshape(img',irow*icol,1); % Reshaping 2D images into 1D image vectors
T = [T temp]; % 'T' grows after each turn
end
I get this error.
>> TrainDatabasePath= '/Users/paulling95/Desktop/Demo/faces database';
TestDatabasePath= '/Users/paulling95/Desktop/Demo/Cropped faces';
>> T = CreateDatabase(TrainDatabasePath)
Error using imread>get_full_filename (line 516)
File "/Users/paulling95/Desktop/Demo/faces database/29.jpg" does not exist.
Error in imread (line 340)
fullname = get_full_filename(filename);
Error in CreateDatabase (line 40)
img = imread(str);
Replace the stuff after '% I have chosen' down to
str = fullfile( TrainDatabasePath, sprintf('%d.jpg', i) );
if ~exist(str, 'file')
error('could not find input file "%s"', str);
end
img = imread(str);
if ndims(img) > 2; img = rgb2gray(img); end
T = [T, img(:)];
However this is likely to generate the same file name. What do you see if you do
ls( fullfile(TrainDatabasePath, '*29*') )

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!