counting number of bounding boxes at live webcam

3 Ansichten (letzte 30 Tage)
주영 이
주영 이 am 10 Dez. 2021
Kommentiert: Anubhav Pal am 14 Apr. 2022
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
주영 이
주영 이 am 11 Dez. 2021
And how to count each type of bounding box? Please.......
Anubhav Pal
Anubhav Pal am 14 Apr. 2022
Can anyone tell me how to count the number of bounding boxes detected in a video ?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
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
Walter Roberson
Walter Roberson am 11 Dez. 2021
annotated_picture = picture;
주영 이
주영 이 am 11 Dez. 2021
% Loading the trained network
load('AlexNetMaskDetect.mat');
net = myNet; clear myNet
% Some preliminaries
camera = webcam('e2eSoft iVCam') ; % Connect to the camera
cr_label = 'Face Mask Detection';
img_size = [227,227];
% Loading the face detector
FDetect = vision.CascadeObjectDetector;
while true
picture = snapshot(camera);
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';
n= size (BB,1); %bbox is the bounding box & 1 represents the no. of rows in bounding box
str_n= num2str(n);
str= strcat( 'number of Without Masks are ', str_n);
end
axis off;
end
for faces_iter = 1:size(BB,1) % bounding boxes around each face
end
label = strcat(cr_label);
else % if no face found
picture = imresize(picture,img_size);
axis off
label = strcat(cr_label);
n= size (BB,1); %bbox is the bounding box & 1 represents the no. of rows in bounding box
str_n= num2str(n);
str= strcat( 'number of With masks are ', str_n);
end
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
image(annotated_picture)
rectangle('Position', BB(faces_iter,:), 'Linewidth',2,'LineStyle','-','EdgeColor',line_color);
label_cell{1} = label;
title(label_cell, 'FontSize', 24); % show the label
drawnow;
end
I change the code. but error is occured. in annotated_picture

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by