MATLAB Answers

0

How to verify if the face has been detected ?

My code is:
while hasFrame(videoFileReader)
videoFrame = readFrame(videoFileReader);
bbox = faceDetector(videoFrame); %detect the face
videoFrame = insertShape(videoFrame, 'Rectangle', bbox); % place a box around the face
%% i want to add an IF statment to verify first if bbox is true !
%crop the rounded face and save it
FacCrop=imcrop(videoFrame,bbox);
fname = sprintf('FaceCropped_%d.jpg',i);
fpath = fullfile('E:', fname);
imwrite(FacCrop, fpath);
i=i+1;
end
  • but since there's some frames where the face can't be detected, that means no bbox, so it returns an error in the imcrop function and won't finnish the rest of the video !
Error using images.internal.imageDisplayParsePVPairs (line 125)
Invalid input arguments.
Error in images.internal.imageDisplayParseInputs (line 69)
[common_args,specific_args] = images.internal.imageDisplayParsePVPairs(varargin{:});
Error in imshow (line 245)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in imcrop>parseInputs (line 252)
imshow(a,cm);
Error in imcrop (line 93)
[x,y,a,cm,spatial_rect,h_image,placement_cancelled] = parseInputs(varargin{:});
Error in FaceTracking (line 19)
FacCrop=imcrop(videoFrame,bbox);
So what i want, is first to verify if the face has been detected or not in bbox, to call the crop function, else, skipp the crop step.
I hope you get my problem, and I hope I find a solution from you guys.
Thanks in advance.

  1 Comment

Also Iam a beginner so take it easy on me 😅

Sign in to comment.

1 Answer

Answer by Filipe Fernandes on 24 Jun 2019
 Accepted Answer

Hello, you could try use
while hasFrame(videoFileReader) videoFrame = readFrame(videoFileReader);
bbox = faceDetector(videoFrame); %detect the face
if ~isempty(bbox)
videoFrame = insertShape(videoFrame, 'Rectangle', bbox); % place a box around the face
%crop the rounded face and save it
FacCrop=imcrop(videoFrame,bbox);
fname = sprintf('FaceCropped_%d.jpg',i);
fpath = fullfile('E:', fname);
imwrite(FacCrop, fpath);
end
i=i+1;
end

  1 Comment

thank you Filipe, I did the same code ...

Sign in to comment.