How can fix the error 'Invalid calling syntax for the "predict" command.' when using the predict function?

12 visualizaciones (últimos 30 días)
I used fitcsvm to train INRIAPerson and when I was using the predict function to get the label of input images, I got an error which is 'Invalid calling syntax for the "predict" command'. But it worked in another code. It confused me and I really do not know how to fix it. Here is how I train the classifier:
function TrainingSVM()
%Input the training images and set image size.
imgTrain = imageDatastore('D:\TrainImage\INRIAPerson','IncludeSubfolders', true, 'LabelSource', 'foldernames');
imageSize = [96, 48];
Test = readimage(imgTrain,1);
Test = imresize(Test,imageSize);
[features, ~] = extractHOGFeatures(Test);
numImages = length(imgTrain.Files);
data = zeros(numImages,size(features,2),'single');
for i = 1:numImages
Train = readimage(imgTrain,i);
Train = imresize(Train,imageSize);
data(i,:) = extractHOGFeatures(Train);
end
trainLabels = imgTrain.Labels;
classifier = fitcsvm(data,trainLabels);
save classifier
end
Here is the error code.
function PedestrianTracking()
%Load the pre-trained classifier
classifier = 'classifier.mat';
%Create object obj
obj = setupSystemObjects();
% Detect moving objects, and track them across video frames.
while ~isDone(obj.reader)
Frame = readFrame();
frame = imresize(Frame, 0.5, 'Antialiasing',false);
[centroids, bboxes, ~] = detectObjects(frame);
Size = size(bboxes);
%Find areas contain people and save them.
Bboxes = [];
Centroids = [];
for i = 1:Size(1,1)
%Get the target area
detectImage = imcrop(frame, bboxes(i, :));
%Resize the target area and do the classification with classifier
DetectImage = imresize(detectImage, [96, 48]);
feature = extractHOGFeatures(DetectImage);
[predictIndex,~] = predict(classifier,feature);
% If people are detected in the DetectImage then store it.
if ( char(predictIndex) - 48 ) == 1
Bboxes = [Bboxes; bboxes(i, :)];
Centroids = [Centroids, centroids(i, :)]
end
end
end
function [centroids, bboxes, mask] = detectObjects(frame)
% Detect foreground.
mask = obj.detector.step(frame);
% Apply morphological operations to remove noise and fill in holes.
mask = imopen(mask, strel('rectangle', [3,3]));
mask = imclose(mask, strel('rectangle', [15, 15]));
mask = imfill(mask, 'holes');
% Perform blob analysis to find connected components.
[~, centroids, bboxes] = obj.blobAnalyser.step(mask);
end
function obj = setupSystemObjects()
obj.reader = vision.VideoFileReader('D:D:\TestVideo\1.mp4');
obj.detector = vision.ForegroundDetector('NumGaussians', 4, ...
'NumTrainingFrames', 20, 'MinimumBackgroundRatio', 0.7);
obj.blobAnalyser = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', true, 'CentroidOutputPort', true, ...
'MinimumBlobArea', 4000);
obj.videoPlayer = vision.VideoPlayer('Position', [20, 400, 700, 400]);
end
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Jun. de 2017
You have
classifier = 'classifier.mat';
and you expect that to be enough to reference a saved classifier. It is not. You need to do something more like
filename = 'classifier.mat'
fileinfo = whos('-file', filename);
varclasses = {fileinfo.class};
matches = ~cellfun(@isempty, regexpi(varclasses, 'classification'));
matchidx = find(matches, 1, 'first');
if isempty(matchidx); error('File "%s" has no classifier stored in it', filename); end
matchvar = fileinfo(matchidx).name;
file_data = load(filename, matchvar);
classifier = file_data.(matchvar);
... All of which would be much easier if only you already knew what the name of the variable was that had been used to store the classifier in the .mat file.
  2 comentarios
BOFU LIU
BOFU LIU el 26 de Jun. de 2017
Yeah, I just found out that I only wrote down the file path but forgot to load the file. Thank you very much. But I met another problem with the same code after I load the classifier. It is said 'No valid system or dataset was specified.'
Walter Roberson
Walter Roberson el 26 de Jun. de 2017
It appears that the code I posted can be abbreviated to:
filename = 'classifier.mat';
file_data = load(filename, 'classifier');
classifier = file_data.classifier;
You could try that and see if it makes a difference.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by