Üye
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 15-Feb-2017 00:51:51
% 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('.');
addpath(addr);
set(handles.text5, 'String', 'Training the Database !');
dos('attrib -h -r -s /S .\train_images\Thumbs.db');
dos('del -h -r -s /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]);
pause(0.01)
[feature] = hog_feature_vector(face);
fea=[fea;feature];
group=[group count];
imshow(face,'Parent',handles.axes1);
% elseif count == 3
%
% face=imresize(face,[100 100]);
% pause(0.01)
%
% [feature] = hog_feature_vector(face);
% fea=[fea;feature];
% group=[group count];
%
% imshow(face,'Parent',handles.axes1);
end
end
end
set(handles.text5, 'String', 'Training Complete !');
warndlg('Training Complete !');
save face_database fea group
% --- 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,3),'Parent',handles.axes1); % 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 Face !');
faceDetector = vision.CascadeObjectDetector();
im = I;
gr = rgb2gray(im);
gr = imadjust(gr);
gr = histeq(gr);
imshow(im,'Parent',handles.axes1);
bbox = step(faceDetector, gr);
if numel(bbox)== 4
testface=imcrop(gr,bbox);
testface=imresize(testface,[100 100]);
pause(0.01)
[test_feature] = hog_feature_vector(testface);
imshow(testface,'Parent',handles.axes1);
else
warndlg('!! No Valid Face Found !!');
return;
end
% c=knnclassify(test_feature,fea,group);
% c = multisvm(fea,group,test_feature);
%% check between 1st and 3rd group, if result is 3rd then check for 2nd and 3rd
% new_gr_index = [find(group == 1),find(group == 4)];
% new_train_group = group(new_gr_index);
% new_train_set = fea(new_gr_index,:);
%
% models = svmtrain(new_train_set,new_train_group);
% result = svmclassify(models,test_feature);
%
% % if first person not recognized, then try for second
%
% if result == 4 % none is recognised
% new_gr_index = [find(group == 2),find(group == 4)]; % check for 2nd and 3rd
% new_train_group = group(new_gr_index);
% new_train_set = fea(new_gr_index,:);
%
% models = svmtrain(new_train_set,new_train_group);
% result = svmclassify(models,test_feature);
%
% if result == 4 % not recognized
%
% new_gr_index = [find(group == 3),find(group == 4)]; % check for 2nd and 3rd
% new_train_group = group(new_gr_index);
% new_train_set = fea(new_gr_index,:);
%
% models = svmtrain(new_train_set,new_train_group);
% result = svmclassify(models,test_feature);
%
% if result == 4 % not recognized
% c = result; % not recognized
% else
% c = result; % then 3nd is recognized
% end
%
% else
% c = result; % 2nd is recognized
% end
%
% else
%
% c = result; % 1st is recognized
%
% end
%%
Mdl = fitcecoc(fea,group);
c = predict(Mdl,test_feature);
%% Serial Port setup and connection
Serial_port = serial('COM4', 'BaudRate', 9600);
fopen(Serial_port);
%%
if c==1
msgbox ('Hello 1');
% data = {'Varun',datestr(now,'yyyy-mm-dd HH:MM:SS')};
% logxldata(data);
fprintf(Serial_port,'1'); fclose(Serial_port);
end
if c==2
msgbox ('Hello 2');
% data = {'Abhishek',datestr(now,'yyyy-mm-dd HH:MM:SS')};
% logxldata(data);
fprintf(Serial_port,'2'); fclose(Serial_port);
end
if c==3
msgbox ('Hello 3');
% data = {'Mayank',datestr(now,'yyyy-mm-dd HH:MM:SS')};
% logxldata(data);
fprintf(Serial_port,'3'); fclose(Serial_port);
end
if c==4
msgbox ('Not Recognized !');
% data = {'Not Recognized !',datestr(now,'yyyy-mm-dd HH:MM:SS')};
% logxldata(data);
fprintf(Serial_port,'x'); fclose(Serial_port);
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 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;