matlab-deep-learning / Pretrained-YOLOv8-Network-For-Object-Detection

YOLO v8 training and inference in MATLAB for Object Detection with yolov8n, yolov8s, yolov8m, yolov8l, yolov8x, networks
https://in.mathworks.com/help/vision/ug/getting-started-with-object-detection-using-deep-learning.html
GNU Affero General Public License v3.0
29 stars 3 forks source link

Unable to run model on live feed #8

Closed jmaxwell-21 closed 2 months ago

jmaxwell-21 commented 3 months ago

I'm trying to run the YOLOv8 Prediction model on a live webcam feed. I know it only works on images, so I am running it in a loop where I input snapshots from the webcam. My code can be seen below. The problem is, when I run this code, the detection is incredibly slow - there is a near 10 second delay. If anyone could help, it would be greatly appreciated.

% Load YOLO v8 small network.
det = yolov8ObjectDetector('yolov8s');

% Initialize the webcam
cam = webcam;

% Create a figure for displaying the results
fig = figure;

% Start the real-time detection loop
while ishandle(fig)
    % Capture a frame from the webcam
    I = snapshot(cam);

    % Perform detection using pretrained model
    [bboxes, scores, labels] = detect(det, I);

    % Visualize detection results
    annotations = string(labels) + ': ' + string(scores);
    Iout = insertObjectAnnotation(I, 'rectangle', bboxes, annotations);

    % Display the annotated frame
    imshow(Iout, 'Parent', gca);

    % Pause for a short time to allow figure update
    pause(0.1);
end

% Clear the webcam object
clear cam;
viakkala commented 3 months ago

Hi, Did you try codegen workflow? You may refer to Code Generation for Object Detection Using YOLO v4 Deep Learning example for more details.

jmaxwell-21 commented 2 months ago

I ran the code in the command window instead of a script, and that made it work better!