Rowan-Classes/5th-Semester-Fall-2023/Signals-and-Systems/Labs/Final-Project/knnFace.m
2024-02-22 14:23:12 -05:00

32 lines
939 B
Matlab

function bestSubject = knnFace(facePath, k, dctData, dctDim, dctDataClass)
%{
facefile path to the face image file
k the number of close neighbors to find
dctData the pretrained dataset
dctDim the dimesion of DCT data to work with
%}
% Find the number of faces in the dataset
N = length(dctData);
% Get the 1-D feature vector of the DCT of the face
features = findfeatures(facePath, dctDim);
% Find the distance from the face to each face in the dataset
dists = zeros(N, 1);
for idx = 1 : N
% Calculate the Euclidean distance in any dimension (found with 2-norm)
dists(idx) = norm(features' - dctData(idx,:), 2);
end
% Sort the distances in ascending order
[sDists, origIdx] = sort(dists);
% Find the corresponding best subjects for the shortest distances
bestSubjects = zeros(k,1);
for idx = 1 : k
bestSubjects(idx) = dctDataClass(origIdx(idx));
end
bestSubject = mode(bestSubjects);