isolating bounding boxes from a video using detector

1 次查看(过去 30 天)
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 个评论
Shirshak
Shirshak 2023-1-27
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 ?

请先登录,再进行评论。

回答(1 个)

Ajay Gajulapally
Ajay Gajulapally 2023-3-2
编辑:Ajay Gajulapally 2023-3-2
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!

Translated by