📄 multimodefacerecognition.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function kfdFaceRecognition()
% z.li, 07-14-2004
% multi mode face recogntion: eig, fisher, kernel-fisher face based methods
% function dependency:
% - mixedClassify()
% - getATTFaceProj()
% input:
% fm - face model: dim x n
% qId, qPose - an unknown face
% ids - known persons
% poses - known poses
% wt - distance metric weights: dim x 1
% R - local neighbourhood radius
% output:
% recId, nnId, fdaId, knlId, recErr, fdaErr, knlErr, m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%function [recId, nnId, fdaId, knlId, recErr, fdaErr, knlErr, m]=multiModeFaceRecognition(fm, ids, poses, qId, qPose,wt, R)
function [recId, nnId, fdaId, knlId, recErr, fdaErr, knlErr, m]=multiModeFaceRecognition(fm, ids, poses, qId, qPose, wt, R)
dbg = 'n';
if dbg=='y'
% 18x16 lower face, 12-d
Fm1 = load('prjATTUpperFaceS04T08L08W18H16K16.dat');
% 14x18 lower face, 12-d
Fm2 = load('prjATTLowerFaceS01T-06L14W18H12K12.dat');
% 21x28 full face, 20-d
Fm3 = load('prjATTFullFaceModel4.dat');
fm = Fm1;
[dim, nFace]=size(fm);
ids = 11:30; poses = [1 2 3 4 6 7];
ids = [11:40]; poses = [1 3 4 6 7 9]; xPoses = [2 5 8 10];
qId =36; qPose = 2;
wt = ones(dim, 1); R=860;
end
%const
fitLocalFDA = 'y';
minPoseCnt = 3; minLocalSampleRatio = 0.2; defaultNNErr = 0.005;
nPerson = length(ids);
nPose = length(poses);
% face model
[dim, nFace]=size(fm);
% distance metric weight
W=eye(dim);
for k=1:dim
W(k,k)=wt(k);
end
% get unknown face: dim x 1, col vec
xFace = getAttFaceProj(fm, qId, qPose);
if isnan(xFace(1))== 1 % not exist
recId=-2; nnId=-2; fdaId=-2; knlId=-2;
recErr=inf; fdaErr=inf; knlErr=inf; m=0;
fprintf('x');
return;
end
% build local model: X: m x dim, Y: mx1
m=0;
for j=1:length(ids)
thisId = ids(j);
% check if this person has enough sample, if so add to model
kCnt=0;
dist=zeros(length(poses), 1);
for k=1:length(poses) % for a person
thisFace=getAttFaceProj(fm, thisId, poses(k)); % col vec
diff = thisFace-xFace;
dist(k) = sqrt(diff'*W*diff);
if dist(k) < R
kCnt=kCnt+1;
end
end % poses
if kCnt >= minPoseCnt % enough sample in local model
% add to the local model
for k=1:length(poses)
if dist(k) < R
m=m+1;
thisFace=getAttFaceProj(fm, thisId, poses(k)); % col vec
X(m,:)=[thisFace']; Y(m)=thisId;
end
end
end %of enough sample
end % ids
% check the local model
if m <= 0
recId=-3; nnId=-3; fdaId=-3; knlId=-3;
recErr=inf; fdaErr=inf; knlErr=inf; m=0;
fprintf('m');
return;
end
% check the NN result
dv=[];
for k=1:m
dv(k)=norm(xFace'-X(k,:), 2);
end
[minDist, idx]=min(dv);
nnId = Y(idx);
% fit local model
if fitLocalFDA == 'y'
nDim = length(unique(Y));
[ev, A]=lda([X Y'], min(nDim-1, dim));
%projection
for j=1:length(Y)
X2(j,:)=X(j,:)*A;
end
X = X2;
xFace=(xFace'*A)';
end
[fdaId, fdaErr]=mixedClassify(xFace', X, Y, 'linear');
if isinf(fdaErr)==1
fdaId = -1;
end
% check the kernel FDA result
[knlId, knlErr]=mixedClassify(xFace', X, Y, 'quadratic');
if isinf(knlErr)==1
knlId = -1;
end
% combine results
if fdaId == -1 | (m/(nPerson*nPose)) <= minLocalSampleRatio
recId = nnId; recErr = defaultNNErr; fprintf('n');
else
recId = fdaId; recErr = fdaErr; fprintf('f');
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -