0% found this document useful (0 votes)
9 views11 pages

App A

The document contains MATLAB source code for a real-time face recognition system designed for a door lock application. It includes functions for initializing the GUI, loading a database of faces, detecting and tracking faces using a webcam, and verifying identities based on facial recognition. The system utilizes Eigenfaces for recognition and can control a door lock based on the recognition results.

Uploaded by

May Zaw Hlaing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

App A

The document contains MATLAB source code for a real-time face recognition system designed for a door lock application. It includes functions for initializing the GUI, loading a database of faces, detecting and tracking faces using a webcam, and verifying identities based on facial recognition. The system utilizes Eigenfaces for recognition and can control a door lock based on the recognition results.

Uploaded by

May Zaw Hlaing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

APPENDIX A

MATLAB SOURCE CODE FOR OVERALL FACE RECOGNITION

Face Recognition Function


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

% Last Modified by GUIDE v2.5 20-Aug-2018 04:13:06

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @RealTimeFaceDoorLock_OpeningFcn, ...
'gui_OutputFcn', @RealTimeFaceDoorLock_OutputFcn, ...
62

'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 RealTimeFaceDoorLock is made visible.


function RealTimeFaceDoorLock_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 RealTimeFaceDoorLock (see VARARGIN)

% delete(instrfindall);
% serialPorts = instrhwinfo ('serial') ;
% nPorts = length(serialPorts.SerialPorts);
% set(handles.SerialCOM, 'String','Com 3');
% set(handles.SerialCOM,'Value',3);

% Choose default command line output for RealTimeFaceDoorLock


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

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


% uiwait(handles.figure1);

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

function DB_EText_Callback(hObject, eventdata, handles)


