Housekeeping
close all; clear; clc;
Initialize our Camera, Frame Size, & Video Player
cam = ipcam('http://10.0.0.187:8081/video.mjpeg'); % Replace with your Pi's IP address
videoFrame = snapshot(cam); % Get a sample frame from the camera
fsize = size(videoFrame); % Get the size of our sample frame
videoPlayer = vision.VideoPlayer('Position', [100 100 [fsize(2), fsize(1)]+30]); % Initialize our video player based on our frame size
Load our Object Detector
Note: Initializing the detectors takes a minute, it might be a good idea to include a section break after this section
yolo = yolov4ObjectDetector("tiny-yolov4-coco"); % The "tiny-yolo" detector is much faster, but less accurate
%yolo = yolov4ObjectDetector("csp-darknet53-coco"); % The "darknet" detector is more accurate, but slower
detector_threshold = 0.3; % Select a threshold for detection (default = 0.5)
Main Loop
runLoop = true; % Boolean to break the loop when the video player is closed
while runLoop
tic % Start a timer for measuring frames-per-second
frame = snapshot(cam); % Get a new frame
[bboxes,scores,labels] = detect(yolo, frame, "Threshold", detector_threshold); % run the detector on the current frame
[bboxes,scores,labels] = selectStrongestBbox(bboxes,scores); % cull the bounding boxes
frame = insertObjectAnnotation(frame,"rectangle",bboxes,labels); % Add bounding boxes to the frame
fps = string(1/toc)+' fps'; % Calculate current frames/second
frame = insertText(frame, [50 50], fps); % Add the frame-time to the frame
step(videoPlayer, frame); % Display the annotated video frame using the video player object.
runLoop = isOpen(videoPlayer); % Check whether the video player window has been closed.
end
clear cam; % Clear the camera object when we're done
release(videoPlayer); % Release the video player
Example & Comments
As you can see, the tiny-yolo detector successfully picks up a few vehicles, but fails to notice ones that are further away, under poor lighting, partially obscured, etc. More powerful detectors
like the darknet-53 network do a better job at detection, but with much slower performance. We can decrease the detection threshold to find more vehicles, but the detector will also pick up
objects like trees as vehicles as well. A solution including transfer learning will be shown soon to demonstrate re-training a pre-trained network like "tiny-yolo" to better detect partially-obscured
vehicles, or entirely new objects altogether.