【毕业设计专栏】007:基于PCA和SVM的表情识别matlab GUI系统设计

DATE: 2021-05-29



本人提供付费咨询服务并长期承接各类毕设项目以及外包项目。联系QQ: 2963033731 加Q备注:CSDN 外包


1、前言

在大学毕业设计时,实现了一种基于PCA和SVM的表情识别系统,提取特征值,并与数据库里的模板相匹配,从而识别表情,准确率可以达到85%。效果如下所示:

2、表情识别效果

若需要相关代码可以在关注和订阅博主专栏之后,加博主QQ(2963033731)索取(代码付费提供)。

2.1、训练

在这里插入图片描述

2.2、测试

在这里插入图片描述

3、部分Matlab代码

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

% Last Modified by GUIDE v2.5 13-Nov-2016 15:47:45

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

% Choose default command line output for FacialRecSysLearn
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = FacialRecSysLearn_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 loaddb_btn.
function loaddb_btn_Callback(hObject, eventdata, handles)
% hObject    handle to loaddb_btn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global P_train;
global train_label;
addpath train;
mapping=getmapping(8,'u2');%LBP映射
W=[2,1,1,1,1,1,2; ...
    2,4,4,1,4,4,2; ...
    1,1,1,0,1,1,1; ...
    0,1,1,0,1,1,0; ...
    0,1,1,1,1,1,0; ...
    0,1,1,2,1,1,0; ...
    0,1,1,1,1,1,0];
facialkind={'悲伤','高兴','惊奇','恐惧','生气','厌恶','中性'};
templates=[];
train_label=[];
    c=[];
for k=1:size(facialkind,2);
    path = sprintf('train\\%s\',(facialkind{k}));
    DIRS = dir(path);
    n=length(DIRS);
    dirinfo='';
    for i=1:n
        if (~DIRS(i).isdir && ~strcmp(DIRS(i).name,'.') && ~strcmp(DIRS(i).name,'..') )
            dirinfo=strvcat(dirinfo,DIRS(i).name);
        end
    end

    for www=1:size(dirinfo,1)
        file=sprintf('%s\\%s',path,dirinfo(www,:));
        templates{www}=imread(file);
        axes(handles.axes1);
        imshow( templates{www},[])
        train_label=[train_label k];
        
        X = double(templates{www});
        X = imresize(X,[128 128],'bilinear');  %采用'bilinear':采用双线性插值算法扩展为128*128
        H2=DSLBP(X,mapping,W);%提取图片的LBP直方图
        Gray=X;
        Gray=(Gray-mean(Gray(:)))/std(Gray(:))*20+128;
        lpqhist=lpq(Gray,3,1,1,'nh');       %计算每个照片lpq直方图
        a=[H2,lpqhist];
        c=[c;a];%LPB和LPQ特征融合
        
        
        pause(0.01);
    end
    
  
end

  P_train=c;
  P_train=mapminmax(P_train,0,1);



% --- 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 P_train; 
global train_label;
%%%%%从这里开始是识别表情的算法,使用支持向量机来识别
addpath SVM-KM  %%添加支持向量机工具箱
c = 100;
lambda = 1e-7;
kerneloption= 1.3;   %设置核参数
kernel='gaussian'; %设置高斯核作为支持向量机的核函数
verbose = 0;
nbclass=7;
[xsup,w,b,nbsv]=svmmulticlassoneagainstall(P_train,train_label',nbclass,c,lambda,kernel,kerneloption,verbose); %使用支持向量机进行训练获得支持向量
[ypred1,maxi] = svmmultival(P_train,xsup,w,b,nbsv,kernel,kerneloption);  %训练集测试
save('svmdata', 'xsup', 'w', 'b','nbsv');


% --- 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)
facialkind={'悲伤','高兴','惊奇','恐惧','生气','厌恶','中性'};
templates=[];
train_label=[];
c=[];
imageSize = [280,180];          % All Images are resized into a common size
numImage=7*20;
img = zeros(imageSize(1)*imageSize(2),numImage);
index=1;
T=[];
for k=1:size(facialkind,2);
    path = sprintf('train\\%s\',(facialkind{k}));
    DIRS = dir(path);
    n=length(DIRS);
    dirinfo='';
    for i=1:n
        if (~DIRS(i).isdir && ~strcmp(DIRS(i).name,'.') && ~strcmp(DIRS(i).name,'..') )
            dirinfo=strvcat(dirinfo,DIRS(i).name);
        end
    end

    for www=1:size(dirinfo,1)
        file=sprintf('%s\\%s',path,dirinfo(www,:));
        pic=imread(file);
        img=imresize(pic,imageSize);
            
   
         [irow icol] = size(img);
          temp = reshape(img',irow*icol,1);   % Reshaping 2D images into 1D image vectors

          T = [T temp]; % 'T' grows after each turn
         index=index+1;
        axes(handles.axes1);
        imshow(pic,[]) 
    end   
end  
[m, A, Eigenfaces] = EigenfaceCore(T);
save('pcadata', 'm','A','Eigenfaces');

---------------------------------------------THE END!-----------------------------------------------------------

posted @ 2022-04-11 16:17  SoaringLee_fighting  阅读(106)  评论(0编辑  收藏  举报