I am having an error in running this code. Kindly help.
Show older comments
Below is the error message received when I ran the classifier section of the code:
Error in SimpleFaceRecognition (line 39)
trainingFeatures(featureCount,:) = extractHOGFeatures(read(training(i),j));
Undefined function or variable 'trainingLabel'.
Error in SimpleFaceRecognition (line 47)
faceClassifier = fitcecoc(trainingFeatures,trainingLabel);
%%Load Image Information from ATT Face Database Directory
faceDatabase = imageSet('orl_faces','recursive');
%%Display Montage of First Face
figure;
montage(faceDatabase(1).ImageLocation);
title('Images of Single Face');
Display Query Image and Database Side-Side
personToQuery = 1;
galleryImage = read(faceDatabase(personToQuery),1);
figure;
for i=1:size(faceDatabase,2)
imageList(i) = faceDatabase(i).ImageLocation(5);
end
subplot(1,2,1);imshow(galleryImage);
subplot(1,2,2);montage(imageList);
diff = zeros(1,9);
%%Split Database into Training & Test Sets
[training,test] = partition(faceDatabase,[0.8 0.2]);
%%Extract and display Histogram of Oriented Gradient Features for single face
person = 5;
[hogFeature, visualization]= ...
extractHOGFeatures(read(training(person),1));
figure;
subplot(2,1,1);imshow(read(training(person),1));title('Input Face');
subplot(2,1,2);plot(visualization);title('HoG Feature');
%%Extract HOG Features for training set
trainingFeatures = zeros(size(training,2)*training(1).Count,4680);
featureCount = 1;
for i=1:size(training,2)
for j = 1:training(i).Count
trainingFeatures(featureCount,:) = extractHOGFeatures(read(training(i),j));
trainingLabel{featureCount} = training(i).Description;
featureCount = featureCount + 1;
end
personIndex{i} = training(i).Description;
end
%%Create 40 class classifier using fitcecoc
faceClassifier = fitcecoc(trainingFeatures,trainingLabel);
3 Comments
Geoff Hayes
on 2 Apr 2018
Chidibere - it looks like your trainingLabel variable is only initialized and updated within the for loop. So if your code does not enter that loop then this variable will be undefined. I suggest that you put a breakpoint at the line
for i=1:size(training,2)
and then run your code. What are the dimensions of training? What is the value of training(i).Count? Does your code ever reach the line
trainingLabel{featureCount} = training(i).Description;
Also, it is good practice to assign something to this variable outside of your for loop....even if it is just simply
trainingLabel = [];
just so that you can avoid error like this in the future.
Chidiebere Ike
on 2 Apr 2018
Edited: Chidiebere Ike
on 2 Apr 2018
Geoff Hayes
on 2 Apr 2018
Should trainingFeatures be a cell array? what are the dimensions of the object that you are assigning to this array? will the dimensions be the same for each iteration of the loop? From https://www.mathworks.com/matlabcentral/answers/93586-why-do-i-get-the-subscripted-assignment-dimension-mismatch-error-message,
This error occurs when you attempt to assign elements to an existing array, but the size of the variable you are trying to assign is not compatible with the existing array.
Answers (0)
Categories
Find more on Face Detection 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!