Sei sulla pagina 1di 3

FACE RECOGNITION CODE

clc
clear all
close all
s=serial('COM3')
set(s, 'Timeout',7);

TrainDatabasePath ='.\TrainDatabase';

vidobj = videoinput('winvideo', 1, 'YUY2_320x240');

set(vidobj, 'FramesPerTrigger', Inf);


set(vidobj, 'ReturnedColorspace', 'rgb')
vidobj.FrameGrabInterval = 3;

start(vidobj);
preview(vidobj);

while(vidobj.FramesAcquired<=2000)

fopen(s)
A=fread(s);
fclose(s);
% A=['U' 'U' 'U'];

if(A(1)=='U')

beep
pause(5)

x=getsnapshot(vidobj);

% url='http://192.168.1.31:8080/photo.jpg';
% handles.webcam_image=imread(url);

imgCrop = [];
[aimg k] = locateface(x);

for i=1:k
%%

imgCrop = aimg(:,:,i);
figure
imshow(uint8(imgCrop));
imwrite(uint8(imgCrop),'test.jpg')
TestImage = 'test.jpg';
T = CreateDatabase(TrainDatabasePath);
[m, A, Eigenfaces] = EigenfaceCore(T);
[OutputName index] = Recognition(TestImage, m, A, Eigenfaces);

SelectedImage = strcat(TrainDatabasePath,'\',OutputName);
SelectedImage = imread(SelectedImage);

% imshow(im)
% title('Test Image');

% stop(vidobj);
% SelectedImage1 = strcat(TrainDatabasePath,'\',OutputName);
% SelectedImage1 = imread(SelectedImage1);

% str = strcat('Matched image is PCA : ',OutputName);


% disp(str)

imgCrop=uint8(imgCrop);
eyeloc=locateeye(imgCrop);
noseloc=locatenose(imgCrop);
eye1=[eyeloc(1)+10 eyeloc(4)/2];
eye2=[eyeloc(1)+eyeloc(3)-10 eyeloc(4)/2];
nose1=[noseloc(1)+noseloc(3)/2 noseloc(2)+noseloc(4)/2];
eye_diff=eu_dist(eye1,eye2);
nose_eye1_diff=eu_dist(eye1,nose1)
nose_eye2_diff=eu_dist(eye2,nose1)

if index<6 && nose_eye1_diff<130&& nose_eye1_diff>120&& nose_eye2_diff<130 &&


nose_eye2_diff>120
disp('Person 1 matched')
figure
imshow(SelectedImage);
title('Equivalent Image using PCA');
% fopen(s);
% fwrite(s,'$M');
% fclose(s);
elseif index>5 && index <11 && nose_eye1_diff<119&& nose_eye1_diff>110 &&
nose_eye2_diff<119 && nose_eye2_diff>110
disp('Person 2 matched')
figure
imshow(SelectedImage);
title('Equivalent Image using PCA');
% fopen(s);
% fwrite(s,'#N');
% fclose(s);
elseif index>10 && index <16 && nose_eye1_diff<128&& nose_eye1_diff>118 &&
nose_eye2_diff<128 && nose_eye2_diff>118
disp('Person 3 matched')
figure
imshow(SelectedImage);
title('Equivalent Image using PCA');
% fopen(s);
% fwrite(s,'#N');
% fclose(s);
elseif index>15 && index <21 && nose_eye1_diff<119&& nose_eye1_diff>111 &&
nose_eye2_diff<119 && nose_eye2_diff>111
disp('Person 4 matched')
figure
imshow(SelectedImage);
title('Equivalent Image using PCA');
% fopen(s);
% fwrite(s,'#N');
% fclose(s);
else
disp('Not matched')
% fopen(s);
% fwrite(s,'STR');
% fclose(s);

fopen(s);
fwrite(s,'STL');
fclose(s);

end
end
Closepreview (vidobj);
stop (vidobj);
end
end