Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The below code is to solve the a real time problem of multiple face detection and
can count the number of persons in the room by counting their faces. It detects
the face by running the video in the program.
CODE :
function multiple_face_detetion_tracking()
profile on
clear all;
clc
% Create system faces used for reading video, detecting moving faces,
% and displaying the results.
obj = initializeObjects();
while(1)
frame = readFrame();
bboxes = detectObjects(frame);
TracksByPredict();
[assignments, unassignedTracks, unassignedDetections] = detectionToTrackAssignment();
updateAssignedTracks();
updateUnassignedTracks();
deleteLostTracks();
addNewTracks();
displayTrackingResults();
end
obj.reader = imaq.VideoDevice();
set(obj.reader,'ReturnedColorSpace','rgb');
obj.videoPlayer = vision.VideoPlayer();
obj.blobAnalyser = vision.CascadeObjectDetector;
obj.blobAnalyser.MinSize=[20 20];
end
bboxes = obj.blobAnalyser.step(frame);
end
function TracksByPredict()
for i = 1:length(tracks)
bbox = tracks(i).bbox;
predictedCentroid = predict(tracks(i).kalmanFilter);
predictedCentroid = predictedCentroid - bbox(1:4) / 2;
nTracks = length(tracks);
nDetections = size(bboxes, 1);
cost = zeros(nTracks, nDetections);
for i = 1:nTracks
cost(i, :) = distance(tracks(i).kalmanFilter, bboxes);
end
costOfNonAssignment = 20;
[assignments, unassignedTracks, unassignedDetections] = ...
assignDetectionsToTracks(cost, costOfNonAssignment);
end
function updateAssignedTracks()
numAssignedTracks = size(assignments, 1);
for i = 1:numAssignedTracks
trackIdx = assignments(i, 1);
detectionIdx = assignments(i, 2);
tracks(trackIdx).age = tracks(trackIdx).age + 1;
tracks(trackIdx).totalVisibleCount = ...
tracks(trackIdx).totalVisibleCount + 1;
tracks(trackIdx).consecutiveInvisibleCount = 0;
end
end
function updateUnassignedTracks()
for i = 1:length(unassignedTracks)
ind = unassignedTracks(i);
tracks(ind).age = tracks(ind).age + 1;
tracks(ind).consecutiveInvisibleCount = tracks(ind).consecutiveInvisibleCount + 1;
end
end
function deleteLostTracks()
if isempty(tracks)
return;
end
invisibleForTooLong = 50;
ageThreshold = 500;
ages = [tracks(:).age];
totalVisibleCounts = [tracks(:).totalVisibleCount];
visibility = totalVisibleCounts ./ ages;
function addNewTracks()
bboxes = bboxes(unassignedDetections, :);
for i = 1:size(bboxes, 1)
tic
bbox = bboxes(i, :);
dtime=toc;
newTrack = struct(...
'id', nextId, ...
'bbox', bbox, ...
'kalmanFilter', kalmanFilter, ...
'age', 1, ...
'totalVisibleCount', 1, ...
'consecutiveInvisibleCount',0,...
'DTime',dtime);
tracks(end + 1) = newTrack;
nextId = nextId + 1;
end
end
function displayTrackingResults()
minVisibleCount = 1;
if ~isempty(tracks)
end
profile report
end
OUTPUT:
The face detection algorithm being used to count number of persons walking out of a restricted place.
Face detection algorithm being used to count the number of faces present in a classroom.
The algorithm is being used to test for multiple faces in the classroom.
We again test the program to detect multiple faces in the classroom. This time we have increased the
number of faces by using a bigger classroom.
The program gives an efficiency of 76.01%. It is not able to detect all the faces in the loop because of the
continuous change in the location of faces between the frames. The program till now has not given any
kind of false positive.