Face Expression Recognition

Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Üye
Katılım
5 Şub 2019
Mesajlar
20
Tepki puanı
10
Yaş
36
7 HİZMET YILI
MATLAB:
function varargout = main_GUI(varargin)
% MAIN_GUI MATLAB code for main_GUI.fig
%      MAIN_GUI, by itself, creates a new MAIN_GUI or raises the existing
%      singleton*.
%
%      H = MAIN_GUI returns the handle to a new MAIN_GUI or the handle to
%      the existing singleton*.
%
%      MAIN_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MAIN_GUI.M with the given input arguments.
%
%      MAIN_GUI('Property','Value',...) creates a new MAIN_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before main_GUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to main_GUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help main_GUI

% Last Modified by GUIDE v2.5 22-May-2018 16:35:25

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @main_GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @main_GUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before main_GUI is made visible.
function main_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to main_GUI (see VARARGIN)

% Choose default command line output for main_GUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes main_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = main_GUI_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in Train_images.
function Train_images_Callback(hObject, eventdata, handles)
% hObject    handle to Train_images (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

fea=[];
group=[];

addr = genpath(pwd);
addpath(addr);

url = '.\train_images';
imds = imageDatastore(url,'LabelSource','foldernames','IncludeSubfolders',true);
labels = unique(imds.Labels);

set(handles.text5, 'String', 'Training the Database !');

dos('attrib -h -r -s /S .\train_images\Thumbs.db');
dos('del /S .\train_images\Thumbs.db');

folder=dir('.\train_images');
count=0;

faceDetector = vision.CascadeObjectDetector();


%%

for mn=3:length(folder)
    count=count+1;
    address=strcat('.\train_images\',folder(mn).name);

    files=dir(address);
    num=numel(files);

    for i=3:num
        str=strcat('.\train_images\',folder(mn).name,'\',files(i).name);
        
        msg_str = sprintf('Processing Folder %d, File %d',count,i-2);
        set(handles.text5, 'String', msg_str);
        
        
        im=imread(str);
        im = imresize(im,[500,NaN]);
        
        [r,c,p] = size(im);
        
        if p == 3
            gr = rgb2gray(im);
        else
            gr = im;
        end
        
        gr = imadjust(gr);
        gr = histeq(gr);
    
        
        bbox = step(faceDetector, gr);
        
        if numel(bbox)== 4
            face=imcrop(gr,bbox);
            face=imresize(face,[100 100]);
            
            
            [feature] = hog_feature_vector(face);
            fea=[fea;feature];
            group=[group count];
            
            imshow(face,'Parent',handles.axes1);
            drawnow
        
                        
        end
        
        
    end
end

set(handles.text5, 'String', 'Training Complete !');
warndlg('Training Complete !');


save face_database fea group labels




% --- Executes on button press in Start_Camera.
function Start_Camera_Callback(hObject, eventdata, handles)
% hObject    handle to Start_Camera (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

cla reset

vid = videoinput('winvideo', 1, 'YUY2_640x480');
src = getselectedsource(vid);
vid.FramesPerTrigger = Inf;
vid.ReturnedColorspace = 'rgb';

axes(handles.axes1);
h1 = image(zeros(480,640));         % create image object
axis ij;            % flip the image
preview(vid,h1)     % display webcam preview

set(handles.text5, 'String', 'Camera Started !');
handles.vid = vid;
guidata(hObject,handles);


% --- Executes on button press in Recognition.
function Recognition_Callback(hObject, eventdata, handles)
% hObject    handle to Recognition (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

load face_database
I = handles.I;

set(handles.text5, 'String', 'Recognizing Emotion !');

%%

im = I;
imshow(im,'Parent',handles.axes1);

[r,c,p] = size(im);
        
if p == 3
    gr = rgb2gray(im);
else
    gr = im;
end

gr = imadjust(gr);
gr = histeq(gr);

%% denoise image using Deep Neural Net-CNN

net = denoisingNetwork('DnCNN');
denoised_Image = denoiseImage(gr, net);

%%

faceDetector = vision.CascadeObjectDetector();
bbox = step(faceDetector, denoised_Image);


if numel(bbox)== 4
    testface=imcrop(gr,bbox);
    testface=imresize(testface,[100 100]);
    
    
    [test_feature] = hog_feature_vector(testface);

    imshow(testface,'Parent',handles.axes1);
    drawnow
    
else
    warndlg('!! No Valid Face Found !!');
    return;
end

%% Classification

    Mdl = fitcknn(fea,group);
    c = predict(Mdl,test_feature);


%%
if c==1
    msg = char(labels(c));
    msgbox (msg);
    system('angry.mp3')
end

if c==2
    msg = char(labels(c));
    msgbox (msg);
    system('disgust.mp3')
end

if c==3
    msg = char(labels(c));
    msgbox (msg);
    system('fear.mp3')
end

if c==4
    msg = char(labels(c));
    msgbox (msg);
    system('happy.mp3')
end

if c==5
    msg = char(labels(c));
    msgbox (msg);
    system('natural.mp3')
end

if c==6
    msg = char(labels(c));
    msgbox (msg);
    system('sad.mp3')
end


if c==7
    msg = char(labels(c));
    msgbox (msg);
    system('surprise.mp3')
end


% --- Executes on button press in Take_snapshot.
function Take_snapshot_Callback(hObject, eventdata, handles)
% hObject    handle to Take_snapshot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

vid = handles.vid;

I = getsnapshot(vid);
stop(vid);

axes(handles.axes1);
imshow(I);

handles.I = I;
guidata(hObject,handles);


% --- Executes on button press in Browse_Image_btn.
function Browse_Image_btn_Callback(hObject, eventdata, handles)
% hObject    handle to Browse_Image_btn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[f,p] = uigetfile('*.*');
filename = [p,f];

I = imread(filename);

axes(handles.axes1);
imshow(I);

handles.I = I;
guidata(hObject,handles);



% --- Executes on button press in Exit_program.
function Exit_program_Callback(hObject, eventdata, handles)
% hObject    handle to Exit_program (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close all;
 
Onaylı Üye
Katılım
28 May 2020
Mesajlar
50
Tepki puanı
0
Ödüller
4
6 HİZMET YILI
Yararlı paylaşım teşekkürler
 
Seçkin Üye
Katılım
20 Haz 2020
Mesajlar
298
Çözümler
2
Tepki puanı
3
Ödüller
3
Sosyal
5 HİZMET YILI
Human Face expression Recognition is one of the most powerful and challenging tasks in social communication
 
Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Üst