MATLAB: Saving classified 7 facial expressions into 7 folders.

classificationface detectionfer

I am trying to detect faces in the image and label the images with predicted facial expressions, such as angry, disgust, happy, fear, surprise and neutral, the I want to crop these predicted facial expressions and save them into folders, as like, the face which is labeled angry should be cropped and saved in angry folder, the face which is labeled happy should be cropped and saved in the happy folder, and so on. I have done till face detection and facial expression prediction, I need help in the cropping and saving task.

Best Answer

Inside the for loop, but at the bottom of the for loop, do this:
% Get predicted class (facial expression) of this sub-image.
thisClass = label_str{i};
% Get output folder for this particular class.
folder = fullfile(location, thisClass);
if ~isfolder(folder)
% If folder does not exist, create it.
mkdir(folder);
end
% Save sub-image into this folder.
thisFileName = sprintf('%d.png', i); % Give it a number as the filename.
thisFileName = fullfile(folder, thisFileName); % Prepend the folder to get the full filename.
fprintf('Writing %s.\n', thisFileName);
% Do the actual write of the sub-image to the folder.
imwrite(classFace, thisFileName)