face detection of multiple faces inside one image

36 visualizzazioni (ultimi 30 giorni)
I tried the following code, It performs the face dection and cropping process on only one image(contain only one face) and only crope one face . I want to perform face detection and cropping process on multiple faceses inside an image .
location = 'C:\Users\TOSHIBA\Desktop\FACE'; % folder in which your images exists
ds = imageDatastore(location) % Creates a datastore for all images in your folder
% Loop through the datastore, read and display each image in its own window.
while hasdata(ds)
img = read(ds) ; % read image from datastore
figure, imshow(img); % creates a new window for each image
end
%figure(1);
%imshow(img);
FaceDetect = vision.CascadeObjectDetector;
FaceDetect.MergeThreshold = 7 ;
BB = step(FaceDetect, img);
figure(2);
imshow(img);
for i = 1 : size(BB,1)
rectangle('Position', BB(i,:), 'LineWidth', 3, 'LineStyle', '-', 'EdgeColor', 'r');
end
for i = 1 : size(BB, 1)
J = imcrop(img, BB(i, :));
I = rgb2gray(J); % change to gray scale
resizedimage = imresize(I, [112 92]);
figure(3);
subplot(1, 1, i);
imshow(resizedimage); % show gray scale
imsave
end

Risposte (1)

Subhadeep Koley
Subhadeep Koley il 31 Ott 2019
You can step() the instantiated objects on the image of interest. The CascadeObjectDetector returns the bounding boxes of the detected objects, and the ShapeInserter delimits them in the image.
Use the code below to detect and crop multiple faces from an image.
% Initialize the detector
faceDetector = vision.CascadeObjectDetector;
shapeInserter = vision.ShapeInserter('BorderColor','Custom','CustomBorderColor',[0 255 0]);
% Read the image
I = imread('solvey.jpg'); % Read your image here
imshow(I);
bbox = step(faceDetector, I);
% Draw boxes around detected faces and display results
I_faces = step(shapeInserter, I, int32(bbox));
figure;imshow(I_faces), title('Detected faces');
% Cropping individual faces
for i=1:size(bbox,1)
face = imresize(imcrop(I,bbox(i,:)),[60 60]);
figure;imshow(face,[]);
end
solvey_faces.png

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by