Home Classify bounding boxes using Neural Net, in a video. MATLAB
Reply: 0

Classify bounding boxes using Neural Net, in a video. MATLAB

Rana Raees
Rana Raees Published in 2018-01-12 17:43:19Z

I am working on processing a video to detect and classify cars according to their model. My aim is to draw bounding boxes around detected cars, and classify those cars using neural net, and display the model of the car as its label above the bounding box. what i have achieved so far is:

  1. draw bounding boxes on detected cars,
  2. get all the bounding boxes and classify them using a .mat file, which is a trained neural network for car models.
  3. display label on the bounding boxes of each car

my problem is:

  1. either i get only one label applied on all bounding boxes of cars, on every frame.
  2. or i get no label at all.

what i want is:

  1. to classify all bounding boxes according to their models in the video.
  2. display the label on the bounding box after classification, on the video.

please see my attached code. m-file. any help would be highly appreciated.

load ('D:\mathworks\BIGDATA.mat')
nnet = myNet; //variable from mat file

foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, ...
'NumTrainingFrames', 50);

% Read a video frame and run the detector.
videoFileReader = vision.VideoFileReader('C:\Users\Raees 
for i=1:150
videoFrame      = step(videoFileReader);
foreground      = step(foregroundDetector, videoFrame);
% Draw the returned bounding box around the detected face.
%videoOut = insertObjectAnnotation(videoFrame,'rectangle',bbox,'Face');
%figure, imshow(videoOut), title('Detected face');
figure; imshow(videoFrame); title('Video Frame');
figure; imshow(foreground); title('Foreground');
se = strel('square', 3);
filteredForeground = imopen(foreground, se);
figure; imshow(filteredForeground); title('Clean Foreground');
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', false, 'CentroidOutputPort', false, ...
'MinimumBlobArea', 150);
bbox = step(blobAnalysis, filteredForeground);
result = insertShape(videoFrame, 'Rectangle', bbox, 'Color', 'green');
numCars = size(bbox, 1);
result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, ...
'FontSize', 14);
figure; imshow(result); title('Detected Cars');
videoPlayer = vision.VideoPlayer('Name', 'Detected Cars');
videoPlayer.Position(3:4) = [650,400];  % window size: [width, height]
se = strel('square', 3); % morphological filter for noise removal
%maskPlayer = vision.VideoPlayer('Position', [740, 400, 700, 400]);

while ~isDone(videoFileReader)

videoFrame = step(videoFileReader); % read the next video frame

% Detect the foreground in the current video frame
foreground = step(foregroundDetector, videoFrame);

% Use morphological opening to remove noise in the foreground
filteredForeground = imopen(foreground, se);

% Detect the connected components with the specified minimum area, and
% compute their bounding boxes

bbox = step(blobAnalysis, filteredForeground);

% Draw bounding boxes around the detected cars
result = insertShape(videoFrame, 'Rectangle', bbox, 'Color', 'green');

% result = insertObjectAnnotation(videoFrame,'rectangle',bbox,label1);

% Display the number of cars found in the video frame
numCars = size(bbox, 1);
%result1 = cell2mat(result);

labelname = cell(numCars,1);
for i=1:numCars
image = videoFrame(bbox(i,2):bbox(i,2)+ bbox(i,4)-1,bbox(i,1):bbox(i,1)+ 
image = imresize(image , [227 227]);
%image = im2uint8(image);
%Ypred2 = categorical(1);
label1 = classify(nnet,image);
%Ypred2 = categorical(label1);
name1 = char(label1);
labelname{i} = name1;

%labelname{i} = char(label1)

result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, ...
'FontSize', 14);

%if numCars>0
%result2 = insertText(videoFrame, [10 10], labelname, 'BoxOpacity', 1, ...
%'FontSize', 14);
%result2 = insertObjectAnnotation(videoFrame,'rectangle',bbox,label1);
result = insertObjectAnnotation(videoFrame,'rectangle',bbox,labelname,... 
%result2 = insertText(result, [10 10], label1, 'BoxOpacity', 1, ...
%   'FontSize', 14);

step(videoPlayer, result);  % display the results
%step(maskPlayer, filteredForeground);  % display the results


release(videoFileReader); % close the video file

this is the snapshot of my output:

output of video classification using neural network

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.554821 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO