人脸识别门禁系统(三)
这篇文章主要分享系统的两个GUI界面.
% GUI1.m
%作者联系方式:360812049@qq.com
function varargout = GUI1(varargin)
% GUI1 MATLAB code for GUI1.fig
% GUI1, by itself, creates a new GUI1 or raises the existing
% singleton*.
%
% H = GUI1 returns the handle to a new GUI1 or the handle to
% the existing singleton*.
%
% GUI1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI1.M with the given input arguments.
%
% GUI1('Property','Value',...) creates a new GUI1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI1_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 GUI1
% Last Modified by GUIDE v2.5 18-Nov-2019 20:53:14
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI1_OpeningFcn, ...
'gui_OutputFcn', @GUI1_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 GUI1 is made visible.
function GUI1_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 GUI1 (see VARARGIN)
% Choose default command line output for GUI1
handles.output = hObject;
global k;%设置全局变量k,方便统计采集按钮的点击次数,即采集的张数
k=0;
set(handles.text6,'string','');
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI1_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 during object creation, after setting all properties.
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (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 listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
val=get(handles.listbox1,'value');%选择摄像头 自带摄像头或USB摄像头
global Camera;
switch val
case 2
Camera=1;
case 3
Camera=2;
end
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (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
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles) %打开摄像头
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global Camera;
global vid; %设置全局变量方便采集图像时使用
vid=videoinput('winvideo',Camera); %读取摄像头
usbvidRes=get(vid,'videoResolution'); %读像素
nBand=get(vid,'NumberOfBands'); %不清楚这个是干什么的
axes(handles.axes2); %设置坐标轴
hImage=imshow(zeros(usbvidRes(2),usbvidRes(1),nBand));%使得显示的坐标轴和图象一致
preview(vid,hImage); %显示实时照片
function pushbutton2_Callback(hObject, eventdata, handles) %抓取照片,采集图像
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global vid;
global k;
PersonName=get(handles.edit1,'string'); %输入采集人的姓名
if isempty(PersonName)==1; %姓名为空时点击采集不增加k,弹出对话框
k=0;
button=questdlg('请输入采集姓名!','采集模块','是','否','是');%内容,标题,选项,默认选项
else
k=k+1;%点击次数
frame=getsnapshot(vid);%截取某一时刻照片
if k==1
mkdir('D:\机器视觉\Original\',PersonName);%创建文件夹用来保存照片
end
imwrite(frame,strcat('D:\机器视觉\Original\',PersonName,'\',num2str(k),'.jpg'));%采集的图像写入
set(handles.text6,'string',num2str(k));%显示已采集的次数
axes(handles.axes3);
imshow(frame);%显示采集的照片
end
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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 button press in pushbutton2.
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles) %统一处理样本图像
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
train_face_detection;%调用train_face_detectiong
%批量处理采集的人脸,检测人脸,扣出人脸,拉伸像素
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles) %清除姓名和k值准备采集下一人
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.edit1,'string','');
global k;
k=0;
set(handles.text6,'string','');
axes(handles.axes3);
cla reset;
% --- Executes on selection change in listbox1.
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
delete(hObject);
% Hint: delete(hObject) closes the figure
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles) %退出系统按钮
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
button=questdlg('尚未进入识别模块,是否确定退出采集系统?','退出系统','是','否','是');%内容,标题,选项,默认选项
if strcmp(button,'是')
close;
end
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles) %打开识别模块
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close;%关闭GUI1
GUI2;%打开GUI2
% GUI2.m
% 作者联系方式:360812049@qq.com
function varargout = GUI2(varargin)
% GUI2 MATLAB code for GUI2.fig
% GUI2, by itself, creates a new GUI2 or raises the existing
% singleton*.
%
% H = GUI2 returns the handle to a new GUI2 or the handle to
% the existing singleton*.
%
% GUI2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI2.M with the given input arguments.
%
% GUI2('Property','Value',...) creates a new GUI2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI2_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 GUI2
% Last Modified by GUIDE v2.5 19-Nov-2019 10:09:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI2_OpeningFcn, ...
'gui_OutputFcn', @GUI2_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 GUI2 is made visible.
function GUI2_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 GUI2 (see VARARGIN)
% Choose default command line output for GUI2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI2_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles) %打开摄像头
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global Camera;
global vid;%设置全局变量方便下面采集时使用
vid=videoinput('winvideo',Camera); %读取摄像头
usbvidRes=get(vid,'videoResolution'); %读像素
nBand=get(vid,'NumberOfBands'); %不清楚这个是干什么的
axes(handles.axes1); %设置坐标轴
hImage=imshow(zeros(usbvidRes(2),usbvidRes(1),nBand));%使得显示的坐标轴和图象一致
preview(vid,hImage); %显示实时照片
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles) %采集图像
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global vid;
global frame;
frame=getsnapshot(vid);%截取某一时刻照片
% 使用采集图像不存盘的方法
% PersonName=get(handles.edit1,'string');%输入采集人的姓名
% mkdir('D:\机器视觉\Original\',PersonName);%创建文件夹用来保存照片
% imwrite(frame,strcat('D:\机器视觉\Original\',PersonName,'\',num2str(k),'.jpg'));%采集的图像写入
% set(handles.text6,'string',num2str(k));%显示已采集的次数
axes(handles.axes2);
imshow(frame);%显示采集的照片
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles) %退出系统
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
button=questdlg('确定退出人脸识别系统?','退出系统','是','否','是');%内容,标题,选项,默认选项
if strcmp(button,'是')
close;
end
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles) %识别
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global iFaces3;
global ImageAve;
global EigenVectors;
global TrainDir;
[DetectLabels,~]=Detect(iFaces3,ImageAve,EigenVectors);
RealLabels=get(handles.edit3,'String');
if isempty(RealLabels)==1
button=questdlg('请刷IC卡!','警告!','确认','取消','确认');%内容,标题,选项,默认选项
else
if RealLabels==DetectLabels
set(handles.edit2,'string',DetectLabels);
axes(handles.axes4);
imshow(strcat(TrainDir,'\',DetectLabels,'\1.jpg'));
else
set(handles.edit2,'string','人脸与IC卡信息不匹配');
button=questdlg('人脸与IC卡信息不匹配','警告!','确认','取消','确认');%内容,标题,选项,默认选项
end
end
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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 listbox1.
function listbox1_Callback(hObject, eventdata, handles) %选择摄像头
% hObject handle to listbox1 (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 listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
val=get(handles.listbox1,'value');
global Camera;
switch val
case 2
Camera=1;
case 3
Camera=2;
end
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (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
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles) %生成分类器
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global ImageAve;
global EigenVectors;
global TrainDir;
TrainDir='D:\机器视觉\Train';
TrainSet = imageSet(TrainDir,'recursive');
[~,TwoDPCA,EigenVectors,ImageAve,nPerson,nImagePerPerson]=TwoDPCAFeatures(TrainSet,80);
ScaleTwoDPCA=Scale(TwoDPCA);
TrainLabels=extractLabels(TrainSet,nPerson,nImagePerPerson);
classifier = fitcecoc(ScaleTwoDPCA,TrainLabels);%两两分类器
save classifier.mat classifier;
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles) %处理采集的图像
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global frame;
global iFaces3;%设置全局变量,用来检测
faceDetector=vision.CascadeObjectDetector();
image=frame;%读图
bbox=step(faceDetector,image);%找出人脸区域
iFaces=insertObjectAnnotation(image,'rectangle',bbox,'Face');%插入矩形框
[m,~]=size(bbox);
if m~=1
S=zeros(1,m);
for n=1:m
S(1,n)=bbox(n,3)*bbox(n,4);
end
[~,indx]=max(S);
bbox=bbox(indx,:);
end
iFaces2=imcrop(iFaces,bbox);%提取出人脸区域
iFaces3=imresize(iFaces2,[112,92]);%将图像像素拉伸到同一尺度
axes(handles.axes3);
imshow(iFaces3);
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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 button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)%清除识别结果
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.edit2,'String','');
axes(handles.axes4);
cla reset;
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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
未经允许,禁止转载!