showing: non existent field 'ClassNames'. Can someone please help me?

1 view (last 30 days)
function ObjectRecognizerLive_shape(trainedClassifier,bag)
[fig, ax1, ax2] = figureSetup(trainedClassifier);
% Start webcam
wcam = webcam;
% Run live car detection
while ishandle(fig)
% Step 1: Get Next Frame
img = snapshot(wcam);
grayimg = rgb2gray(img);
% Step 2: Extract Features
shapeFeatures = double(encode(bag,grayimg));
% Step 3: Predict car using extracted features
[imagepred, probabilities] = predict(trainedClassifier,shapeFeatures);
% Step 4: Plot Results
try
imshow(insertText(img,[640,1],upper(cellstr(imagepred)),...
'AnchorPoint','RightTop','FontSize',50,'BoxColor','Green',...
'BoxOpacity',0.4),'Parent',ax1);
ax2.Children.YData = probabilities;
ax2.YLim = [0 1];
catch err
end
drawnow
end
function cname = getClassifierName(trainedClassifier)
% getClassifierName extracts name of the classifier from a trained model
cname = class(trainedClassifier);
if isa(trainedClassifier,'ClassificationECOC')
cname = 'SVM';
end
pos = strfind(cname,'.');
if ~isempty(pos)
cname = cname(pos(end)+1:end);
end
function [fig, ax1, ax2] = figureSetup(trainedClassifier)
% figureSetup sets up figure window for webcam feed and bar chart for
% classification probability
warning('off','images:imshow:magnificationMustBeFitForDockedFigure')
set(0,'defaultfigurewindowstyle','docked')
fig = figure('Name','Recogonizer','NumberTitle','off');
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);
bar(ax2,zeros(1,numel(trainedClassifier.ClassNames)),'FaceColor',[0.2 0.6 0.8])
set(ax2,'XTickLabel',cellstr(trainedClassifier));
title(getClassifierName(trainedClassifier.ClassNames)), ylabel('Probability')
set(0,'defaultfigurewindowstyle','normal')
  10 Comments
Fernando Ortega Loza
Fernando Ortega Loza on 30 Mar 2023
Hi, te problem is the Classification Learner, the rsesult of this is a struct, into the object we have the Trained Classifier, into we have de ClassNames
Image Analyst
Image Analyst on 31 Mar 2023
@Fernando Ortega Loza When you export the trained model it goes into the workspace with the name you give it, by default "trainedModel'. When you call save() to save it into a mat file,
save('My Classifier.mat', 'trainedModel');
and then recall the mat file
s = load('My Classifier.mat');
s will be a structure. You can then get the trained model from the structure like this:
trainedModel = s.trainedModel
If you look at trained model, I believe one of the fields say how to use it. So then you can pass trainedModel into predict

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 7 Oct 2016
Use the debugger. It's the way the rest of us solve problems like this, and you should too. So look at where you call ObjectRecognizerLive_shape(trainedClassifier,bag) and see what you pass in for trainedClassifier to see if that class has any properties called ClassNames. It won't. It will have a field according to what classifier you used. So if you didn't save or remember the classifier you used you'll have to check the fields with fieldnames() and strcmp() or ismember() to see which class you used: ClassificationSVM, CompactClassificationSVM, ClassificationECOC, or CompactClassificationECOC.
Here is what the help says:
Find the name of the classification model object in the exported struct. Examine the fields of the struct to find the model name, for example, C.ClassificationSVM, where C is the name of your struct, e.g., trainedClassifier. Model name depends on what type of SVM you trained (binary or multiclass) and whether you exported a compact model or not. Models can be ClassificationSVM, CompactClassificationSVM, ClassificationECOC, or CompactClassificationECOC.
Like I said, you can use fieldnames() and strfind() for that.
  5 Comments
Hermise Raju
Hermise Raju on 7 Oct 2016
[imagepred, probabilities] = predict(trainedClassifier,shapeFeatures)
when I run this, it is showing that no valid system or data is specified. My system is the trainedClassifier from the classification learner app, and my data is the shapeFeature from features extracted from bag of features. Can you please help me why it is showing that error?

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!