live object detection using raspberry pi cam

6 views (last 30 days)
clear classes;
net = squeezenet();
net.Layers
plot(net)
mypi = raspi;
mycam = cameraboard(mypi,'Resolution','1280x720');
for ii = 1:500
img = snapshot(mycam);
imagesc(img);
drawnow
end
mycam.Rotation = 180;
v = VideoReader('myvideo.mpeg');
frames = readframe(v,[01 Inf]);
stop(mycam);
stop(v);
getFile(mypi,'myvideo.mpeg','C:\Users\cyberlab\Documents\MATLAB');
%% Create Video Player
videoPlayer = vision.VideoPlayer;
fgPlayer = vision.VideoPlayer;
%% Create Foreground Detector (Background Subtraction)
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3,'NumTrainingFrames', 50);
%% Run on first 75 frames to learn background
for i = 1:75
videoFrame = step(videoReader);
foreground = step(foregroundDetector,videoFrame);
end
pictures = dir('C:\Users\cyberlab\Documents\MATLAB\automobile/*.jpg');
numFiles = length(pictures);
for i_pics = 1:numFiles
bild = imread(pictures(i_pics).name);
image{i_pics} = double(bild)/255;
end
%% Perform morphology to clean up foreground
cleanForeground = imopen(foreground, strel('Disk',1));
%% Create blob analysis object
%Blob analysis object further filters the detected foreground by rejecting blobs which contain fewer
% than 150 pixels.
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', false, 'CentroidOutputPort', false, ...
'MinimumBlobArea', 500);
%% Loop through video
while ~isDone(videoReader)
%Get the next frame
videoFrame = step(videoReader);
%Detect foreground pixels
foreground = step(foregroundDetector,videoFrame);
% Perform morphological filtering
cleanForeground = imopen(foreground, strel('Disk',1));
% Detect the connected components with the specified minimum area, and
% compute their bounding boxes
bbox = step(blobAnalysis, cleanForeground);
% Draw bounding boxes around the detected cars
result = insertShape(videoFrame, 'Rectangle', bbox, 'Color', 'red');
% Display the number of cars found in the video frame
numCars = size(bbox, 1);
text = sprintf('Detected Vehicles = %d',numCars);
result = insertText(result, [320 320], numCars, 'BoxOpacity', 1, ...
'FontSize', 14);
% Display output
step(videoPlayer, result);
disp('numCars');
disp(numCars)
% find total number of cars in whole frame
for ii=1:length(videoFrame)%numof frames
totalcount(ii)=videoFrame(ii);
end
totcars=sum(totalcount)/5;
end
disp('Total number of cars in entire video')
disp(round(totcars));
%% release video reader and writer
release(videoPlayer);
release(videoReader);
release(fgPlayer);
delete(videoPlayer); % delete will cause the viewer to close
delete(fgPlayer);2.PNG

Answers (1)

Bhaskar Vundurthy
Bhaskar Vundurthy on 8 Jan 2020
While 'myvideo.mpeg' is being called (line 13), there does not exist a file with such a name in the current MATLAB path.
Modify the filename to retreive appropriate file or consider calling the video file 'vid.mp4' that is present in the current MATLAB path.

Categories

Find more on MATLAB Support Package for Raspberry Pi Hardware 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!