fid=fopen("names.txt","r");
folder="jpg-/";
name=fgetl(fid);
num_faces=143;
face_dims=[115 144];
faces=zeros(num_faces, prod(face_dims));
for i=1:num_faces
final=strcat(folder,name);
A=imread(final);
faces(i,:)=A(:);
name=fgetl(fid);
i++;
endfor
n=i

# input has been taken

mean_face=mean(faces,1);
mean_f = reshape(mean_face(:), face_dims);
imwrite(uint8(mean_f), 'images/mean.jpg');

#found and written the mean image

shifted_faces = faces - repmat(mean_face,size(faces,1), 1);
size(shifted_faces)
covar = shifted_faces*shifted_faces'/num_faces;
[tmp,e] = eig(covar);
size(e)
size(tmp)
EV = shifted_faces'*tmp;
size(EV)
eval=zeros(num_faces,1);
for i=1:num_faces
eval(i,1) = e(i,i);
EV(:,i) = EV(:,i)/norm(EV(:,i));
endfor
#original image
rface = faces(25,:);
rface;
size(rface)
tface = reshape(rface(:), face_dims);
imwrite(uint8(tface), 'images/face25i.jpg');

for m=[2,10,30,80,143]
pca = EV(:,num_faces-m+1:num_faces);
#now project it
proj1 = (rface-mean_face)*pca;
fface = proj1*(pca');
fface +=mean_face;
fface = reshape(fface(:), face_dims);
imwrite(uint8(fface), strcat('images/face25fm=',num2str(m), '.jpg'));
endfor
