MATLAB Answers

Hi, How do I resize a video to satisfy google net's size input layer ?

2 views (last 30 days)
Syafiqah Daud
Syafiqah Daud on 18 Jul 2018
Answered: Guillaume on 18 Jul 2018
I am currently doing a project whereby I have to detect vehicles using googlenet but I need to resize the size of the video for that. Anyone is able to help?
  4 Comments
Syafiqah Daud
Syafiqah Daud on 18 Jul 2018
Are you willing to clarify about "pass it each frame of the video." As I am new to MatLab. Here is my code. Thank you!
clear
% camera = webcam; % Connect to the camera
net = googlenet; % Load the neural net
net.Layers
while true
labelType =''
trafficVid = VideoReader('C:\Users\syafiqah\Desktop\project\traffic.mp4')
get(trafficVid)
implay('C:\Users\syafiqah\Desktop\project\traffic.mp4');
sz = net.Layers(1).Inputsize;
trafficVid = imresize(trafficVid,[sz(1),sz(2)]);
label = classify(net, video); % Classify the picture
if (label == 'ambulance')
labelType = 'Vehicle Recognised'
elseif (label == 'moped')
labelType = 'Vehicle Recognised'
elseif (label == 'tank')
labelType = 'Vehicle Recognised'
elseif (label == 'sports car')
labelType = 'Vehicle Recognised'
elseif (label == 'moving van')
labelType = 'Vehicle Recognised'
elseif (label == 'trailer truck')
labelType = 'Vehicle Recognised'
elseif (label == 'garbage truck')
labelType = 'Vehicle Recognised'
elseif (label == 'trailer truck')
labelType = 'Vehicle Recognised'
else
end
title(char(labelType)); % Show the label
darkCarValue = 50;
darkCar = rgb2gray(read(trafficVid,71));
noDarkCar = imextendedmax(darkCar, darkCarValue);
imshow(darkCar)
figure, imshow(noDarkCar)
sedisk = strel('disk',2);
noSmallStructures = imopen(noDarkCar, sedisk);
imshow(noSmallStructures)
nframes = trafficVid.NumberOfFrames;
I = read(trafficVid, 1);
taggedCars = zeros([size(I,1) size(I,2) 3 nframes], class(I));
for k = 1 : nframes
singleFrame = READFRAME(trafficVid, k);
% Convert to grayscale to do morphological processing.
I = rgb2gray(singleFrame);
% Remove dark cars.
noDarkCars = imextendedmax(I, darkCarValue);
% Remove lane markings and other non-disk shaped structures.
noSmallStructures = imopen(noDarkCars, sedisk);
% Remove small structures.
noSmallStructures = bwareaopen(noSmallStructures, 150);
% Get the area and centroid of each remaining object in the frame. The
% object with the largest area is the light-colored car. Create a copy
% of the original frame and tag the car by changing the centroid pixel
% value to red.
taggedCars(:,:,:,k) = singleFrame;
stats = regionprops(noSmallStructures, {'Centroid','Area'});
if ~isempty([stats.Area])
areaArray = [stats.Area];
[junk,idx] = max(areaArray);
c = stats(idx).Centroid;
c = floor(fliplr(c));
width = 2;
row = c(1)-width:c(1)+width;
col = c(2)-width:c(2)+width;
taggedCars(row,col,1,k) = 255;
taggedCars(row,col,2,k) = 0;
taggedCars(row,col,3,k) = 0;
end
end
frameRate = trafficVid.FrameRate;
implay(taggedCars,frameRate);
drawnow;
end

Sign in to comment.

Answers (2)

Diwakar Ravichandran
Diwakar Ravichandran on 18 Jul 2018
Hi Syafiqah,
This is a question that was already answered on MATLAB answers. This should give you some headway for your problem. This is the link to the answer.
Hope this helps
Cheers!

Guillaume
Guillaume on 18 Jul 2018
Are you willing to clarify about "pass it each frame of the video."
Your question is puzzling since at first glance you're doing the right thing in the 2nd half of your code. Yet, the first half of your code is nonsense.
The classification should be done similarly to the 2nd half. Something similar to:
trafficVid = VideoReader('C:\Users\syafiqah\Desktop\project\traffic.mp4');
for k = 1:trafficVid.NumberOfFrames %or use while trafficVid.HasFrame
label = classify(net, imresize(trafficis.readFrame, net.Layers(1).Inputsize)); %get image, resize and pass to classifier
%...
end

Community Treasure Hunt

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

Start Hunting!

Translated by