Demo entry 6741545

matlab

   

Submitted by anonymous on May 16, 2018 at 06:38
Language: Matlab. Code size: 10.2 kB.

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

% Last Modified by GUIDE v2.5 04-May-2018 02:29:59

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @FaceRecognition_OpeningFcn, ...
                   'gui_OutputFcn',  @FaceRecognition_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 FaceRecognition is made visible.
function FaceRecognition_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 FaceRecognition (see VARARGIN)

% Choose default command line output for FaceRecognition
handles.output = hObject;
global bplay %是否播放
global bcapture%是否抓拍
global faceI

faceI = [];
bcapture = 0; 
bplay = 0;

set(gcf, 'Name', '人脸识别系统');%设置窗口名称 

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = FaceRecognition_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 pushbuttonplayvideo.
function pushbuttonplayvideo_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonplayvideo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global bplay
global bcapture
global faceI

if ~exist('users.mat')%是否训练了
    report = '未发现用户信息,请先训练!';
    msgbox(report,'提示');
    set(handles.textreport,'String',report);
    return
end

if ~exist('trainresult.mat')%是否训练了
    report = '你还未训练样本,请先训练!';
    msgbox(report,'提示');
    set(handles.textreport,'String',report);
    return
end
load trainresult.mat
load users.mat

samplesnum = length(trainlabel);%训练样本个数 

if bplay %如果正在播放 返回
    return
end

bplay = 1;
videoFileReader= imaq.VideoDevice('macvideo', 1, 'YCbCr422_1280x720');
set(videoFileReader,'ReturnedColorSpace','rgb');
while bplay     %循环播放
     
    videoFrame  = step(videoFileReader);%提取一帧
    if bcapture %如果抓拍
%         videoFrame = imread('aa.jpg');
        [faceI,box] = facedetect(videoFrame);
        if(~isempty(faceI)) %如果检测出人脸
            faceI = uint8(faceI*255);
            I = faceI;
            grayI = rgb2gray(I);
            grayI = imresize(grayI,goalsize);%转为固定的目标大小
            I1 = grayI(:);%图像转为1列矩阵
            I1 = double(I1');
            Z=(I1-mA);%减去均值
            feature1 = Z*V;%降维得到PCA特征
            dd = zeros(1,samplesnum);
            for j = 1:samplesnum%遍历训练样本
                feature2 = PCAfeatures(j,:);%取出一个特征
                d = mydist(feature1,feature2);%计算特征之间的距离
                dd(j) = d;%距离存起来 用于排序
            end
            [value,index] = min(dd);%最小值,以及所在位置
            testlabel = trainlabel(index);%获得最近距离样本的标签,就是第几人  
            
            axes(handles.axes2)
            imshow(faceI)
            if value > thresholdvalue
                strreport = sprintf('此人不属于我们人物库内!'); 
                set(handles.textreport,'string',strreport);
                
            else
            
            %取出对应的学号和姓名即可
            stuid = users(testlabel).stuid;
            name = users(testlabel).name;
            
            strreport = sprintf('人物信息:\r\n学号:%s\r\n姓名:%s\r\n',stuid,name);
            
            set(handles.textreport,'string',strreport);
           
            % Update handles structure
            guidata(hObject, handles);
            end
        else
            set(handles.textreport,'string','没有检测到人脸!')
        end
        
        bcapture = 0;
    end
    
    %显示原始图像
    axes(handles.axes1)
    imshow(videoFrame);
    drawnow;                     % 实时更新图像 
end; 

delete(videoFileReader);





% --- Executes on button press in pushbuttonstop.
function pushbuttonstop_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonstop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global bplay
if bplay
    bplay = 0;
end


% --- Executes on button press in pushbuttonclose.
function pushbuttonclose_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonclose (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
pushbuttonstop_Callback(hObject, eventdata, handles) %先关闭摄像头
pause(1.5)%暂停等待摄像头关闭
close


% --- Executes on button press in pushbuttoncapturereco.
function pushbuttoncapturereco_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttoncapturereco (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global bplay
global bcapture
if ~exist('trainresult.mat')%是否训练了
    report = '你还未训练样本,请先训练!';
    msgbox(report,'提示');
    set(handles.textreport,'String',report);
    return
end


if bplay
    bcapture = 1;%设置抓拍 
end


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

if(~isempty(faceI)) %如果检测出人脸
  [filename,pathname]=uiputfile({'*.bmp','BMP files';'*.jpg;','.JPG files'},'设定保存位置');%弹出文件筛选框 选择保存路径
  if isequal(filename,0)||isequal(pathname,0)
      return;
  end
  
  fpath=fullfile(pathname,filename);
  imwrite(faceI,fpath);%保存图片
end


% --- Executes on button press in pushbuttonpictest.
function pushbuttonpictest_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonpictest (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global faceI
if ~exist('users.mat')%是否训练了
    report = '未发现用户信息,请先训练!';
    msgbox(report,'提示');
    set(handles.textreport,'String',report);
    return
end

if ~exist('trainresult.mat')%是否训练了
    report = '你还未训练样本,请先训练!';
    msgbox(report,'提示');
    set(handles.textreport,'String',report);
    return
end

load trainresult.mat
load users.mat
samplesnum = length(trainlabel);%训练样本个数 

[filename,pathname] = uigetfile({'*.jpg;*.bmp;*.tif;*.png;*.gif','All Image Files'},'请选择一张图片');
if filename == 0%如果没有选择,直接返回即可
    return;
end
strfullname = strcat(pathname,filename);%取得图像文件全名  
I = imread(strfullname);%读取图片
axes(handles.axes1)
imshow(I);
[faceI,box] = facedetect(I);

if(isempty(faceI)) %如果检测出人脸
   strreport = sprintf('没有定位出人脸!');

    set(handles.textreport,'string',strreport);
else
  
    grayI = rgb2gray(faceI);
    grayI = imresize(grayI,goalsize);%转为固定的目标大小
    I1 = grayI(:);%图像转为1列矩阵
    I1 = double(I1');
    Z=(I1-mA);%减去均值
    feature1 = Z*V;%降维得到PCA特征
    dd = zeros(1,samplesnum);
    for j = 1:samplesnum%遍历训练样本
        feature2 = PCAfeatures(j,:);%取出一个特征
        d = mydist(feature1,feature2);%计算特征之间的距离
        dd(j) = d;%距离存起来 用于排序
    end
    [value,index] = min(dd);%最小值,以及所在位置
    testlabel = trainlabel(index);%获得最近距离样本的标签,就是第几人  

    axes(handles.axes2)
    imshow(faceI)
    
    if value > thresholdvalue
        strreport = sprintf('此人不属于我们人物库内!'); 
        set(handles.textreport,'string',strreport);
        return
    end

    %取出对应的学号和姓名即可
    stuid = users(testlabel).stuid;
    name = users(testlabel).name;

    strreport = sprintf('人物信息:\r\n学号:%s\r\n姓名:%s\r\n',stuid,name);

    set(handles.textreport,'string',strreport);

end


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

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).