Demonstration of Facial Expression Recognition Using LBP Feature
%Model Training:
clc;
clear all;
close all;
warning off;
imds=imageDatastore('Mj','IncludeSubFolders',true,'LabelSource','foldernames');
trainingFeatures=[];
trainingLabels=imds.Labels;      
for i = 1:numel(imds.Files)        
% Read images using a for loop
    img = readimage(imds,i);
   
trainingFeatures(i,:)=extractLBPFeatures(rgb2gray(img));
end
Classifier =fitcecoc(trainingFeatures,trainingLabels);
save Classifier Classifier
 
%Testing:
clc
clear all
close all
warning off;
load Classifier;
cao=webcam;
faceDetector=vision.CascadeObjectDetector;
while true
    e=cao.snapshot;
    bboxes
=step(faceDetector,e);
    if(~isempty(bboxes))
    es=imcrop(e,bboxes);
    es=imresize(es,[128
128]);
    es=rgb2gray(es);
    [Features] =
extractLBPFeatures(es);
   
PredictedClass=predict(Classifier,Features);
   
PredictedClass=char(PredictedClass);
   
imshow(e),title(PredictedClass);
    ax=gca;
   
ax.TitleFontSizeMultiplier=1.5;
    pause(0.1);
    else
      imshow(e);
      ax=gca;
      title('Face Not
Detected');
      
ax.TitleFontSizeMultiplier=2;
      pause(0.1);
    end
end
clc
clear all
close all
warning off;
cao=webcam;
faceDetector=vision.CascadeObjectDetector;
c=150;
temp=0;
while true
    e=cao.snapshot;
    bboxes
=step(faceDetector,e);
    if(sum(sum(bboxes))~=0)
    if(temp>=c)
        break;
    else
   
es=imcrop(e,bboxes(1,:));
    es=imresize(es,[128
128]);
   
filename=strcat(num2str(temp),'.bmp');
    imwrite(es,filename);
    temp=temp+1;
    imshow(es);
    drawnow;
    end
    else
        imshow(e);
        drawnow;
    end
end
No comments