You are on page 1of 5

function varargout = pengenalan_pola(varargin)

% FACERECOGNITIONTOOL M-file for FaceRecognitionTool.fig


% FACERECOGNITIONTOOL, by itself, creates a new FACERECOGNITIONTOOL or raises
the existing
% singleton*.
%
% H = FACERECOGNITIONTOOL returns the handle to a new FACERECOGNITIONTOOL or
the handle to
% the existing singleton*.
%
% FACERECOGNITIONTOOL('CALLBACK',hObject,eventData,handles,...) calls the
local
% function named CALLBACK in FACERECOGNITIONTOOL.M with the given input
arguments.
%
% FACERECOGNITIONTOOL('Property','Value',...) creates a new
FACERECOGNITIONTOOL or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before FaceRecognitionTool_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to FaceRecognitionTool_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 FaceRecognitionTool

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @pengenalan_pola_OpeningFcn, ...
'gui_OutputFcn', @pengenalan_pola_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 FaceRecognitionTool is made visible.


function pengenalan_pola_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 FaceRecognitionTool (see VARARGIN)
% Choose default command line output for FaceRecognitionTool
set(handles.edit2,'visible','off');
set(handles.text4,'visible','off');
set(handles.text5,'visible','off');
set(handles.togglebutton10,'visible','off');
set(handles.togglebutton11,'visible','off');

axes(handles.axes4)
cla
axes(handles.axes3)
cla
handles.output = hObject;
% addpath(genpath([pwd '\']));
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes FaceRecognitionTool wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = pengenalan_pola_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;

function pushbutton4_Callback(hObject, eventdata, handles)


global datapath
datapath = uigetdir('D:\ikan\train');
database=dir(fullfile(datapath,'*.jpg'));
h = waitbar(0, 'Reading Test Image..');
length(database)
for k = 1:length(database)
waitbar(k/length(datapath));
end
close(h)

function pushbutton5_Callback(hObject, eventdata, handles)


global datapath
testpath = uigetdir('D:\ikan\test0','select path of test images');
testfiles=dir(fullfile(testpath,'*.jpg'));
IsTrue=0;
gambar=0;
length(testfiles)
set(handles.text5,'visible','on');
for k=1:length(testfiles)
gambar = gambar+1
TestImage = strcat(testpath,'\',int2str(gambar),'.jpg');
recog_img = facerecog(datapath,TestImage);
selected_img = strcat(datapath,'\',recog_img);
select_img = imread(selected_img);

axes(handles.axes3)
cla
axes(handles.axes4)
cla

axes(handles.axes3)
imshow(TestImage);
title('Test Image','Color','Black','FontSize',15);
axes(handles.axes4)
imshow(selected_img);
title('Detected','Color','red','FontSize',15);
set(handles.togglebutton10,'visible','on');
set(handles.togglebutton11,'visible','on');
while 1
pause(eps);
if get(handles.togglebutton10,'value')==1
IsTrue=IsTrue+1;
set(handles.togglebutton10,'value',0)
break;
elseif get(handles.togglebutton11,'value')==1
set(handles.togglebutton11,'value',0)
break;
end
end
set(handles.togglebutton10,'visible','off');
set(handles.togglebutton11,'visible','off');

end
eta = (IsTrue/gambar)*100;
set(handles.edit2,'visible','on');
set(handles.text4,'visible','on');
set(handles.edit2,'String',[num2str(eta)]);
drawnow;
set(handles.text5,'visible','off');

function pushbutton6_Callback(hObject, eventdata, handles)


global datapath
[Test_File Test_File_Path] = uigetfile('*.jpg;*.pgm;*.png;*.tif','Select a Test
Image');
test_image = [Test_File_Path Test_File];
recog_img = facerecog(datapath,test_image);
selected_img = strcat(datapath,'\',recog_img);
select_img = imread(selected_img);

axes(handles.axes3)
cla
axes(handles.axes4)
cla

axes(handles.axes3)
imshow(test_image);
title('Test Image','Color','Black','FontSize',15);
axes(handles.axes4)
imshow(selected_img);
title('Detected','Color','red','FontSize',15);

function [recognized_img]=facerecog(datapath,testimg)

% number of all images in the training Tdbbase


D = dir(datapath); % D is a Lx1 structure with 4 fields as: name,date,byte,isdir
of all L files present in the directory 'datapath'
imgcount = 0;
for i=1 : size(D,1)
if not(strcmp(D(i).name,'.')|strcmp(D(i).name,'..')|
strcmp(D(i).name,'Thumbs.db'))
imgcount = imgcount + 1; % number of all images in the training database
end
end

% 1) Data-base construction
X = [];
for i = 1 : imgcount
str = strcat(datapath,'\',int2str(i),'.jpg');
img = imread(str);
[r c] = size(img);
temp = reshape(img',r*c,1); %Reshaping 2D images into 1D image vectors

X = [X temp];
end

% 2) Finding mean-image
m = mean(X,2);
imgcount = size(X,2);

% 3) Subtracting mean-image from Train-Database


A = [];
for i=1 : imgcount
temp = double(X(:,i)) - m;
A = [A temp];
end

% 4) Finding co-variance matrix


L= A' * A;

% 5) Finding eigen values and eigen vectors


[V,D]=eig(L); % V : eigenvector matrix D : eigenvalue matrix

% 6) Selecting eigen-values w.r.t eigen-vectors


L_eig_vec = [];
for i = 1 : size(V,2)
if( D(i,i) > 1 )
L_eig_vec = [L_eig_vec V(:,i)];
end
end

% 7) Projection image matrix ( PCA-output )


eigenfaces = A * L_eig_vec;

% 8) Miniature database
projectimg = [ ]; % projected image vector matrix
for i = 1 : size(eigenfaces,2)
temp = eigenfaces' * A(:,i);
projectimg = [projectimg temp];
end

% Testing:
% 9) Finding test image projecttion
test_image = imread(testimg);
test_image = test_image(:,:,1);
[r c] = size(test_image);
temp = reshape(test_image',r*c,1); % creating (MxN)x1 image vector from the 2D
image
temp = double(temp)-m; % mean subtracted vector
projtestimg = eigenfaces'*temp; % projection of test image onto the space

% 10) Finding euclidean distance


euclide_dist = [ ];
for i=1 : size(eigenfaces,2)
temp = (norm(projtestimg-projectimg(:,i)))^2;
euclide_dist = [euclide_dist temp];
end

% 11) Finding equivalent image path


[euclide_dist_min recognized_index] = min(euclide_dist);
recognized_img = strcat(int2str(recognized_index),'.jpg');

% 12) Displaying result


fprintf('gambar yang cocok : '), disp (recognized_img);
fprintf('euclidean distance : '),disp (min(euclide_dist));

% --- Executes on button press in togglebutton10.


function togglebutton10_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.togglebutton10,'value',1)

% --- Executes on button press in togglebutton11.


function togglebutton11_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.togglebutton11,'value',1);

You might also like