counting number of bounding boxes at live webcam
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
close all;
clear all;
clc;
% Loading the trained network
load('AlexNetMaskDetect.mat');
net = myNet; clear myNet
% Some preliminaries
if ~exist('camera')
camera = webcam; % Connect to the camera
end
cr_label = 'Face Mask Detection';
img_size = [227,227];
% Loading the face detector
FDetect = vision.CascadeObjectDetector;
while true
picture = camera.snapshot;
orig_picture = picture;
BB = step(FDetect, picture);
if size(BB,1) >= 1 % if a face is found
for faces_iter = 1:size(BB,1) % for total number of faces found in the camera
picture_cropped = imcrop(orig_picture,BB(faces_iter,:));
picture_resized = imresize(picture_cropped,img_size);
label = classify(net, picture_resized); % find out if it has mask or not
label_text = char(label);
text_color = 'green';
line_color = 'g';
if strcmp(label_text, 'Without Mask')
text_color = 'red';
line_color = 'r';
end
picture = insertText(picture,BB(faces_iter,1:2),label_text,'FontSize',16,'BoxColor',...
'white','BoxOpacity',0,'TextColor',text_color); % text label around each face
image(picture); % show the picture
axis off;
end
for faces_iter = 1:size(BB,1) % bounding boxes around each face
rectangle('Position', BB(faces_iter,:), 'Linewidth',2,'LineStyle','-','EdgeColor',line_color);
end
label = strcat(cr_label);
else % if no face found
image(picture); % show the picture
picture = imresize(picture,img_size);
axis off
label = strcat(cr_label);
end
label_cell{1} = label;
title(label_cell, 'FontSize', 24); % show the label
drawnow;
numWithoutMask = size(BB, 1);
picture = insertText(picture, [10 10], numWithoutMask, 'BoxOpacity', 1, ...
'FontSize', 14);
figure; imshow(picture);
step(FDetect, picture); % display the results
end
I want to count the number of labels without mask and the number of labels with mask on the real-time webcam, and display each on the screen. But when I do the code as above, the figure window keeps appearing infinitely, the labels cannot be distinguished, and the two are merged to appear. What should I do?
plzzzzzzzzzzzㅠㅠㅠ
4 Kommentare
Anubhav Pal
am 14 Apr. 2022
Can anyone tell me how to count the number of bounding boxes detected in a video ?
Antworten (1)
Walter Roberson
am 11 Dez. 2021
Do not do any image() or imshow() inside the
if size(BB,1) >= 1 % if a face is found
Instead, before that statement, copy picture to annotated_picture, and
annotated_picture = insertText(annotated_picture,BB(faces_iter,1:2),label_text,'FontSize',16,'BoxColor',...
'white','BoxOpacity',0,'TextColor',text_color); % text label around each face
without displaying the image.
Do not rectangle() at that point.
At the statement
if strcmp(label_text, 'Without Mask')
when the condition is true, increment a counter of the number without mask; put in an else that increments the with_mask count.
Remove the whole
else % if no face found
branch -- none of those statements are needed.
Then, after the if is done, use insertText() on annotated_picture, inserting labels for the count of number with mask, and the count for the number without mask.
Then image(annotated_image).
Now do the loop to rectangle() the boxes into place.
... Though what would be even better is if you did not use rectangle() at all, and instead used insertShape() to draw in the rectangles into annotated_image .
If you follow these instructions, you will have one image() call, and it will be displaying the annotated_image with all the text in it, including the mask / no-mask counts.
5 Kommentare
Siehe auch
Kategorien
Mehr zu Image Data Workflows finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!