How to deploy my code on raspberry pi as a standalone?

1 次查看(过去 30 天)
vid = videoinput('winvideo', 1);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img)
h = findobj('type','figure');
n = length(h);
for k=1:n
baseFileName = sprintf('Img #%d.png', k);
fullFileName = fullfile('C:\Users\Cv\Desktop\image classification2 - copy',['img' '.bmp']);
imwrite(img, fullFileName);
end
outputFolder = fullfile('caltech102');
rootFolder = fullfile(outputFolder, '101_ObjectCategories');
categories = {'Bottles', 'NotBottles'};
imds = imageDatastore(fullfile(rootFolder,categories),'LabelSource', 'foldernames');
tb1 = countEachLabel(imds)
minSetCount = min(tb1{:,2})
imds = splitEachLabel(imds, minSetCount, 'randomize');
countEachLabel(imds);
Bottles = find(imds.Labels == 'Bottles', 1);
NotBottles = find(imds.Labels == 'NotBottles', 1);
% figure
% subplot(2,2,1);
% imshow(readimage(imds,airplanes));
% subplot(2,2,2);
% imshow(readimage(imds,ferry));
% subplot(2,2,3);
% imshow(readimage(imds,laptop));
net = resnet50();
figure
plot(net)
title('Architecture of ResNet-50');
set(gca, 'YLim', [150 170]);
net.Layers(1);
net.Layers(end);
numel(net.Layers(end).ClassNames);
[trainingSet, testSet] = splitEachLabel(imds, 0.3, 'randomize');
imageSize = net.Layers(1).InputSize;
augmentedTrainingSet = augmentedImageDatastore(imageSize, ...
trainingSet, 'ColorPreprocessing', 'gray2rgb');
augmentedTestSet = augmentedImageDatastore(imageSize, ...
testSet, 'ColorPreprocessing', 'gray2rgb');
w1 = net.Layers(2).Weights;
w1 = mat2gray(w1);
figure
montage(w1)
title('First Convolutional Layer Weight')
featureLayer = 'fc1000';
trainingFeatures = activations(net, ...
augmentedTrainingSet, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
trainingLables = trainingSet.Labels;
classifier = fitcecoc(trainingFeatures,trainingLables, ...
'Learner', 'Linear', 'Coding', 'onevsall','ObservationsIn', 'columns');
testFeatures = activations(net, ...
augmentedTestSet, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
predictLabels = predict(classifier, testFeatures, 'ObservationsIn','columns');
testLables = testSet.Labels;
confMat = confusionmat(testLables,predictLabels);
confMat = bsxfun(@rdivide, confMat, sum(confMat,2));
mean(diag(confMat));
newImage = imread(fullfile('img.bmp'));
ds = augmentedImageDatastore(imageSize, ...
newImage, 'ColorPreprocessing', 'gray2rgb');
imageFeatures = activations(net, ...
ds, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
Label = predict(classifier, imageFeatures, 'ObservationsIn','columns');
sprintf('The Loaded image belongs to %s class', Label)
  1 个评论
Ahmed abbasi
Ahmed abbasi 2020-3-28
This code basically uses CNN and detects whether the image is bottle or not using image processing.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2020-3-28
It is not possible to deploy CNN training to hardware.
It is not possible to deploy augmentedImageDatastore to hardware.
Your strategy would have to be to train on the host, and save the net and activations, and load() it in the code that was deployed to hardware, where you would use it only to predict()
  4 个评论
Ahmed abbasi
Ahmed abbasi 2020-3-28
Okay now i got your point. Training will be done in the Host computer and than deploy and ras pi for prediction. Thank you so much. Appreciated SIR.
amgad
amgad 2020-6-2
A small question:
how to deploy 'load' to Raspberry Pi in order to have the code run as standalone

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Recognition, Object Detection, and Semantic Segmentation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by