Clear Filters
Clear Filters

isolating bounding boxes from a video using detector

1 view (last 30 days)
Hi mathworks team,
I am trying to isolate bounding boxes values from frames of video using trained detector but it gives this error ''Index exceeds the number of array elements. Index must not exceed 1.''
here is code
% import the video file
obj = VideoReader('test2video.mp4');
vid = read(obj);
% read the total number of frames
frames = obj.NumberOfFrames;
% file format of the frames to be saved in
ST ='.jpg';
a = cell(1, frames+1);
% reading and writing the frames
for x = 1 : frames
% % converting integer to string
Sx = num2str(x);
%
% % concatenating 2 strings
Strc = strcat(Sx, ST);
Vid = vid(:, :, :, x);
cd frames;
% %
% % % exporting the frames
imwrite(Vid, Strc);
cd ..
for i = 1:x
m = imread(['C:\Users\shirs\Documents\SURF\SSDVOBJECT\frames\' Sx(i) '.jpg']);
[bboxes, scores] = detect(detector, m);
a{i} = bboxes;
figure
imshow(m)
end
end
  3 Comments
Shirshak
Shirshak on 27 Jan 2023
No .. this line is for saving it in one folder..then from there i try to run a loop and isolate the bounding boxes.
any answers ?

Sign in to comment.

Answers (1)

Ajay Gajulapally
Ajay Gajulapally on 2 Mar 2023
Edited: Ajay Gajulapally on 2 Mar 2023
Hi Shirshak,
As per my understanding, you want to isolate the bounding boxes of detected objects in a video frame by frame. Kindly check the way you use your second for loop. The modified code can go as:
  1. Importing the video file and reading the frames
% import the video file
obj = VideoReader('________'); % use the video file here
vid = read(obj);
% read the total number of frames
frames = obj.NumFrames;
2. Writing the frames to a required path.
% file format of the frames to be saved in
ST ='.jpg';
% reading and writing the frames
for x = 1 : frames
% % converting integer to string
Sx = num2str(x);
%
% % concatenating 2 strings
Strc = strcat(Sx, ST);
Vid = vid(:, :, :, x);
cd frames;
% % % exporting the frames
imwrite(Vid, Strc);
cd ..
end
3. Load your trained detector and save the bounding boxes to a variable.
detector = yolov3ObjectDetector("tiny-yolov3-coco"); % load your trained detector here
a = cell(1,frames+1);
for i = 1:frames
z = "PATH_NAME\frames\"+string(i)+".jpg";
m = imread(z);
[bboxes, scores] = detect(detector, m);
a{1,i} = bboxes;
annotatedImage = insertShape(m, 'Rectangle', a{i});
figure
imshow(annotatedImage);
end
Hope this helps!

Community Treasure Hunt

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

Start Hunting!