How can fix the error 'Invalid calling syntax for the "predict" command.' when using the predict function?
이전 댓글 표시
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
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Detect, Extract, and Match Features에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!