% hObject handle to DB_EText (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of DB_EText as text


% str2double(get(hObject,'String')) returns contents of DB_EText as a double

% --- Executes during object creation, after setting all properties.


function DB_EText_CreateFcn(hObject, eventdata, handles)
% hObject handle to DB_EText (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on selection change in AP_List.


function AP_List_Callback(hObject, eventdata, handles)
% hObject handle to AP_List (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns AP_List contents as cell


array
% contents{get(hObject,'Value')} returns selected item from AP_List
val=get(handles.AP_List,'Value');
name=get(handles.AP_List,'String');

% --- Executes during object creation, after setting all properties.


function AP_List_CreateFcn(hObject, eventdata, handles)
% hObject handle to AP_List (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
64

% --- Executes on button press in LoadDB.


function LoadDB_Callback(hObject, eventdata, handles)
% hObject handle to LoadDB (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global m A Eigenfaces;global datapath;
name = {};
set(handles.AP_List, 'String', name);
datapath = uigetdir('select database folder');
for word_folder = struct2cell(dir(datapath))
if char(word_folder(1))==char('.')
else
name(end+1)=word_folder(1);
end
end
% Updates the listbox to match the current workspace
set(handles.AP_List, 'String',name);
set(handles.DB_EText, 'String',datapath);

T = CreateDatabase(strcat(datapath,'\'));
[m, A, Eigenfaces] = EigenfaceCore(T);

% --- Executes on selection change in SerialCOM.


function SerialCOM_Callback(hObject, eventdata, handles)
% hObject handle to SerialCOM (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns SerialCOM contents as cell


array
% contents{get(hObject,'Value')} returns selected item from SerialCOM

% --- Executes during object creation, after setting all properties.


function SerialCOM_CreateFcn(hObject, eventdata, handles)
% hObject handle to SerialCOM (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
65

% --- Executes on button press in CamOnPB.


function CamOnPB_Callback(hObject, eventdata, handles)
% hObject handle to CamOnPB (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

global KeepCameraRunning Flag_v vidcam;


KeepCameraRunning = false;
Flag_v = false;
vidcam = webcam;
videoFrame = snapshot(vidcam);
imshow(videoFrame)

% --- Executes on button press in Track_PB.


function Track_PB_Callback(hObject, eventdata, handles)
% hObject handle to Track_PB (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global KeepCameraRunning Flag_v;
global vidcam;
global videoFrame faceDetector pointTracker;
global bbox D_Image framergb;

% Create the face detector object.


faceDetector = vision.CascadeObjectDetector();
% Create the point tracker object.
pointTracker = vision.PointTracker('MaxBidirectionalError', 2);
numPts = 0;
KeepCameraRunning = true;
while KeepCameraRunning
if(Flag_v == false)
videoFrame = snapshot(vidcam);
framergb = videoFrame;
videoFrameGray = rgb2gray(videoFrame);

if numPts < 10
% Detection mode.
bbox = faceDetector.step(videoFrameGray);

if ~isempty(bbox)
% Find corner points inside the detected region.
points = detectMinEigenFeatures(videoFrameGray, 'ROI', bbox(1, :));

% Re-initialize the point tracker.


xyPoints = points.Location;
numPts = size(xyPoints,1);
release(pointTracker);
66

initialize(pointTracker, xyPoints, videoFrameGray);

% Save a copy of the points.


oldPoints = xyPoints;

% Convert the rectangle represented as [x, y, w, h] into an


% M-by-2 matrix of [x,y] coordinates of the four corners. This
% is needed to be able to transform the bounding box to display
% the orientation of the face.
bboxPoints = bbox2points(bbox(1, :));

% Convert the box corners into the [x1 y1 x2 y2 x3 y3 x4 y4]


% format required by insertShape.
bboxPolygon = reshape(bboxPoints', 1, []);

% Display a bounding box around the detected face.


D_Image = insertShape(videoFrame, 'Polygon', bboxPolygon, 'LineWidth',
3);

% Display detected corners.


D_Image = insertMarker(D_Image, xyPoints, '+', 'Color', 'white');
end

else
% Tracking mode.
[xyPoints, isFound] = step(pointTracker, videoFrameGray);
visiblePoints = xyPoints(isFound, :);
oldInliers = oldPoints(isFound, :);

numPts = size(visiblePoints, 1);

if numPts >= 10
% Estimate the geometric transformation between the old points
% and the new points.
[xform, oldInliers, visiblePoints] = estimateGeometricTransform(...
oldInliers, visiblePoints, 'similarity', 'MaxDistance', 4);

% Apply the transformation to the bounding box.


bboxPoints = transformPointsForward(xform, bboxPoints);

% Convert the box corners into the [x1 y1 x2 y2 x3 y3 x4 y4]


% format required by insertShape.
bboxPolygon = reshape(bboxPoints', 1, []);

% Display a bounding box around the face being tracked.


D_Image = insertShape(videoFrame, 'Polygon', bboxPolygon, 'LineWidth',
3);

% Display tracked points.


% D_Image = insertMarker(D_Image, visiblePoints, '+', 'Color', 'white');
67

% Reset the points.


oldPoints = visiblePoints;
setPoints(pointTracker, oldPoints);
end

end

% Display the annotated video frame using the video player object.
imshow(D_Image)
if Flag_v
KeepCameraRunning = false;
break;
end
else
KeepCameraRunning = false;
release(pointTracker);
release(faceDetector);
break;
end

end
KeepCameraRunning = false;

% --- Executes on button press in DetectPB.


function DetectPB_Callback(hObject, eventdata, handles)
% hObject handle to DetectPB (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global KeepCameraRunning Flag_v;
global framergb;
global bbox IFace;

KeepCameraRunning = false;
Flag_v = true;

% Detection mode
%% Annotate detected faces.
IFace = insertObjectAnnotation(framergb, 'rectangle', bbox, 'Detected Face');
imshow(IFace);

% --- Executes on button press in ShowPB.


function ShowPB_Callback(hObject, eventdata, handles)
% hObject handle to ShowPB (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global KeepCameraRunning Flag_v;
68

global IFace CropFace;


global bbox;

KeepCameraRunning = false;
Flag_v = true;
if Flag_v && (KeepCameraRunning == false)
%% Crop detected faces.
CropFace = imcrop(IFace, bbox);
imshow(CropFace); title('Cropped Face');
end

% --- Executes on button press in Verify_PB.


function Verify_PB_Callback(hObject, eventdata, handles)
% hObject handle to Verify_PB (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

global m A Eigenfaces;
global framergb KeepCameraRunning serConn;
KeepCameraRunning = false;
delete(instrfindall);
set(handles.SerialCOM, 'String','Com 4');
serConn = serial('com4', 'TimeOut', 1,'BaudRate',300);
format compact
fopen(serConn);

% Create the face detector object.


faceDetector = vision.CascadeObjectDetector();
bboxes = [];

%% Detect faces.
bboxes = step(faceDetector, framergb);
Face = imcrop(framergb,bboxes);
CF = imresize(Face, [200,200]);
imshow(CF),

TestImage = CF;
% threshould = 0.09;
threshould=str2double(get(handles.Th_EText,'String'));

%%%%%%%%%%%%%%%%%%
ProjectedImages = [];
Train_Number = size(Eigenfaces,2);
for i = 1 : Train_Number
temp = Eigenfaces'*A(:,i); % Projection of centered images into facespace
ProjectedImages = [ProjectedImages temp];
end
69

%%%%%%%%%%%%%%%%%%%%%%%% Extracting the PCA features from


test image
InputImage = TestImage;
temp = InputImage(:,:,1);

[irow, icol] = size(temp);


InImage = reshape(temp',irow*icol,1);
Difference = double(InImage)- m; % Centered test image
ProjectedTestImage = Eigenfaces'*Difference; % Test image feature vector

%%%%%%%%%%%%%%%%%%%%%%%% Calculating Euclidean distances


% Euclidean distances between the projected test image and the projection
% of all centered training images are calculated. Test image is
% supposed to have minimum distance with its corresponding image in the
% training database.

Euc_dist = [];
for i = 1 : Train_Number
q = ProjectedImages(:,i);
temp = ( norm( ProjectedTestImage - q ) )^2;
Euc_dist = [Euc_dist temp];
end

maximum= max( Euc_dist);

Euc_dist = Euc_dist/maximum;

[Euc_dist_min , Recognized_index] = min(Euc_dist);

if Euc_dist_min >= threshould


% if Euc_dist_min <= threshould
index =0;
set(handles.Recog_EText,'String','Unknown Person: Lock the Door!')
fclose(serConn);

else
index=Recognized_index;
set(handles.Recog_EText,'String','Known Person: Unlock the Door!')
fprintf(serConn, 1);
fclose(serConn);
end
display(Euc_dist_min)
display(index)
display(Recognized_index);

% --- Executes on button press in StopPB.


function StopPB_Callback(hObject, eventdata, handles)
70

% hObject handle to StopPB (see GCBO)


% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and us er data (see GUIDATA)
global KeepCameraRunning Flag_v serConn;

KeepCameraRunning = false;
Flag_v = false;
delete(serConn);
clear('vidcam')
clear all

function Th_EText_Callback(hObject, eventdata, handles)


% hObject handle to Th_EText (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Th_EText as text


% str2double(get(hObject,'String')) returns contents of Th_EText as a double

% --- Executes during object creation, after setting all properties.


function Th_EText_CreateFcn(hObject, eventdata, handles)
% hObject handle to Th_EText (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function Recog_EText_Callback(hObject, eventdata, handles)


% hObject handle to Recog_EText (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Recog_EText as text


% str2double(get(hObject,'String')) returns contents of Recog_EText as a double

% --- Executes during object creation, after setting all properties.


function Recog_EText_CreateFcn(hObject, eventdata, handles)
% hObject handle to Recog_EText (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
71

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

You might also like