saving classified 7 facial expressions into 7 folders.

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.

 Accepted 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)

8 Comments

I am getting this error
Error using mkdir
The filename, directory name, or volume label syntax is incorrect.
Error in FacialExpRecognition (line 50)
mkdir(folder);
OKay Dear, There was problem with the path of directory. It is solved now. Thank you
the images are overwriting in the folders. how to solve this ?
You can write them into subfolders of the folder where the image lives if you want
[~, thisFolder, ext] = fileparts(ds.Filenames{k}); % Or whatever.
folder = fullfile(thisFolder, thisClass);
I have multiple frams in the folder, each frame has multiple faces, the code is cropping the faces and saving them in the created folders, but it is overwritting them. The all images are not getting saved. How to solve this please.
How would you like to name them? If no preference, then how about just using an 8 digit random number? If not, then what protocol would you like to use? i was just going to use the name with the face number after it but apparently in different folders, you have some images with the same names, so suggest something.
Images are of different names. But I do not know why its over writting them. please help me.
It's difficult for me to help you when I don't have the directory structure that you're using. Make it easy for me to help you by zipping up a folder of small images with the names that they have and explain where you'd like the sub-images to be saved. Have several, so you will probably have to resize the images to get the zip file to be smaller than the (I think) 5 MB attachment limit.

Sign in to comment.

More Answers (1)

hi, i have to do same work. would you -please share the code of face detection and cropping? i shall really be thankful to you if you provide me code. please reply.

1 Comment

When reading your request, it would not be uncommon for people to be left with the impression that you are trying to avoid having to program the most difficult steps of your homework assignment.

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!