Rectangle on the detected class's position in an image using a trained cnn

1 vue (au cours des 30 derniers jours)
vdk vdk
vdk vdk le 15 Juin 2019
Commenté : vdk vdk le 17 Juin 2019
camera = webcam;
camera.Resolution = '640x480';
net = googlenet;
h = figure;
while ishandle(h)
im = snapshot(camera);
image(im)
I1 = imcrop(im, [ 1 1 160 479]);
[label1,score1] = classify(trainNet,I1);
I2 = imcrop(im, [ 161 1 160 479]);
[label2,score2] = classify(trainNet,I2);
I3 = imcrop(im, [ 321 1 160 479]);
[label3,score3] = classify(trainNet,I3);
I4 = imcrop(im, [ 480 1 160 479]);
[label4,score4] = classify(trainNet,I4);
if label1=='door'
rectangle('Position',[ 1 1 160 480],'EdgeColor','b','LineWidth',3)
end
if label2=='vrata'
rectangle('door',[ 161 1 160 479],'EdgeColor','b','LineWidth',3)
end
if label3=='vrata'
rectangle('door',[ 321 1 160 479],'EdgeColor','b','LineWidth',3)
end
if label4=='vrata'
rectangle('door',[ 480 1 160 479],'EdgeColor','b','LineWidth',3)
end
end
I created a CNN with 2 classes 'door' and 'no door' and i'm using it to detect a door with this code that devides the screen from the camera into 4 parts and it uses rectangle on the the parts it detects a door, but i really want for the rectangle to appear on the exact position of the detected door, is there a way to do that?
  4 commentaires
Image Analyst
Image Analyst le 16 Juin 2019
What did you do to train the network that you DO have right now? It should be pretty similar. Replace the last layer with your ground truth and feed in your labeled training images. What did you do to create your existing trainNet?
vdk vdk
vdk vdk le 17 Juin 2019
This is the code i used to create the CNN, i'm using 7441 images for door and 7448 for no door.
clear
clc
imagesDir = 'D:\uchene\4ti kurs\diplomna chast\New folder\posleden test';
images = imageDatastore(imagesDir,'IncludeSubfolders',true,'FileExtensions','.jpg','LabelSource','foldernames');
[imdsTrain,imdsValidation] = ...
splitEachLabel(images,0.7,'randomized');
numClasses = numel(categories(imdsTrain.Labels));
trainingLabels = imdsTrain.Labels;
filterSize = [5 5];
numFilters = 42;
layers = [
imageInputLayer([480 161 3]);
convolution2dLayer(filterSize, numFilters, 'Padding', 2)
reluLayer()
maxPooling2dLayer(3, 'Stride', 2)
convolution2dLayer(filterSize, numFilters, 'Padding', 2)
reluLayer()
convolution2dLayer(filterSize, numFilters, 'Padding', 2)
reluLayer()
maxPooling2dLayer(3, 'Stride',2)
convolution2dLayer(filterSize, 2 * numFilters, 'Padding', 2)
reluLayer()
convolution2dLayer(filterSize, 2 * numFilters, 'Padding', 2)
reluLayer()
maxPooling2dLayer(3, 'Stride',2)
fullyConnectedLayer(numClasses*32)
reluLayer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer
];
miniBatchSize = 64;
numIterationsPerEpoch = floor(numel(imdsTrain.Labels)/miniBatchSize);
options = trainingOptions('sgdm',...
'MiniBatchSize',miniBatchSize,...
'MaxEpochs',50,...
'InitialLearnRate',1e-5,...
'L2Regularization', 0.004, ...
'Verbose',false,...
'Plots','training-progress',...
'ValidationData',imdsValidation,...
'ValidationFrequency',numIterationsPerEpoch,...
'ExecutionEnvironment','gpu');
trainNet = trainNetwork(imdsTrain,layers,options);

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by