This code basically uses CNN and detects whether the image is bottle or not using image processing.
How to deploy my code on raspberry pi as a standalone?
1 view (last 30 days)
Show older comments
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)
Accepted Answer
Walter Roberson
on 28 Mar 2020
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 Comments
amgad
on 2 Jun 2020
A small question:
how to deploy 'load' to Raspberry Pi in order to have the code run as standalone
More Answers (0)
See Also
Categories
Find more on Recognition, Object Detection, and Semantic Segmentation 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!